Feign is a declarative REST call client developed by Netflix; Ribbon load balancing and Hystrⅸ service circuit breaker are very basic components for microservice development in our Spring Cloud. Let’s take a look at it together. I hope it will be helpful to everyone. .
Recommended study: "java video tutorial"
Feign is declarative Web Service client, which makes calling between microservices easier, similar to a controller calling service. SpringCloud integrates Ribbon and Eureka and can use Feigin to provide a load-balanced http client. Feign implements load balancing through interfaces and annotations.
(Excerpted from Crazy God Talks JAVA)
What can Feign do?
Feign aims to make it easier to write Java Http clients
When using Ribbon RestTemplate, RestTemplate was used to encapsulate Http requests, forming a set of templated calling methods. . However, in actual development, since service dependencies may be called in more than one place, and an interface is often called in multiple places, a client class is usually encapsulated for each microservice to package the calls of these dependent services. Therefore, Feign has made further encapsulation on this basis, and he will help us define and implement the definition of dependent service interface. Under Feign's implementation, we only need to create an interface and configure it using annotations (similar to the previous The Mapper annotation is marked on the Dao interface, and now a Feign annotation is marked on the microservice interface) to complete the interface binding to the service provider, simplifying the development effort of automatically encapsulating the service call client when using Spring Cloud Ribbon.
Feign integrates Ribbon by default
Using Ribbon to maintain the service list information of MicroServiceCloud-Dept, and achieving client load balancing through polling, unlike Ribbon, Feign only It is necessary to define a service binding interface and implement service invocation elegantly and simply in a declarative way.
<?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"> <parent> <artifactId>springcloud-demo2</artifactId> <groupId>com.you</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>springcloud-eureka-7001</artifactId> <dependencies> <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server --> <!--Eureka Server--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> <version>1.4.6.RELEASE</version> </dependency> <!--热部署--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </project>
server:4. Configure configBeanport: 801
eureka:client:
register-with-eureka: false #No eureka registers itself
service-url:
defaultZone: http://localhost:7001/eureka/
ribbon:
eureka:
enabled: true
package com.you.config; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class ConfigBean { @Bean @LoadBalanced //ribbon /*配置负载均衡实现RestTemplate*/ /*IRule*/ /*RoundRobinRule 轮询 */ /*RandomRule 随机*/ /*AvailabilityFilteringRule 优先过滤掉跳闸、访问故障的服务,对剩下的进行轮询 */ public RestTemplate getRestTemplate() { return new RestTemplate(); } }
package com.you.config; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class ConfigBean { @Bean @LoadBalanced //ribbon /*配置负载均衡实现RestTemplate*/ /*IRule*/ /*RoundRobinRule 轮询 */ /*RandomRule 随机*/ /*AvailabilityFilteringRule 优先过滤掉跳闸、访问故障的服务,对剩下的进行轮询 */ public RestTemplate getRestTemplate() { return new RestTemplate(); } }
package com.you; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.ribbon.RibbonClient; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @EnableEurekaClient @EnableFeignClients(basePackages = { "com.you"}) public class FeignDeptConsumer_80 { public static void main(String[] args) { SpringApplication.run(FeignDeptConsumer_80.class,args); } }
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> <version>1.4.6.RELEASE</version> </dependency>2) Configure Service
package com.you.service; import com.you.pojo.Dept; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @Component @FeignClient(value = "SPRINGCLOUD-PROVIDER-DEPT") public interface DeptClientService { @GetMapping("/dept/aDept/{id}") public Dept getDeptOfId(@PathVariable("id") Long id); }3) Note The service name must be written correctly. The content in GetMapper must be consistent with the provider, otherwise an error will be reported (I spent an afternoon looking for it)The following is the content of the provider 4. ResultIn this way, the data can be obtained, and the default algorithm of load balancing is still polling! Recommended study: "
java video tutorial"
The above is the detailed content of SpringCloud Feign explained in detail. For more information, please follow other related articles on the PHP Chinese website!