SpringCloud: Feign整合Spring cloud circuit breaker(resilience4j)

1。在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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.edu.tju</groupId>
    <artifactId>springcloudcircuitbreakerfeign</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath />
    </parent>
    <properties>
        <maven-compiler-plugin.version>3.6.1</maven-compiler-plugin.version>
        <maven-eclipse-plugin.version>2.10</maven-eclipse-plugin.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>2020.0.2</spring-cloud.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-circuitbreaker-resilience4j -->

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-circuitbreaker-resilience4j</artifactId>
        </dependency>

<!--
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>
-->


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

        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-commons -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-commons</artifactId>
            <version>2.2.9.RELEASE</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>3.0.2</version>
        </dependency>

    </dependencies>


</project>

2.在配置文件application.properties里开启feign的circuit breaker支持:

feign.circuitbreaker.enabled=true
server.port=8091

3.在启动类加上@EnableFeignClients注解
4.编写FeignClient接口,(可以使其抛出错误来进行测试,比如在RequestMapping里配置一个不存在的地址)

@FeignClient(name = "demoservice", url = "http://139.198.xx.xx:9301", fallback = Fallback.class)
public  interface TestClient {

    @RequestMapping(method = RequestMethod.GET, value = "/test")
   String getTime();


}

5.和上一个类的同一个文件里编写用来进行熔断处理的类:

@Component
class Fallback implements TestClient {

    public String getTime() {
        return "exception caught when getting time.";
    }


}

6.编写控制器类,调用Feign接口

package cn.edu.tju.controller;

import cn.edu.tju.service.TestClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    @Autowired
    private TestClient testClient;
    @RequestMapping("/hi")
    public  String test(){
        String result=testClient.getTime();
        return result;
    }
}

7.运行主类,访问接口:http://localhost:8091/hi,输出如下,
SpringCloud: Feign整合Spring cloud circuit breaker(resilience4j)_第1张图片
可以看到熔断逻辑被执行了

你可能感兴趣的:(SpringCloud,spring,cloud,java,微服务)