springcloud之使用feign消费服务并且使用hystrix进行熔断

版权声明:本文为博主原创文章,未经博主允许不得转载(需要转载联系QQ:787824374)!!! https://blog.csdn.net/qq_19107011/article/details/89326789

服务消费(新建一个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();
    }


}

控制器

扫描二维码关注公众号,回复: 5906488 查看本文章
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,然后再一次请求页面

连接地址

猜你喜欢

转载自blog.csdn.net/qq_19107011/article/details/89326789