zoukankan      html  css  js  c++  java
  • 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 ;
        }
    }

     

  • 相关阅读:
    Nginx设置支持Https
    windows git bash bash: xxx: command not found
    无法打开arxiv.org的解决办法
    divmod的妙用
    py2 to py3
    keras模型可视化
    python中[-1]、[:-1]、[::-1]、[n::-1]使用方法
    ubuntu “快捷方式”
    tf.keras遇见的坑:Output tensors to a Model must be the output of a TensorFlow `Layer`
    ImportError: Failed to import pydot. You must install pydot and graphviz for `pydotprint` to work.
  • 原文地址:https://www.cnblogs.com/mingforyou/p/15652478.html
Copyright © 2011-2022 走看看