12、Feign整合斷路器Hystrix

公眾號: java樂園

成都創(chuàng)新互聯(lián)公司專注于企業(yè)全網(wǎng)營銷推廣、網(wǎng)站重做改版、富順網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、HTML5、成都商城網(wǎng)站開發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為富順等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。

上編說了《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>

        <!-- 說明是一個(gè) 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注解上添加了一個(gè)屬性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

名稱欄目:12、Feign整合斷路器Hystrix
文章鏈接:http://muchs.cn/article26/ghsocg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、定制網(wǎng)站、服務(wù)器托管、靜態(tài)網(wǎng)站、ChatGPT、網(wǎng)站維護(hù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)