I. about Feign
Feign means "pretend, camouflage, transform" in English. It is a lightweight framework for http request calling. It can send http request in the way of interface annotation instead of http client calling by encapsulating http request message. Feign templates the request by processing comments. When it is actually called, it passes in parameters, which are then applied to the request according to the parameters, and then converted into a real request. This kind of request is relatively intuitive. Feign is widely used in Spring Cloud solutions, and is an indispensable component for learning the microservice architecture based on Spring Cloud.
By default, Feign integrates Ribbon and Eureka to achieve the effect of load balancing.
II. Development steps
1. pom configuration
<?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.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.bdqn</groupId> <artifactId>springcloud-feign</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springcloud-feign</name> <description>Demo project for Spring Boot</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR2</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <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> <version>2.1.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2. application.yml configuration
#Service address server: port: 8104 #apply name spring: application: name: springcloud-feign #Address of Registration Center eureka: client: service-url: defaultZone: http://127.0.0.1:8100/eureka register-with-eureka: true fetch-registry: true
3. Create interface
@FeignClient("springcloud-emp") public interface EmpServiceApi { @RequestMapping("/empinfo") String getUserInfo(); }
Springcloud EMP is the service name to call
4. Control layer controller
@RestController public class EmpController { @Autowired EmpServiceApi service; @RequestMapping("/test") public String test(){ String msg = service.getUserInfo(); return msg; } }
- Startup class
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class SpringcloudFeignApplication { public static void main(String[] args) { SpringApplication.run(SpringcloudFeignApplication.class, args); } }
6. test
Start the registry, spring cloud EMP service and feign client. First, check the registry:
Spring cloud EMP services are opened to simulate clusters, and then we can find built-in load balancing through feign client call: