springcloud之使用feign消費服務並且使用hystrix進行熔斷

服務消費(新建一個feign工程並且使用hystrix)

項目截圖

在這裏插入圖片描述

配置文件

pom.xml文件

<?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">
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath/>
    </parent>

    <modelVersion>4.0.0</modelVersion>

    <groupId>xiejx.cn</groupId>
    <artifactId>eureka-consumer-feign-hystrix</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>


    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 負載均衡-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>

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

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

</project>

application.yml

spring:
  application:
    name: eureka-consumer
server:
  port: 3001
#server.port=${random.int[10000,19999]}
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:1001/eureka
logging:
  file: ./logs/${spring.application.name}.log
feign:
  hystrix:
    enabled: true

配置文件需要注意的就是需要打開feign.hystrix.enable的配置並且springboot2.0以上需要手動依賴netflix-hystrix

代碼

啓動的主類

package jfun;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;

/**
 * @Author MIV
 * @Date 2019/4/15
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
@EnableFeignClients
public class FeignHystrixApp {

    public static void main(String[] args) {
        new SpringApplicationBuilder(FeignHystrixApp.class).web(true).run(args);
    }
}

服務接口

package jfun;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * @Author Miv
 * @Date 2019/4/15
 */
@FeignClient(name = "eureka-client", fallback = DcClientFailBack.class)
public interface DcClient {

    @HystrixCommand
    @GetMapping("/dc")
    String consumer();
}

熔斷實現

package jfun;

import org.springframework.stereotype.Component;

/**
 * @Author Miv
 * @Date 2019/4/15
 */
@Component
public class DcClientFailBack implements DcClient {

    @Override
    public String consumer() {
        System.out.println("熔斷,默認回調函數");
        return "熔斷,默認回調函數";
    }
}

服務層

package jfun;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author Miv
 * @Date 2019/4/15
 */
@Service
public class DcService {
    @Autowired
    DcClient dcClient;

    public String dc() {
        return dcClient.consumer();
    }


}

控制器

package jfun;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @Author Miv
 * @Date 2019/4/15
 */
@RestController
public class DcController {

    @Autowired
    DcService dcService;

    @GetMapping("/consumer")
    public String dc() {
        return dcService.dc();
    }

}

監控

配置文件

pom.xml

<?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>xiejx.cn</groupId>
    <artifactId>hystrix-dashboard</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-parent</artifactId>
        <version>Dalston.SR1</version>
        <relativePath />
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>


</project>

application.yml

spring:
  application:
    name: hystrix-dashboard
server:
  port: 4000

代碼

package jfun;

import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

/**
 * @Author Miv
 * @Date 2019/4/15
 */
@EnableHystrixDashboard
@SpringCloudApplication
public class HystrixDashboardApp {
    public static void main(String[] args) {
        System.out.println("======================訪問管理hystrix======================");
        System.out.println("http://localhost:4000/hystrix");
        System.out.println("監控feign:http://localhost:3001/hystrix.stream");
        System.out.println("======================訪問管理hystrix======================");

        SpringApplication.run(HystrixDashboardApp.class, args);
    }
}

操作和驗證

啓動服務

  1. 啓動eureka-server服務(服務發現)
  2. 啓動eureka-client(註冊服務)
  3. 啓動eureka-consumer-feign-hystrix(服務消費)
  4. 啓動hystrix-dashboard(服務監控)

請求http://localhost:3001/consumer進行服務消費,可以看到服務正常消費
然後關閉eureka-client,可以看到服務正常熔斷,符合預期。

監控服務

進入http://localhost:4000/hystrix查看監控面板

在這裏插入圖片描述
輸入需要監控的服務,點擊Monitor Stream進行監控
在這裏插入圖片描述

如果此頁面加載不出來,可以重新訪問http://localhost:3001/consumer,然後再一次請求頁面

連接地址

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章