使用OpenFeign结合与精简restTemplate与负载均衡步骤,降低代码的耦合度。
注意引入openfeign的启动依赖
org.springframework.cloud
spring-cloud-starter-openfeign
完整pom
cloud2020
com.ezerbel.cloud
1.0-SNAPSHOT
4.0.0
cloudalibaba-consumer-nacos-order84
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
com.ezerbel.cloud
cloud-api-commons
${project.version}
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-devtools
runtime
true
org.projectlombok
lombok
true
org.springframework.cloud
spring-cloud-starter-openfeign
com.alibaba.cloud
spring-cloud-starter-alibaba-sentinel
com.alibaba.csp
sentinel-core
org.springframework.boot
spring-boot-starter-test
test
开启OpenFeign对sentinel的支持
feign:
sentinel:
enabled: true
完整yml
server:
port: 84
spring:
application:
name: nacos-order-consumer
cloud:
nacos:
discovery:
server-addr: localhost:8848
sentinel:
transport:
#配置Sentinel dashboard地址
dashboard: localhost:8080
#默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口
port: 8719
filter:
enabled: false
#消费者将要去访问的微服务名称(成功注册入nacos的微服务提供者),用于resttemplate调用前的属性注入
service-url:
nacos-user-service: http://nacos-payment-provider
management:
endpoints:
web:
exposure:
include: '*'
# 激活Sentinel对Feign的支持
feign:
sentinel:
enabled: true
使用服务注册中心中的nacos-payment-provider服务,并能够调用其paymentSQL方法。
@FeignClient(value = "nacos-payment-provider",fallback = PaymentFallbackService.class)
public interface PaymentService
{
@GetMapping(value = "/paymentSQL/{id}")
public CommonResult paymentSQL(@PathVariable("id") Long id);
}
@FeignClient(value = "nacos-payment-provider",fallback = PaymentFallbackService.class)
public interface PaymentService
{
@GetMapping(value = "/paymentSQL/{id}")
public CommonResult paymentSQL(@PathVariable("id") Long id);
}
public class BlockHandlers {
public static CommonResult paymentSQLBlockHandler(@PathVariable Long id, BlockException exception){
Payment payment = new Payment(id,null);
String msg = exception.getClass().getCanonicalName() + " ,请求被block,服务暂时不可用! request id = " + id;
return new CommonResult<>(432,msg,payment) ;
}
}
public class FallBacks {
public static CommonResult paymentSQLFallBack(@PathVariable Long id,Throwable e){
Payment payment = new Payment(id,null);
String msg = e.getClass().getCanonicalName() + " ,请求出现异常,现在回滚! request id = " + id;
return new CommonResult<>(434,msg,payment) ;
}
}
当目标服务不可用或出现异常时,调用此类中的同名方法
@Component
public class PaymentFallbackService implements PaymentService
{
@Override
public CommonResult paymentSQL(Long id)
{
return new CommonResult<>(44444,"服务降级返回,---PaymentFallbackService",new Payment(id,"errorSerial"));
}
}
//==================OpenFeign
@Resource
private PaymentService paymentService;
@GetMapping(value = "/consumer/paymentSQL/{id}")
@SentinelResource(value="paymentSQL",blockHandlerClass = BlockHandlers.class,blockHandler = "paymentSQLBlockHandler",fallbackClass = FallBacks.class,fallback = "paymentSQLFallBack")
public CommonResult paymentSQL(@PathVariable Long id)
{
return paymentService.paymentSQL(id);
}
@EnableFeignClients
触发限流后,blockException并没有被友好处理,而是直接抛出了Error页面!
找了好久原因,竟然是因为传入的参数 id 的类型不一致:
public String xxxBlockHandler(@PathVariable long id, BlockException exception){…}
实际上应该传入Long,而不是long。
在给定blockHandler之后,如果没有找到合适的方法供调用,并没有友好提示,有点坑的。
再次测试成功限流