目录
一、feign介绍
二、feign的使用
1、feign远程调用
(1)创建项目
(2)编辑pom
(3)编辑yml
(4)编辑主程序
(5)创建三个服务的Service层
(6)创建FeignController
(7)调用流程图示
2、feign负载均衡
3、feign超时和重试
(1)重试的默认参数
(2)编辑yml
4、feign降级
(1)编辑yml
(2)Service修改@FeignClient
(3)添加降级类
5、Feign熔断+dashborad监控仪表盘
(1)配置hystrix
(2)配置actuator
(3)启动dashborad服务
一、feign介绍
feign整合了ribbon和hystrix,不仅能进行远程调用,还具备了负载均衡,重试,降级和熔断的功能。提供了一种声明式的Web服务客户端定义的方式。
使用了我们所熟悉的spring mvc注解来对方法接口进行设置,拼接后台的服务访问路径和提交的参数。
二、feign的使用
1、feign远程调用
(1)创建项目
(2)编辑pom
添加eureka和feign依赖
org.springframework.cloud spring-cloud-starter-netflix-eureka-clientorg.springframework.cloud spring-cloud-starter-openfeign
(3)编辑yml
服务名称为:feign,使用端口:3001
spring:
application:
name: feign
server:
port: 3001
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
(4)编辑主程序
添加@EnableFeignClient注解,开启Spring Cloud Feign的功能
package cn.tedu.sp09;
import ...;
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class Sp09FeignApplication {
public static void main(String[] args) {
SpringApplication.run(Sp09FeignApplication.class, args);
}
}
(5)创建三个服务的Service层
分别添加@FeignClient("微服务名称"),并添加对应的方法,其中方法上使用@RequestMapping注解拼接路径以及参数
itemService
package cn.tedu.sp09.service;
import ...;
@FeignClient("item-service")
public interface ItemFeignService {
@GetMapping("/{orderId}")
JsonResult> getItems(@PathVariable String orderId);
@PostMapping("/decreaseNumber")
JsonResult decreaseNumber(@RequestBody List- items);
}
userService
package cn.tedu.sp09.service;
import ...;
@FeignClient(name="user-service",fallback = UserFeignServiceFB.class)
public interface UserFeignService {
@GetMapping("/{userId}")
JsonResult getUser(@PathVariable Integer userId);
// 拼接路径 /{userId}/score?score=新增积分
//注意,如果请求参数名与方法参数名不同,@RequestParam不能省略,并且要指定请求参数名:
//@RequestParam("score") Integer s
@GetMapping("/{userId}/score")
JsonResult addScore(@PathVariable Integer userId, @RequestParam Integer score);
}
orderService
package cn.tedu.sp09.service;
import ...;
@FeignClient(name="order-service",fallback = OrderFeignServiceFB.class)
public interface OrderFeignService {
@GetMapping("/{orderId}")
JsonResult getOrder(@PathVariable String orderId);
@GetMapping("/")
JsonResult addOrder();
}
(6)创建FeignController
将三个Service注入,分别完成对应路径的方法
package cn.tedu.sp09.controller;
import ...;
@RestController
public class FeignController {
@Autowired
private ItemFeignService itemService;
@Autowired
private UserFeignService userService;
@Autowired
private OrderFeignService orderService;
@GetMapping("/item-service/{orderId}")
public JsonResult> getItems(@PathVariable String orderId) {
return itemService.getItems(orderId);
}
@PostMapping("/item-service/decreaseNumber")
public JsonResult decreaseNumber(@RequestBody List- items) {
return itemService.decreaseNumber(items);
}
//
@GetMapping("/user-service/{userId}")
public JsonResult
getUser(@PathVariable Integer userId) {
return userService.getUser(userId);
}
@GetMapping("/user-service/{userId}/score")
public JsonResult addScore(@PathVariable Integer userId, Integer score) {
return userService.addScore(userId, score);
}
//
@GetMapping("/order-service/{orderId}")
public JsonResult getOrder(@PathVariable String orderId) {
return orderService.getOrder(orderId);
}
@GetMapping("/order-service")
public JsonResult addOrder() {
return orderService.addOrder();
}
}
(7)调用流程图示
2、feign负载均衡
无需其他配置,feign默认开启了负载均衡
3、feign超时和重试
(1)重试的默认参数
ConnectTimeout=1000
ReadTimeout=1000
MaxAutoRetries=0
MaxAutoRetriesNextServer=1
(2)编辑yml
可以在其中对超时参数进行设置
其中,
ribbon.xxx是全局的配置
服务名称.ribbon.xxx是对特定的服务的特殊配置,优先级更高,会覆盖掉全局配置
spring:
application:
name: feign
server:
port: 3001
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
ribbon:
ConnectTimeout: 1000
ReadTimeout: 1000
item-service:
ribbon:
MaxAutoRetries: 1
MaxAutoRetriesNextServer: 2
ConnectTimeout: 1000
ReadTimeout: 500
4、feign降级
默认没有开启降级需要单独配置
(1)编辑yml
添加feign.hystrix.enable=true开启使用降级
...
feign:
hystrix:
enabled: true
此时访问未启动的服务,由于没有降级方法,会报错误信息
也可以为了方便,将超时时间设置小一点
...
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 500
(2)Service修改@FeignClient
添加到对应的降级方法类,类名可以自行命名,这里都以xxxFeignServiceFB形式出现
@FeignClient(name="服务名称",fallback=降级类名.class)
itemService
...
@FeignClient(name="item-service", fallback = ItemFeignServiceFB.class)
public interface ItemFeignService {
...
userService
...
@FeignClient(name="user-service",fallback = UserFeignServiceFB.class)
public interface UserFeignService {
...
orderService
...
@FeignClient(name="order-service",fallback = OrderFeignServiceFB.class)
public interface OrderFeignService {
...
(3)添加降级类
降级类需要实现远程调用Service接口
ItemFeignServiceFB
package cn.tedu.sp09.service;
import ...;
@Component
public class ItemFeignServiceFB implements ItemFeignService{
@Override
public JsonResult> getItems(String orderId) {
return JsonResult.err("无法获取商品订单列表");
}
@Override
public JsonResult decreaseNumber(List- items) {
return JsonResult.err("无法修改商品库存");
}
}
UserFeignServiceFB
package cn.tedu.sp09.service;
import ...;
public class UserFeignServiceFB implements UserFeignService{
@Override
public JsonResult getUser(Integer userId) {
return JsonResult.err("无法获取用户信息");
}
@Override
public JsonResult addScore(Integer userId, Integer score) {
return JsonResult.err("无法增加用户积分");
}
}
OrderFeignServiceFB
package cn.tedu.sp09.service;
import ...;
@Component
public class OrderFeignServiceFB implements OrderFeignService{
@Override
public JsonResult getOrder(String orderId) {
return JsonResult.err("无法获取商品订单");
}
@Override
public JsonResult addOrder() {
return JsonResult.err("无法保存订单");
}
}
5、Feign熔断+dashborad监控仪表盘
(1)配置hystrix
添加hystrix依赖
org.springframework.cloud spring-cloud-starter-netflix-hystrix
主程序添加@EnableCircuitBreaker注解
package cn.tedu.sp09;
import ...;
@EnableCircuitBreaker
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class Sp09FeignApplication {
public static void main(String[] args) {
SpringApplication.run(Sp09FeignApplication.class, args);
}
}
(2)配置actuator
添加actuator依赖
org.springframework.boot spring-boot-starter-actuator
暴露hystrix.stream监控端点
在yml中配置
management:
endpoints:
web:
exposure:
include: hystrix.stream
启动服务查看暴露的端点
http://localhost:3001/actuator
(3)启动dashborad服务
需要创建一个dashborad服务
具体步骤参考以下博客中的hystrix dashborad配置
Hystrix(二)熔断_바 보71的博客-CSDN博客
访问dashborad
访问路径:
http://localhost:4001/hystrix
输入暴露端点
输入暴露的端点:
http://localhost:3001/actuator/hystrix.stream
(4)使用ab进行压力测试
发送20000个请求,并发50
ab -n 20000 -c 50 http://localhost:3001/item-service/35
具体参考
Apache ab(压力测试工具) 的下载和使用_바 보71的博客-CSDN博客_ab测试工具下载



