前面我们通过两篇文章介绍了一下spring cloud ribbon实现负载均衡
- SpringCloud Ribbon负载均衡的集成引入
- SpringCloud Ribbon负载均衡的实现及自定义负载均衡
今天主要给大家介绍一下负载均衡的另一种实现方式,废话不多说,开始今天的内容。
1)Feign 简介
feign是声明式的web service客户端,它使微服务之间的调用更加简单,就像传统ssm框架中controller调用service一样,SpringCloud集成了Eureka和Ribbon,可在使用Feign时提供负载均衡的http客户端。
只需要创建一个接口,添加注解即可
调用微服务的方式:
- 通过微服务名称调用【ribbon】
- 通过接口和注解调用【feign】
2)Feign 的原理
- 启动时扫描所有包下所有
@FeignClient
注解的类,并将这些类注入到spring的IOC容器中。当定义的Feign中的接口被调用时,通过JDK的动态代理来生成RequestTemplate
。 RequestTemplate
中包含请求的所有信息,如请求参数,请求URLRequestTemplate
生成Request
,然后将Request
交给client处理- 最后client封装成
LoadBaLanceClient
,结合ribbon负载均衡地发起调用
3)Feign的集成实现
- 引入依赖,修改pom.xml
<!-- Feign依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
- 创建Feign接口
@Component
@FeignClient(value = "pro_user") //value:微服务名称
public interface IUserService {
@GetMapping("/api/user/add")
public boolean addUser(User user);
@GetMapping("/api/user/query/{id}")
public User queryUserById(@PathVariable("id")Long id);
@GetMapping("/api/user/query")
public List<User> queryUserList();
}
- 消费者调用
-`@RestController
public class UserController {
@Qualifier("IUserService")
@Autowired
private IUserService userService = null;
@RequestMapping("/con/user/{id}")
public User getser(@PathVariable("id") long id){
return this.userService.queryUserById(id);
}
@RequestMapping("/con/user/save")
public boolean saveUser(String username,String pass){
User u = new User();
u.setUsername(username).setPass(pass);
return this.userService.addUser(u);
}
@RequestMapping("/query")
public List<User> queryAllUser(){
return this.userService.queryUserList();
}
}`
- 修改启动类,添加Feign
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages = {"com.fyy"})
public class FeignConApplication {
public static void main(String[] args) {
SpringApplication.run(FeignConApplication.class,args);
}
}
至此我们就完成了Feign的集成配置,是不是感觉更像我们平常开发中使用的方式呢。这就是Feign的特点
4)FeignClient注解的一些属性
属性名 | 默认值 | 作用 | 备注 |
---|---|---|---|
value | 调用服务名称与name属性相同 | ||
serviceId | 调用服务ID | 已过期 | |
name | 调用服务名称与value属性相同 | ||
url | 全路径地址或hostname,http或https可选 | ||
decode404 | false | 配置响应状态码为404时是否应该抛出FeignExceptions | |
configuration | {} | 自定义当前feign client的一些配置 | 参考FeignClientsConfiguration |
fallback | void.class | 熔断机制,调用失败时,走的一些回退方法,可以用来抛出异常或给出默认返回数据 | 底层依赖hystrix,启动类要加上@EnableHystrix |
path | 自动给所有方法的requestMapping前加上前缀,类似与controller类上的requestMapping | ||
primary | true |
5)Feign配置详解
feign:
client:
config:
default:
connectTimeout: 5000 #连接时间
readTimeout: 5000 #读时
loggerLevel: basic #日志
okhttp:
enabled: true #开启OKhttp来发送请求,默认是使用jdk中的HttpURLConnection发送HTTP请求
hystrix:
enabled: true #开启熔断机制,调用失败时,走的一些回退方法
compression:
request: #请求
enabled: true #开启
mime-types: text/xml,application/xml,application/json #开启支持压缩的MIME TYPE
min-request-size: 2048 #配置压缩数据大小的下限
response: #响应
enabled: true #开启响应GZIP压缩
由于开启GZIP压缩之后,Feign之间的调用数据通过二进制协议进行传输,返回值需要修改为ResponseEntity<byte[]>才可以正常显示,否则会导致服务之间的调用乱码
源码地址:下载地址