讲解Feign 实现负载均衡,熔断,且它的使用方法是定义一个接口,然后在上面添加注解。不再需要拼接URL,参数等操作。该文章最后还有已经搭建好的项目,免费领取!!
Feign简介:
Feign是一个声明式WebService客户端.使用Feign能让编写WebService客户端更加简单,它的使用方法是定义一个接口,然后在上面添加注解。不再需要拼接URL,参数等操作,且支持负载均衡,服务熔断,压缩请求,和日志配置 ,而且我们再也不需要去拼接我们的请求地址啦,所以说Feign真的是非常的好用啊。
实现:
1、在我们的消费者引入依赖
2、编写feign客户端接口
3、消费者启动类添加支持Feign注解
4、访问测试接口
实现过程:
在消费者pom.xml文件中引入依赖
<!--配置feign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
创建feign客户端
在消费者中创建UserClient接口:
具体代码实现:
package com.shenwang.feign.client;
import com.shenwang.domain.User;
import com.shenwang.feign.fallback.UserClientFallBack;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author: shenwang
* Date: 2021/7/10
*/
@Service("userClient")
@FeignClient(value = "user-provider")
public interface UserClient {
/**
* 根据id获取用户信息
* @param id
* @return
*/
@RequestMapping(value = "/user/findById/{id}")
User findById(@PathVariable(value = "id") Integer id);
}
注意: FeignClient注解参数value的值对应的是我们提供者application.yml文件中给的服务的名字,RequestMapping参数路径要与提供者对该方法的实现的路径保持一致
Feign会通过动态代理,帮我们生成实现类。
注解@FeignClient声明Feign的客户端,注解value指明服务名称
接口定义的方法,采用SpringMVC的注解。Feign会根据注解帮我们生成URL地址
注解@RequestMapping中的/user,不要忘记。因为Feign需要拼接可访问地址
然后我们控制层直接引用UserClient 调用接口方法
package com.shenwang.controller;
import com.shenwang.feign.client.UserClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author: shenwang
* Date: 2021/7/8
*/
@RestController
@RequestMapping("/consumer")
public class UserController {
@Autowired
private UserClient userClient;
/**
* 根据id获取用户信息
* @HystrixCommand 如果发生问题,则调用降级处理方法
* @param id
* @return
*/
@GetMapping("findById/{id}")
public Object findById(@PathVariable Integer id){
return userClient.findById(id);
}
}
开启Feign
在启动类加上注解@EnableFeignClients
package com.shenwang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* user-consumer 启动类
* @author shenwang
* @EnbelDiscoveryClient 开启Eureka客户端发现功能
* @EnbelCircuitBreaker 开启熔断
* @EnbelFeignClients 开启Feign
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
@EnableFeignClients
public class EurekaConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaConsumerApplication.class, args);
}
/**
* 将RestTemplate实例放到spring容器中
* @LoadBalanced 开启负载均衡
* @return
*/
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
测试:
Feign负载均衡
Feign自身已经集成了Ribbon,因此使用Feign的时候,不需要额外引入依赖。
在application.yml文件中配置负载均衡
Feign内置的ribbon默认设置了请求超时时长,默认是1000,可以修改
ribbon内部有重试机制,一旦超时,会自动重新发起请求。如果不希望重试可以关闭配置:
# 修改服务地址轮询策略,默认是轮询,配置之后变随机
user-provider:
ribbon:
#轮询
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule
ConnectTimeout: 10000 # 连接超时时间
ReadTimeout: 2000 # 数据读取超时时间
MaxAutoRetries: 1 # 最大重试次数(第一个服务)
MaxAutoRetriesNextServer: 0 # 最大重试下一个服务次数(集群的情况才会用到)
OkToRetryOnAllOperations: false # 无论是请求超时或者socket read timeout都进行重试
这样我们的负载均衡就可以使用啦~
Feign熔断支持
feign整合Hystrix熔断器
Feign默认也有对Hystrix的集成!
实现步骤
1. 在配置文件application.yml中开启feign熔断器支持
2. 编写FallBack处理类,实现FeignClient客户端
3. 在@FeignClient注解中,指定FallBack处理类。
在配置文件application.yml中开启feign熔断器支持
feign:
hystrix:
enabled: true # 开启Feign的熔断功能
熔断降级类创建
在子工程 消费者(user-consumer)下创建一类 com.shenwang.feign.fallback.UserClientFallBack
再用该类去实现我们刚刚创建的UserClient 具体代码如下:
package com.shenwang.feign.fallback;
import com.shenwang.domain.User;
import com.shenwang.feign.client.UserClient;
import org.springframework.stereotype.Component;
/**
* 熔断降级类
* @author: shenwang
* Date: 2021/7/12
*/
@Component
public class UserClientFallBack implements UserClient {
/**
* 服务降级处理方法
* @param id
* @return
*/
@Override
public User findById(Integer id) {
User user=new User();
user.setUsername("fallback 服务降级了.....");
return user;
}
}
指定Fallback处理类
在UserClient @FeignClient注解中 指定FallBack处理类
测试
关闭服务的提供者 再去请求服务 再测试结果:
成功熔断!!
今天博客就分享到这里啦
下面是该项目的链接:
链接:https://pan.baidu.com/s/1NeiI5KOnxhxy3sqE_iyY_g
提取码:ahua