溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

12、Feign整合斷路器Hystrix

發(fā)布時(shí)間:2020-07-26 22:08:38 來源:網(wǎng)絡(luò) 閱讀:1993 作者:huangjinjin520 欄目:編程語言

公眾號: java樂園

上編說了《RestTemplate+Ribbon整合斷路器Hystrix》,這篇來看看如何Feign整合斷路器Hystrix,F(xiàn)eign整合斷路器Hystrix也是相對比較簡單的。Feign默認(rèn)已經(jīng)自帶斷路器Hystrix,所以不需要像RestTemplate+Ribbon整合斷路器Hystrix那樣需要在SpringBoot的啟動類添加注解。但是Feign自帶斷路器并沒有打開,需要做些額外的配置。

feign:
     hystrix:
         enabled: true

1、 新建項(xiàng)目sc-eureka-client-consumer-feign-hystrix,對應(yīng)的pom.xml文件如下

<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>spring-cloud</groupId>
    <artifactId>sc-eureka-client-consumer-feign</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>sc-eureka-client-consumer-feign</name>
    <url>http://maven.apache.org</url>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
    </parent>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

        </dependencies>
    </dependencyManagement>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>

        <!-- 說明是一個 eureka client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

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

        <!-- <dependency> 
                        <groupId>org.springframework.cloud</groupId> 
                        <artifactId>spring-cloud-starter-feign</artifactId> 
                    <version>1.4.5.RELEASE</version> 
             </dependency> -->

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

    </dependencies>
</project>

備注:從繼續(xù)關(guān)系可以看出spring-cloud-starter-openfeign已經(jīng)集成斷路器Hystrix
12、Feign整合斷路器Hystrix
2、 新建springboot啟動類

package sc.consumer;

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

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class ConsumerFeignApplication {

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

}

3、 新建配置文件bootstrap.yml和application.yml
bootstrap.yml

server:
    port: 5800

application.yml

spring:
    application:
        name: sc-eureka-client-consumer-feign-hystrix

eureka:
    client:
        registerWithEureka: true #是否將自己注冊到Eureka服務(wù)中,默認(rèn)為true
        fetchRegistry: true #是否從Eureka中獲取注冊信息,默認(rèn)為true
        serviceUrl:
            defaultZone: http://localhost:5001/eureka/

feign:
    hystrix:
enabled: true

說明:在application.yml配置文件添加了開啟斷路器Hystrix的配置項(xiàng)

12、Feign整合斷路器Hystrix
4、 新建服務(wù)消費(fèi)者類UserService.java

package sc.consumer.service;

import java.util.Map;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import sc.consumer.model.User;
import sc.consumer.service.hystrix.UserServiceHystrix;

@FeignClient(value="sc-eureka-client-provider", fallback=UserServiceHystrix.class)
public interface UserService {

    @GetMapping("/user/getUser/{id}")
    Map<String, Object> getUser(@PathVariable(value ="id") Long id);

    @RequestMapping("/user/listUser")
    Map<String, Object> listUser();

    @PostMapping("/user/addUser")
    Map<String, Object> addUser(@RequestBody User user);

    @PutMapping("/user/updateUser")
    Map<String, Object> updateUser(@RequestBody User user);

    @DeleteMapping("/user/deleteUser/{id}")
    Map<String, Object> deleteUser(@PathVariable(value ="id") Long id);

}

可以看到在該類的FeignClient注解上添加了一個屬性fallback

12、Feign整合斷路器Hystrix
5、 新建斷路器處理類UserServiceHystrix.java

package sc.consumer.service.hystrix;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import sc.consumer.model.User;
import sc.consumer.service.UserService;

@Component
public class UserServiceHystrix  implements UserService{

    @Override
    public Map<String, Object> getUser(Long id) {
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("code", "000000");
        result.put("msg", "success");
        User u = new User();
        u.setId(-1L);
        u.setUserName("failBackName");
        result.put("body", u);
        return result;
    }

    @Override
    public Map<String, Object> listUser() {
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("code", "000000");
        result.put("msg", "success");
        List<User> list = new ArrayList<User>();
        User u = new User();
        u.setId(-1L);
        u.setUserName("failBackName");
        list.add(u);
        result.put("body", list);
        return result;
    }

    @Override
    public Map<String, Object> addUser(User user) {
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("code", "000000");
        result.put("msg", "success");
        result.put("body", 0);
        return result;
    }

    @Override
    public Map<String, Object> updateUser(User user) {
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("code", "000000");
        result.put("msg", "success");
        result.put("body", 0);
        return result;
    }

    @Override
    public Map<String, Object> deleteUser(Long id) {
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("code", "000000");
        result.put("msg", "success");
        result.put("body", 0);
        return result;
    }

}

該類實(shí)現(xiàn)了UserService接口,并實(shí)現(xiàn)了該接口的所有方法。

6、 分別啟動注冊中心sc-eureka-server和服務(wù)提供者sc-eureka-client-provider

7、 啟動sc-eureka-client-consumer-feign-hystrix,并驗(yàn)證是否啟動成功
方式一:查看日志看看對應(yīng)的端口是啟動
12、Feign整合斷路器Hystrix
方式二:查看注冊中心是否注冊成功
12、Feign整合斷路器Hystrix
8、 使用postman驗(yàn)證斷路器是否啟用
上篇從服務(wù)提供者是否正常啟動驗(yàn)證斷路器是否啟動,今天看看從數(shù)據(jù)庫是否啟動來驗(yàn)證斷路器是否啟動
(1)MySQL服務(wù)正常啟動時(shí)訪問:
http://127.0.0.1:5800/feign/user/listUser

12、Feign整合斷路器Hystrix
(2)MySQL服務(wù)關(guān)閉時(shí)訪問
http://127.0.0.1:5800/feign/user/listUser
12、Feign整合斷路器Hystrix
再看下后臺日志:

12、Feign整合斷路器Hystrix
其他接口可以自行按照以上方式進(jìn)行驗(yàn)證,看看斷路器是否啟動

源碼 https://gitee.com/hjj520/spring-cloud-2.x

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI