十二、Feign整合斷路器Hystrix

公衆號: java樂園java

上編說了《RestTemplate+Ribbon整合斷路器Hystrix》,這篇來看看如何Feign整合斷路器Hystrix,Feign整合斷路器Hystrix也是相對比較簡單的。Feign默認已經自帶斷路器Hystrix,因此不須要像RestTemplate+Ribbon整合斷路器Hystrix那樣須要在SpringBoot的啓動類添加註解。可是Feign自帶斷路器並無打開,須要作些額外的配置。web

feign:
   hystrix:
     enabled: true

一、 新建項目sc-eureka-client-consumer-feign-hystrix,對應的pom.xml文件以下spring

<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>

備註:從繼續關係能夠看出spring-cloud-starter-openfeign已經集成斷路器Hystrix數據庫

clipboard.png

二、 新建springboot啓動類apache

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);
    }

}

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

server:
  port: 5800

application.yml

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

eureka:
  client:
    registerWithEureka: true #是否將本身註冊到Eureka服務中,默認爲true
    fetchRegistry: true #是否從Eureka中獲取註冊信息,默認爲true
    serviceUrl:
      defaultZone: http://localhost:5001/eureka/
 
feign:
  hystrix:
enabled: true

說明:在application.yml配置文件添加了開啓斷路器Hystrix的配置項springboot

clipboard.png

四、 新建服務消費者類UserService.javaapp

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註解上添加了一個屬性fallbackmaven

clipboard.png

五、 新建斷路器處理類UserServiceHystrix.javaide

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;
    }

}

該類實現了UserService接口,並實現了該接口的全部方法。

六、 分別啓動註冊中心sc-eureka-server和服務提供者sc-eureka-client-provider

七、 啓動sc-eureka-client-consumer-feign-hystrix,並驗證是否啓動成功
方式一:查看日誌看看對應的端口是啓動

clipboard.png

方式二:查看註冊中心是否註冊成功

clipboard.png

八、 使用postman驗證斷路器是否啓用
上篇從服務提供者是否正常啓動驗證斷路器是否啓動,今天看看從數據庫是否啓動來驗證斷路器是否啓動
(1)MySQL服務正常啓動時訪問:
http://127.0.0.1:5800/feign/user/listUser

clipboard.png

(2)MySQL服務關閉時訪問
http://127.0.0.1:5800/feign/user/listUser

clipboard.png

再看下後臺日誌:

clipboard.png

其餘接口能夠自行按照以上方式進行驗證,看看斷路器是否啓動

相關文章
相關標籤/搜索