1. Review
The previous example uses Resttemplate to implement the rest API call, which is roughly the following code:
@GetMapping ("/user/{id}") public user FindByID (@PathVariable Long id) { Returnthis. Resttemplate.getforobject ("http://microservice-provider-user/" + ID, user. Class); }
By code restraint, we construct the URL using a concatenation string, which has only one parameter.
In reality, however, URLs often have multiple parameters. If you use this method to construct the URL at this point, it becomes inefficient and difficult to maintain.
2. Feign Introduction
Feign is a declarative, templated HTTP client developed by Netflix, inspired by retrofit, JAXRS-2.0, and WebSocket. Feign can help us to invoke HTTP APIs more conveniently and gracefully.
In spring cloud, using feign is very simple-creating an interface and adding some annotations to the interface, the code is done. Feign supports a variety of annotations, such as Feign's own annotations or
Jax-rs annotations and so on.
Spring cloud has enhanced feign, enabling feign to support spring MVC annotations and integrate the Ribbon and Eureka to make feign easier to use.
3. Integrating feign for service consumers
> Copy Project Microservice-consumer-movie, change Artifactid to Microservice-consumer-movie-feign
> Add Dependency of Feign
< Dependency > < groupId >org.springframework.cloud</groupId> < Artifactid >spring-cloud-starter-openfeign</artifactid></ dependency>
> Create a feign interface and add @feignclient annotations
Packagecom.itmuch.cloud.microserviceconsumermoviefeign.feign;ImportCom.itmuch.cloud.microserviceconsumermoviefeign.pojo.User;Importorg.springframework.cloud.openfeign.FeignClient;Importorg.springframework.web.bind.annotation.GetMapping;Importorg.springframework.web.bind.annotation.PathVariable; @FeignClient (name= "Microservice-provider-user") Public Interfaceuserfeignclient {@GetMapping (value= "/{id}") User FindByID (@PathVariable ("id") Long ID);}
The microservice-provider-user in the @FeignClient annotation is an arbitrary client name used to create the ribbon load balancer. In this case, due to the use of Eureka,
So the ribbon parses microservice-provider-user into services in the Eureka Server service registry.
If you do not want to use Eureka, you can also use the Service.ribbon.listOfServers property to configure the server list
You can also use the URL property to specify the requested URL (the URL can be the full URL or host name), for example
@FeignClient (name = "Microservice-provider-user", url = "Http://localhost:8000/")
> Modify the Controller code to invoke the feign interface
PackageCom.itmuch.cloud.microserviceconsumermoviefeign.controller;Importcom.itmuch.cloud.microserviceconsumermoviefeign.feign.UserFeignClient;ImportCom.itmuch.cloud.microserviceconsumermoviefeign.pojo.User;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.web.bind.annotation.GetMapping;Importorg.springframework.web.bind.annotation.PathVariable;ImportOrg.springframework.web.bind.annotation.RestController; @RestController Public classMoviecontroller {@AutowiredPrivateuserfeignclient userfeignclient; @GetMapping ("/user/{id}") PublicUser FindByID (@PathVariable Long id) {return This. Userfeignclient.findbyid (ID); }}
> Modifying the Startup class to add @enablefeignclients annotations
Packagecom.itmuch.cloud.microserviceconsumermoviefeign;Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.SpringBootApplication;Importorg.springframework.cloud.client.discovery.EnableDiscoveryClient;Importorg.springframework.cloud.openfeign.EnableFeignClients;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.web.client.resttemplate;@ Springbootapplication@enablediscoveryclient@enablefeignclients Public classmicroserviceconsumermoviefeignapplication { Public Static voidMain (string[] args) {Springapplication.run (microserviceconsumermoviefeignapplication.class, args); } @Bean Publicresttemplate resttemplate () {return Newresttemplate (); }}
> Start Microservice-discovery-eureka
> Start more than two Microservice-provider-user instances
> Start microservice-consumer-movie-feign
> Multiple access to HTTP://LOCALHOST:8010/USER/1, similar logs are visible under the console of each instance
Hibernate:select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_ 0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=?2018-03-27 17:48:19.468 TRACE 14160---[nio-8001- EXEC-2] o.h.type.descriptor.sql.basicbinder:binding parameter [1] as [BIGINT]-[1]2018-03-27 17:48:19.480 TRACE 14160---[nio-8001-exec-2] o.h.type.descriptor.sql.basicextractor:extracted value ([age 2_0_0_]: [INTEGER])-[20]2018-03-27 17:48:19.481 TRACE 14160---[nio-8001-exec-2] o.h.type.descriptor.sql.basicextractor:extracted value ([Bal Ance3_0_0_]: [NUMERIC])-[100.00]2018-03-27 17:48:19.481 TRACE 14160---[nio-8001-exec-2] o.h.type.descriptor.sql.basicextractor:extracted value ([Nam E4_0_0_]: [VARCHAR])-[Zhang San]2018-03-27 17:48:19.481 TRACE 14160---[nio-8001-exec-2] o.h.type.descriptor.sql.basicextractor:extracted value ([use Rname5_0_0_]: [VARCHAR])-[account1]
4. Summary
The results show that this paper realizes the declarative Rest API call, and also realizes the load balance on the client side.
The custom feign configuration is explained below, so please expect ~ ~ ~
5. Reference
Li---"Spring Cloud and Docker microservices architecture and Combat"
Springcloud series 10: Using feign to implement declarative rest calls