09.Openfeign整合Sentinel

一、项目结构

1.父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 https://maven.apache.org/xsd/maven-4.0.0.xsd">


    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.luoruiyuan</groupId>
    <artifactId>Spring-Cloud-Lry</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Spring-Cloud-Lry</name>
    <description>Spring-Cloud-Lry</description>
    <packaging>pom</packaging>

    <modules>
        <module>stock</module>
        <module>order</module>
        <module>nacos-config</module>
        <module>sentinel-java</module>
        <module>order-sentinel</module>
        <module>order-oepnfeign-sentinel</module>
        <module>stock-oepnfeign-sentinel</module>
    </modules>

    <properties>
        <java.version>1.8</java.version>
        <spring.boot.version>2.3.12.RELEASE</spring.boot.version>
        <spring.cloud.version>Hoxton.SR12</spring.cloud.version>
        <spring.cloud.alibaba.version>2.2.8.RELEASE</spring.cloud.alibaba.version>
    </properties>

    <!-- 版本管理(子工程引入了才会导jar包) -->
    <dependencyManagement>
        <dependencies>

            <dependency>
                <!--Spring Boot 版本管理-->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-parent</artifactId>
                <version>${spring.boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>


            <!--Spring Cloud 版本管理-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring.cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!--Spring Cloud Alibaba 版本管理-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring.cloud.alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>



    <!-- 直接导入jar包 -->
    <dependencies>
        <!--  Spring Boot启动器  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <!--  测试  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--  WEB场景  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--  开发工具 (feign.sentinel.enabled=true 时不要导入此依赖,不然项目报错)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> --> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </dependencies> </project>

2.stock-oepnfeign-sentinel项目


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>Spring-Cloud-Lry</artifactId>
        <groupId>cn.luoruiyuan</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>stock-oepnfeign-sentinel</artifactId>



    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>


    <dependencies>
        <!--  alibaba 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>

    </dependencies>


</project>

application.yaml

server:
  port: 8991
spring:
  #开发工具
  devtools:
    restart:
      #启动热部署
      enabled: true
  application:
    #应用名称,也是nacos服务名称
    name: stock-openfeign-sentinel-service
  cloud:
    nacos:
      #登录用户名
      username: nacos
      #登录密码
      password: nacos
      #命名空间,根据环境进行设定(dev,uat,pro)
      namespace: public
      #服务器地址
      server-addr: 127.0.0.1:8848

StockController

package cn.luoruiyuan.stock.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author LRY
 * @date 2022/7/2
 */
@RestController
@RequestMapping("/stock")
public class StockController {

    @GetMapping("/addStock")
    public String addStock(){
        double err=1/0;
        return "===减库存===";
    }

}

3.order-oepnfeign-sentinel


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>Spring-Cloud-Lry</artifactId>
        <groupId>cn.luoruiyuan</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>order-openfeign-sentinel</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>


    <dependencies>
        <!--  alibaba 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.yaml

server:
  port: 8990

spring:

  application:
    #应用名称,也是nacos服务名称
    name: order-openfeign-sentinel-service

  cloud:
    nacos:
      #登录用户名,服务端用户名
      username: nacos
      #登录密码,服务端密码
      password: nacos
      #服务器地址
      server-addr: 127.0.0.1:8848

    #sentinel
    sentinel:
      transport:
        #sentinel地址
        dashboard: 127.0.0.1:9908

#openfeign启动sentinel
feign:
  sentinel:
    enabled: true

StockService

package luoruiyuan.openfeign.stock;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient(name = "stock-openfeign-sentinel-service",path="/stock",fallback = StockServiceFallback.class)
public interface StockService {

    @GetMapping("/addStock")
    public String addStock();

}

StockServiceFallback

package luoruiyuan.openfeign.stock;

import org.springframework.stereotype.Component;

/**
 * @author LRY
 * @date 2022/8/6
 */
@Component
public class StockServiceFallback implements StockService{
    @Override
    public String addStock() {
        return "降级";
    }
}

OrderController

package luoruiyuan.order.controller;

import lombok.RequiredArgsConstructor;
import luoruiyuan.openfeign.stock.StockService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author LRY
 * @date 2022/7/2
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/order")
public class OrderController {

    private final StockService stockService;

    @GetMapping("/add")
    public String addOrder() {
        return "==下单成功--" + stockService.addStock();
    }
}

OrderOpenfeignSentinelApplication

package luoruiyuan;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * @author LRY
 * @date 2022/7/2
 */
@SpringBootApplication
@EnableFeignClients
public class OrderOpenfeignSentinelApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(OrderOpenfeignSentinelApplication.class, args);
    }
}

4.访问资源








(1)