sentinel整合openfeign进行降级

 

具体流程图:(原理)

第一步:POM文件

<?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>vnacos</artifactId>
        <groupId>com.nacos</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>order-sentinel-feign</artifactId>

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

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--nacos-服务注册发现-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <!--1. 添加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>

        <!--加入sentinel依赖  不加入hibernate依赖  会报以下错误:
        Unable to create a Configuration, because no Bean Validation provider could be found. Add a provide
        -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.2.4.Final</version>
        </dependency>

    </dependencies>

</project>

第二部:创建service服务

package com.order.sentinel.feign.service;

import com.order.sentinel.feign.service.Impl.SentinelFeignProductServiceImpl;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * @author vn
 * @version 1.0
 * @date 2022/3/1 20:18
 */

@FeignClient(value = "product-service",path = "/vn/product",fallback = SentinelFeignProductServiceImpl.class)
public interface ProductService {


    @ApiOperation(value = "获取所有库存列表",notes = "不需要任何参数")
    @ApiImplicitParam(name = "",value = "",required = false,dataType = "")
    @GetMapping("/{id}")
    String  getProduct(@PathVariable("id") String id);

}

第三步:创建实现类

package com.order.sentinel.feign.service.Impl;

import com.order.sentinel.feign.service.ProductService;
import org.springframework.stereotype.Component;

/**
 * @author vn
 * @version 1.0
 * @date 2022/3/1 20:35
 */

@Component
public class SentinelFeignProductServiceImpl implements ProductService {

    @Override
    public String getProduct(String id) {
        return "降级";
    }
}

第四步:创建controller

package com.order.sentinel.feign.controller;

import com.order.sentinel.feign.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author vn
 * @version 1.0
 * @date 2022/3/1 20:21
 */


@RestController
@RequestMapping("/vn/sentinel/feign")
public class VnSentinelFeignController {

    @Value("${server.port}")
    private String port;

    @Autowired
    private ProductService productService;

    @GetMapping("/{id}")
    public  String  selectProduct(@PathVariable("id") String id){
        String product = productService.getProduct(id);
        return "成功:" +port + ":" + product;
    }

}

第五步:创建启动类

package com.order;

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

/**
 * @author vn
 * @version 1.0
 * @date 2022/3/1 20:14
 */

@SpringBootApplication
@EnableFeignClients
public class VnSentinelFeignApplication {


    public static void main(String[] args) {
        SpringApplication.run(VnSentinelFeignApplication.class,args);
    }

}

第七步:配置文件

server:
  port: 8040
spring:
  application:
    name: order-sentinel-feign
  cloud:
    nacos:
      server-addr: 192.168.43.197:8848
      discovery:
        username: nacos
        password: nacos
        namespace: public
feign:
  sentinel:
    enabled: true    # openfeign整合sentinel

 

posted @ 2022-03-01 20:58  VNone  阅读(556)  评论(0)    收藏  举报