美文网首页
Sentinel服务熔断OpenFeign

Sentinel服务熔断OpenFeign

作者: Shaw_Young | 来源:发表于2020-09-08 21:55 被阅读0次

    修改84模块

    1. pom.xml加入以下依赖
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    
    1. yml
    server:
      port: 84
    
    spring:
      application:
        name: nacos-order-consumer
      cloud:
        nacos:
          discovery:
            server-addr: localhost:8848
        sentinel:
          transport:
            dashboard: localhost:8080
            port: 8719
    
    service-url:
      nacos-user-service: http://nacos-payment-provider
    
    #对Feign的支持
    feign:
      sentinel:
        enabled: true
    
    1. 带@FeignClient注解的业务接口
    package com.young.springcloud.alibaba.service;
    
    import com.young.springcloud.entities.CommonResult;
    import com.young.springcloud.entities.Payment;
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    
    
    @FeignClient(value = "nacos-payment-provider",fallback = PaymentFallbackService.class)
    public interface PaymentService
    {
        @GetMapping(value = "/paymentSQL/{id}")
        public CommonResult<Payment> paymentSQL(@PathVariable("id") Long id);
    }
    
    1. PaymentFallbackService实现类
    package com.young.springcloud.alibaba.service;
    
    import com.young.springcloud.entities.CommonResult;
    import com.young.springcloud.entities.Payment;
    import org.springframework.stereotype.Component;
    
    @Component
    public class PaymentFallbackService implements PaymentService
    {
        @Override
        public CommonResult<Payment> paymentSQL(Long id)
        {
            return new CommonResult<>(44444,"服务降级返回,---PaymentFallbackService",new Payment(id,"errorSerial"));
        }
    }
    
    1. CircleBreakerController类加入以下代码
    // OpenFeign
    @Resource
    private PaymentService paymentService;
    
    @GetMapping(value = "/consumer/paymentSQL/{id}")
    public CommonResult<Payment> paymentSQL(@PathVariable("id") Long id) {
        return paymentService.paymentSQL(id);
    }
    
    1. 主启动类添加@EnableFeignClients启动Feign的功能
    @EnableDiscoveryClient
    @SpringBootApplication
    @EnableFeignClients
    public class OrderNacosMain84
    {
        public static void main(String[] args) {
            SpringApplication.run(OrderNacosMain84.class, args);
        }
    }
    

    相关文章

      网友评论

          本文标题:Sentinel服务熔断OpenFeign

          本文链接:https://www.haomeiwen.com/subject/vhirdktx.html