Feign introduction
Feign is a declarative Web service client, which makes it easier to write Web service clients, create an interface with feign and annotate it, with pluggable annotation support including feign annotations and jax-rs annotations, feign also supports pluggable encoders and decoders, Spring Cloud adds annotations to spring MVC, with the spring Web using Httpmessageconverters, the Spring cloud integrated with the load-balanced HTTP client provided by the Ribbon and Eureka Fei Enl
Declarative rest Client: Feign
Start eureka_register_service Engineering (Registration Center) and biz-service-0 project (service producer) first
Create a MAVEN project Eureka_feign_client
Pom.xml
<parent><groupId>org.springframework.boot</groupId><artifactId> Spring-boot-starter-parent</artifactid><version>1.4.3.release</version><relativepath/> <!-- lookup parent from repository --></parent><properties> <project.build.sourceencoding>utf-8</project.build.sourceencoding><java.version>1.8</ java.version></properties><dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactid>spring-cloud-starter-eUreka</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</artifactId> <scope>test</scope> </dependency></dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> &nbsP;<artifactid>spring-cloud-dependencies</artifactid> <version> brixton.sr5</version> <type>pom</type> < scope>import</scope></dependency> </dependencies></ Dependencymanagement>
Turn on the feign feature with @enablefeignclients annotations in the application main class
Startup file Feignapplication.java
@SpringBootApplication @enablediscoveryclient@enablefeignclientspublic class Feignapplication {public static void Main (string[] args) {Springapplication.run (feignapplication.class, args); }}
Defining the Service Interface class Userclient.java
Use @feignclient ("biz-service-0") annotations to bind the interface corresponding to the BIZ-SERVICE-0 service
@FeignClient ("biz-service-0") public interface Userclient {@RequestMapping (method = requestmethod.get, value = "/getuse R ") public User getuserinfo (); @RequestMapping (method = requestmethod.get, value = "/getuser") public String getuserinfostr (); @RequestMapping (method = requestmethod.get, value = "/info") public String info ();}
Call the Usercontroller defined above in the Web layer, as follows
@RestControllerpublic class UserController { @Autowired UserClient userClient; @RequestMapping (value = "/getuserinfo ", method = requestmethod.get) public user getuserinfo () { return userclient.getuserinfo (); } @RequestMapping (value = "/getuserinfostr", Method = requestmethod.get) public string getuserinfostr () { return userclient.getuserinfostr (); } @RequestMapping (value = "/info", method = requestmethod.get) public string info () { &Nbsp;return userclient.info (); }}
Application.properties Configuration variables
spring.application.name=feign-consumerserver.port=8004eureka.client.serviceurl.defaultzone=http://localhost:8000/eureka/
Visit http://127.0.0.1:8004/getuserinfo
Summarize:
In fact, the HTTP invocation service method is encapsulated by feign, which allows the client to invoke the method directly as if it were a local method, similar to how the remote service is exposed in Dubbo, except that Dubbo is based on the private binary protocol, and feign is essentially an HTTP client
"Micro-Service Architecture" Springcloud Feign (v)