提交 87a655c8 编写于 作者: W wangfeihu

Sentinel服务熔断(上次提交是ribbon系列,这次是feign系列)

上级 b21a36f7
...@@ -22,6 +22,11 @@ ...@@ -22,6 +22,11 @@
<groupId>com.alibaba.cloud</groupId> <groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency> </dependency>
<!-- openfeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- 引入自己定义的 api 通用包,可以使用 Payment 支付 Entity --> <!-- 引入自己定义的 api 通用包,可以使用 Payment 支付 Entity -->
<dependency> <dependency>
<groupId>org.afeiamic.springcloud</groupId> <groupId>org.afeiamic.springcloud</groupId>
......
...@@ -3,9 +3,11 @@ package com.afeiamic.springcloud; ...@@ -3,9 +3,11 @@ package com.afeiamic.springcloud;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication @SpringBootApplication
@EnableDiscoveryClient @EnableDiscoveryClient
@EnableFeignClients
public class OrderNacosMain84 { public class OrderNacosMain84 {
public static void main(String[] args) { public static void main(String[] args) {
......
...@@ -2,6 +2,7 @@ package com.afeiamic.springcloud.controller; ...@@ -2,6 +2,7 @@ package com.afeiamic.springcloud.controller;
import com.afeiamic.springcloud.entities.Payment; import com.afeiamic.springcloud.entities.Payment;
import com.afeiamic.springcloud.entities.R; import com.afeiamic.springcloud.entities.R;
import com.afeiamic.springcloud.service.PaymentService;
import com.alibaba.csp.sentinel.annotation.SentinelResource; import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException; import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
...@@ -45,4 +46,16 @@ public class CircleBreakerController { ...@@ -45,4 +46,16 @@ public class CircleBreakerController {
Payment payment = new Payment(id, null); Payment payment = new Payment(id, null);
return new R<>(445, "blockHandler-sentinel 限流 , 无此流水 : blockException" + e.getMessage(), payment); return new R<>(445, "blockHandler-sentinel 限流 , 无此流水 : blockException" + e.getMessage(), payment);
} }
//==================OpenFeign
@Resource
private PaymentService paymentService;
@GetMapping("/consumer/paymentSQL/{id}")
public R<Payment> paymentSQL(@PathVariable Long id) {
if (id == 4) {
throw new RuntimeException("没有该id");
}
return paymentService.paymentSQL(id);
}
} }
package com.afeiamic.springcloud.service;
import com.afeiamic.springcloud.entities.Payment;
import com.afeiamic.springcloud.entities.R;
import com.afeiamic.springcloud.service.fallback.PaymentFallbackService;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
/**
* 使用 fallback 方式是无法获取异常信息的,
* 如果想要获取异常信息,可以使用 fallbackFactory 参数
*/
@FeignClient(value = "nacos-payment-provider", fallback = PaymentFallbackService.class) // 调用过程中关闭9003服务者
public interface PaymentService {
@GetMapping(value = "/paymentSQL/{id}")
public R<Payment> paymentSQL(@PathVariable("id") Long id);
}
package com.afeiamic.springcloud.service.fallback;
import com.afeiamic.springcloud.entities.Payment;
import com.afeiamic.springcloud.entities.R;
import com.afeiamic.springcloud.service.PaymentService;
import org.springframework.stereotype.Service;
@Service
public class PaymentFallbackService implements PaymentService {
@Override
public R<Payment> paymentSQL(Long id) {
return new R<>(444, "服务降级返回 , 没有该流水信息", new Payment(id, "errorSerial..."));
}
}
...@@ -18,3 +18,8 @@ spring: ...@@ -18,3 +18,8 @@ spring:
# 消费者将要去访问的微服务名称 ( 注册成功进 nacos 的微服务提供者 ) # 消费者将要去访问的微服务名称 ( 注册成功进 nacos 的微服务提供者 )
service-url: service-url:
nacos-user-service: http://nacos-payment-provider nacos-user-service: http://nacos-payment-provider
# 激活sentinel对feign的支持
feign:
sentinel:
enabled: true
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册