This article is Spring Cloud column Title VII article to learn first six article content contribute to a better understanding of the article:
-
Spring Cloud first post | Spring Cloud Foreword and common components Glance
-
The second Spring Cloud | use and understanding of Eureka registry
-
Spring Cloud Part III | Eureka registration center set up high availability
-
Spring Cloud Part VI | Hystrix Hystrix Dashboard dashboard monitor
First, what is the Feign
Feign Netflix is developed REST client calls a declarative; Ribbon load balancing, Hystrⅸ service fuse is our Spring Cloud service development in micro very basic components used in the process, we also found that they usually occur at the same time , and configuration is also very similar, each has developed a lot of the same code, so Spring Cloud-based Netflix Feign integrated Ribbon and Hystrix two components, so that our development work easier, like Spring boot is Spring + SpringMVC simplified, Spring Cloud Feign on the Ribbon load balancing, Hystrⅸ service fuse is simplified, were further encapsulated in its basis, not only in the configuration greatly simplifies the development work, while also providing a declarative Web services The client-defined way. Dubbo use of similar use.
Second, consumers realized using Feign
1. Create the customer service named (springcloud-service-feign)
2, add dependencies
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
3, add annotations on the startup class
@EnableFeignClients
4, Statement service
HelloService a defined interface through @FeignClient annotation to specify the service name and then bind the service, and then in the notes provided by the Spring MVC to bind the interface of the service provider, as follows:
// use feign client binding annotation name of the remote, the name can be uppercase or lowercase @FeignClient (value = "springcloud-Service-Provider" ) public interface the HelloService { // declare a method that is remote the method provided by the service provider @RequestMapping ( "/ provider / Hello" ) public String Hello (); }
5, call the service using the Controller, the code is as follows
@Autowired Private the HelloService helloService; @ RequestMapping ( "/ the Hello" ) public String the Hello () { // call declarative interface method implementation calls for remote service return helloService.hello (); }
6, application.yml configured as follows
the Spring: the Application: name: springcloud -service- Feign Server: Port: 9091 Eureka: Client: Service - url: defaultzone: HTTP: // localhost: 8700 / Eureka # Client update every 30 seconds from the time service information service Eureka Registry -fetch-seconds the interval the-: 30 # I need to be registered on the service Eureka the Register the -with-Eureka: to true # need to retrieve the service FETCH -registry: to true # heartbeat detection time and renew instance: # tell the server, If I did not give you the 10s of hair heartbeat, on behalf of my fault, and I will weed out, default 90s #Eureka server in I received the last time limit after waiting a heartbeat, in seconds, over the excluded (the client tells the server to wait in accordance with this rule himself) Lease-DURATION-in--expiration seconds The: 10 # 2s to send to the server every time a heartbeat, prove themselves still alive, default 30s time #Eureka sent by the client to the server heartbeat interval in seconds (the client tells the server they will follow the rules) Lease -renewal-in-seconds the interval the-: 2
7 to start the test, visit the address http: // localhost: 9091 / feign / hello
Third, the use Feign support features
Load Balancing:
Spring Cloud provides the Ribbon to achieve load balancing, use Ribbo injected directly into a RestTemplate objects can, RestTemplate has already done the load balancing configuration in Spring Cloud, using Feign also directly can achieve load balancing, the definition of a @FeignClient comment the interface, and then use @RequestMappin annotations to map the remote REST services on the method, which is also responsible for well balanced configuration.
Services fuse:
1, open the file hystrix function application.yml
# Enable hystrix fuse mechanism Feign: hystrix: Enabled: to true
2, the callback specified fuse logic
@FeignClient(value = "springcloud-service-provider", fallback = MyFallback.class)
@Component public class MyFallback the implements the HelloService { @Override public String Hello () { return "remote service is unavailable, the local logical temporarily employed instead of ....." ; } }
3, test, allowing service providers timeout on the line
If you need to capture the exception thrown by the provider can be used:
@FeignClient(value = "springcloud-service-provider", fallbackFactory = MyFallbackFactory.class)
@Component public class MyFallbackFactory implements FallbackFactory<HelloService> { @Override public HelloService create(Throwable throwable) { return new HelloService() { @Override public String hello() { return throwable.getMessage(); } }; } }
Detailed reference case Source: https://gitee.com/coding-farmer/spirngcloud-learn