• Sentinel 整合OpenFeign降级


    package com.wsm.stock.controller;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/stock")
    public class StockController {
    
        @Value("${server.port}")
        String port;
    
        @RequestMapping("/reduct")
        public String reduct(){
            System.out.println("扣减库存");
            return "扣减库存:"+port;
        }
    
        @RequestMapping("/reduct2")
        public String reduct2(){
            int a=1/0;
            System.out.println("扣减库存2");
            return "扣减库存2:"+port;
        }
    }

    pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>springcloudalibaba</artifactId>
            <groupId>com.wsm.springcloud</groupId>
            <version>0.0.1-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>order-openfeign-sentinel</artifactId>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
    <!--            <version>2.5.5</version>-->
            </dependency>
    
            <!-- nacos 服务注册发现  -->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            </dependency>
    
            <!-- 添加 openfeign 依赖 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
    
            <!-- sentinel 启动器 -->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
            </dependency>
        </dependencies>
    
    </project>

    application.yml

    feign:
      sentinel:
        #OpenFeign 整合 Sentinel
        enabled: true
    server:
      port: 8041
      #应用名称  (nacos 会将该名称当作服务名称)
    spring:
      application:
        name: order-openfeign-sentinel
      cloud:
        nacos:
    #      server-addr: 127.0.0.1:8848
          server-addr: 192.168.133.128:8847  #集群 nginx 负载均衡访问 nacos
          discovery:
            username: nacos
            password: nacos
            namespace: public
    feign:
      sentinel:
        #OpenFeign 整合 Sentinel
        enabled: true
    package com.wsm.order.feign;
    
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @FeignClient(value="stock-nacos",path="/stock",fallback = StockFeignServiceFallBack.class)
    public interface StockFeignService {
    
        @RequestMapping("/reduct2")
        public String reduct2();
    }
    package com.wsm.order.feign;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class StockFeignServiceFallBack implements StockFeignService {
        @Override
        public String reduct2() {
            return "降级了!!";
        }
    }
    package com.wsm.order.controller;
    
    import com.wsm.order.feign.StockFeignService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/order")
    public class OrderController {
    
        @Autowired
        StockFeignService stockFeignService;
    
        @RequestMapping("/add")
        public String add(){
            System.out.println("下单成功!");
            String stock_msg = stockFeignService.reduct2();
            return "hello feign "+ stock_msg ;
        }
    }

     

  • 相关阅读:
    垃圾回收机制_合集
    线程_同步应用
    动态给类的实例对象 或 类 添加属性
    【Python】画一个心形
    【JS】网站运行时间
    【Python】random库
    【HTML】iframe嵌套界面自适应,可高度自由收缩
    【HTML】三种方法使HTML单页面输入密码才能访问
    维护
    投喂记录
  • 原文地址:https://www.cnblogs.com/mingforyou/p/15652478.html
Copyright © 2020-2023  润新知