1. Introduction to Feign
Ribbon was used before, but it would be troublesome to splice parameters for each interface. Feign can be understood as adding a layer of encapsulation to Ribbon so that we don't have to deal with those troublesome parameters when invoking the service.
2. Code Implementation
-
Create a new Feign service
-
pom.xml
<?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.pikaqiu.springcloud</groupId> <artifactId>feign</artifactId> <version>0.0.1-SNAPSHOT</version> <name>SpringCloud-Feign</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR2</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <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> <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> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
-
application.yml
server: port: 5002 spring: application: name: feign eureka: client: service-url: defaultZone: http://eureka-server1:3001/eureka/,http://eureka-server2:3002/eureka/ instance: instance-id: Feign
-
In the project startup class, add annotations @EnableEurekaClient and @EnableFeignClients
@SpringBootApplication @EnableEurekaClient @EnableFeignClients public class SpringCloudFeignApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudFeignApplication.class, args); } }
-
Next comes the core interface definition. Create a new interface FeignService.java. Where the @FeignClient parameter corresponds to the client name
/** * @author Luliang Mountain * @date 2019/7/17 */ @FeignClient("eureka-client") public interface FeignService { @RequestMapping("test") String test(String userName); }
-
Now you can see in eurek that two client services and one feign service have been registered!
-
Test access
http://localhost:5002/test?userName = Test
You will find that Feign will poll for access to two client services
The service port number: 4001, received parameters: test The service port number: 4002, received parameters: test
3. Replacement Load Balancing Strategy
Feign's default strategy is rotation training, which means visiting each service one by one, or changing policies or defined policies by itself. There are many defined policies under com. netflix. loadbalance package, which can be chosen by itself, and can also write a policy class by referring to the written policy class.
eureka-client: ribbon: NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
-
eureka-client needs a load balancing service name
-
NFLoadBalancer RuleClassName Policy Class Path, Write the Path in your Project if it's customized