91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

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

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

12、Feign整合斷路器Hystrix

發布時間:2020-07-26 22:08:38 來源:網絡 閱讀:2002 作者:huangjinjin520 欄目:編程語言

公眾號: java樂園

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

feign:
     hystrix:
         enabled: true

1、 新建項目sc-eureka-client-consumer-feign-hystrix,對應的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>

備注:從繼續關系可以看出spring-cloud-starter-openfeign已經集成斷路器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服務中,默認為true
        fetchRegistry: true #是否從Eureka中獲取注冊信息,默認為true
        serviceUrl:
            defaultZone: http://localhost:5001/eureka/

feign:
    hystrix:
enabled: true

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

12、Feign整合斷路器Hystrix
4、 新建服務消費者類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;
    }

}

該類實現了UserService接口,并實現了該接口的所有方法。

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

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

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

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

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

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

武乡县| 彝良县| 阳原县| 冕宁县| 会理县| 重庆市| 丽江市| 武冈市| 同仁县| 瓦房店市| 蒲城县| 和林格尔县| 洪江市| 洱源县| 景宁| 南通市| 莱西市| 平武县| 德保县| 九龙县| 荔浦县| 当涂县| 延津县| 鸡东县| 滦平县| 高台县| 新邵县| 平潭县| 葫芦岛市| 八宿县| 东莞市| 岱山县| 乌恰县| 沂南县| 武清区| 横峰县| 拉孜县| 二连浩特市| 安康市| 南康市| 武宣县|