I. Introduction of FEIGN
Feign is a declarative pseudo-HTTP client that makes it easier to write HTTP clients. With feign, you only need to create an interface and annotate it. It has pluggable annotation features that can be used with feign annotations and jax-rs annotations. The feign supports pluggable encoders and decoders. The feign integrates the Ribbon by default and combines with Eureka to achieve load balancing by default.
Nutshell:
- Feign uses an interface-based annotation
- Feign integrates the Ribbon
Ii. preparatory work
Continue with the previous section of the project, start the Eureka-server, the port is 8761; Start Service-hi two times with ports of 8762, 8773, respectively.
Third, create a feign service
Create a new Spring-boot project, named Serice-feign, in its pom file introduced feign start dependent spring-cloud-starter-feign, Eureka's start relies on Spring-cloud-starter-eureka, the web's start-dependent Spring-boot-starter-web, the code is as follows:
<?xml version= "1.0" encoding= "UTF-8"? ><project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http ://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelversion>4.0.0 </modelVersion> <groupId>com.forezp</groupId> <artifactid>service-feign</artifactid > <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>service-fe Ign</name> <description>demo Project for Spring boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> <relativePath/> <!--lookup parent from repository to </parent> <properties> <p Roject.build.sourceencoding>utf-8</project.build.sourceencoding> <project.reporting.outputencoding >UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties& Gt <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactid>spring-cloud-starter-feig N</artifactid> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-starter-test& lt;/artifactid> <scope>test</scope> </dependency> </dependencies> <d ependencymanagement> <dependencies> <dependency> <groupid>org.springfra Mework.cloud</groupid> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.RC1</version> <type>pom</type> <scope>Import</scope> </dependency> </dependencies> </dependencyManagement> <BUILD&G T <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> &L t;/build> <repositories> <repository> <id>spring-milestones</id> <name>spring milestones</name> <url>https://repo.spring.io/milestone</url><snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories></project>
在工程的配置文件application.yml文件,指定程序名为service-feign,端口号为8765,服务注册地址为http://localhost:8761/eureka/ ,代码如下:
Eureka: client: serviceurl: defaultzone:http://localhost:8761/eureka/ Server: 8765Spring: application: name:service-feign
In the program's startup class Servicefeignapplication, plus the @enablefeignclients annotation opens the feign function:
@SpringBootApplication @enablediscoveryclient@enablefeignclients Public class servicefeignapplication { publicstaticvoid main (string[] args) { Springapplication.run (servicefeignapplication. class , args);} }
Defines a feign interface that specifies which service to invoke through @ feignclient ("service name"). For example, in the code called the Service-hi Service "/hi" interface, the code is as follows:
/***/= "Service-hi")publicinterface Schedualservicehi { = "/hi", method = requestmethod.get) = "name") String name);
At the controller layer of the Web layer, exposes a "/hi" API interface, consumes the service through the feign client Schedualservicehi defined above. The code is as follows:
@RestController Public class Hicontroller { @Autowired Schedualservicehi Schedualservicehi; = "/hi", method = requestmethod.get) public string sayhi (@RequestParam string name) { /c10>return schedualservicehi.sayhifromclientone (name);} }
The schema code is as follows:
Source source technical support for complete projects 2147775633
Enterprise Distribution Micro Service Cloud Springcloud Springboot MyBatis (iii) service consumer (feign)