2.3 服务消费者 - 2.3.2. Feign
优质
小牛编辑
131浏览
2023-12-01
Feign介绍
Feign是一个声明式的web service客户端,它使得编写web service客户端更为容易。创建接口,为接口添加注解,即可使用Feign。Feign可以使用Feign注解或者JAX-RS注解,还支持热插拔的编码器和解码器。Spring Cloud为Feign添加了Spring MVC的注解支持,并整合了Ribbon和Eureka来为使用Feign时提供负载均衡。
译自:http://projects.spring.io/spring-cloud/docs/1.0.3/spring-cloud.html
Feign示例
创建一个Maven项目,并在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>
<artifactId>microservice-consumer-movie-feign</artifactId>
<packaging>jar</packaging>
<parent>
<groupId>com.itmuch.cloud</groupId>
<artifactId>spring-cloud-microservice-study</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
</project>
启动类:MovieFeignApplication.java
/**
* 使用@EnableFeignClients开启Feign
* @author eacdy
*/
@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class MovieFeignApplication {
public static void main(String[] args) {
SpringApplication.run(MovieFeignApplication.class, args);
}
}
实体类:User.java
public class User {
private Long id;
private String username;
private Integer age;
...
// getters and setters
}
Feign测试类:UserFeignClient.java。
/**
* 使用@FeignClient("microservice-provider-user")注解绑定microservice-provider-user服务,还可以使用url参数指定一个URL。
* @author eacdy
*/
@FeignClient(name = "microservice-provider-user")
public interface UserFeignClient {
@RequestMapping("/{id}")
public User findByIdFeign(@RequestParam("id") Long id);
}
Feign的测试类:FeignController.java
@RestController
public class FeignController {
@Autowired
private UserFeignClient userFeignClient;
@GetMapping("feign/{id}")
public User findByIdFeign(@PathVariable Long id) {
User user = this.userFeignClient.findByIdFeign(id);
return user;
}
}
application.yml
server:
port: 8020
spring:
application:
name: microservice-consumer-movie-feign
eureka:
client:
serviceUrl:
defaultZone: http://discovery:8761/eureka/
instance:
preferIpAddress: true
ribbon:
eureka:
enabled: true # 默认为true。如果设置为false,Ribbon将不会从Eureka中获得服务列表,而是使用静态配置的服务列表。静态服务列表可使用:<client>.ribbon.listOfServers来指定。参考:http://projects.spring.io/spring-cloud/docs/1.0.3/spring-cloud.html#spring-cloud-ribbon-without-eureka
### 参考:https://spring.io/guides/gs/client-side-load-balancing/
同样的,启动该应用,多次访问http://localhost:8020/feign/1,我们会发现和Ribbon示例一样实现了负载均衡。
代码地址(任选其一):
Feign代码地址:
http://git.oschina.net/itmuch/spring-cloud-study/tree/master/microservice-consumer-movie-feign
https://github.com/eacdy/spring-cloud-study/tree/master/microservice-consumer-movie-feign