Spring Cloud 使用Turine 监控Feign断熔器

不管现实多么惨不忍睹,都要持之以恒地相信,这只是黎明前短暂的黑暗而已。不要惶恐眼前的难关迈不过去,不要担心此刻的付出没有回报,别再花时间等待天降好运。真诚做人,努力做事!你想要的,岁月都会给你。Spring Cloud 使用Turine 监控Feign断熔器,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

前提:本章包含的技术内容:Eureka Server、Hystrix、Feign、Turbine、Ribbon、Eureka client

本期Spring Cloud Version :Greenwich SR1

架构图如下图:

Spring Cloud 使用Turine 监控Feign断熔器

1、Eureka Server

application.yml


spring:
 application:
  name: eureka-server

 profiles:
   active: master

eureka:
 client:
  #此项目不作为客户端注册
  register-with-eureka: false
  # 是否从其他的服务中心同步服务列表
  fetch-registry: false
  #registerWithEureka: false
  #fetchRegistry: false
 server:
  enable-self-preservation: false
  eviction-interval-timer-in-ms: 5000

logging:
 level:
  com.netflix: INFO

application-master.yml

server:
  host: master
  port: 8761


eureka:
  instance:
    hostname: master

pom.xml

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

SpringCloudEurekaServerApplication

package com.xing.eurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class SpringCloudEurekaServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringCloudEurekaServerApplication.class, args);
	}

}

2、Order provider

application.yml

spring:
  profiles:
   active: salv1
  application:
    name: provider-core

application-salv1.yml

server:
  port: 8088

euraka:
  client:
    #将此项目注册到Eureka服务
    register-with-eureka: true
    serviceUrl:
      #defaultZoo: 
      defaultZoo: http://master:8761/eureka/
    instance:
       preferIpAddress: true
    instanst:
      metadata-map:
        cluster: interface

SpringCloudProviderCoreApplication

package com.xing.providercore;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class SpringCloudProviderCoreApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringCloudProviderCoreApplication.class, args);
	}

}

pom.xml

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

OrderController

package com.xing.providercore.controller;

import com.xing.providercore.entity.Order;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

/**
 * @Class UserController
 * @Author 作者姓名:刘兴
 * @Version 1.0
 * @Date 创建时间:2019-09-21 09:04
 * @Copyright Copyright by wisestar company
 * @Direction 类说明
 */
@RestController
public class OrderController {

    private final Logger logger = LoggerFactory.getLogger(getClass());
    private static Random random = new Random() ;

    @GetMapping("/{id}")
    public Order findById(@PathVariable Integer id) throws Exception {
        logger.info("订单查询中心接口:查询订单"+ id +"信息 --- 测试Hytrix 默认超时间是否超时..... ");
        Thread.sleep(2000);
        Order findOne = new Order() ;
        findOne.setOrderPrice( random.nextDouble( ) );
        findOne.setId( id );
        findOne.setOrderName("PS4 SLIM 500G");
        findOne.setOrderCode("20190901-PS4-001");
        return findOne;
    }

}

Order

package com.xing.providercore.entity;

/**
 * @Class Order
 * @Author 作者姓名:刘兴
 * @Version 1.0
 * @Date 创建时间:2019-09-20 21:25
 * @Direction 类说明
 */
public class Order {

    private Integer id;
    private String orderCode;
    private String orderName;
    private Double orderPrice;

    get set ..................
}

3、Client Hystrix

application.yml

server:
  port: 8095

spring:
  application:
    name: client-feign

euraka:
 client:
    # 将服务注册是Eureka-server
    register-with-eureka: true
    serviceUrl:
      defaultZoo: http://master:8761/eureka/
    # 配合 turbine 启动,然后监测各个consumer端的hystrix 的运行情况
    instanst:
      metadata-map:
        cluster: client_feign

management:
  endpoints:
    web:
      exposure:
        include: '*'

feign:
  hystrix:
    enabled: true
# 说明:请务必注意,从Spring Cloud Dalston开始,Feign默认是不开启Hystrix的

SpringCloudClientHystrixApplication.java

package com.xing.hystrix;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;


/***
 * hystrix监控地址: http://127.0.0.1:8095/actuator/hystrix.stream
 */
@EnableEurekaClient
@SpringBootApplication
@EnableFeignClients
@EnableCircuitBreaker
public class SpringCloudClientHystrixApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringCloudClientHystrixApplication.class, args);
	}

}

pom.xml

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</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-netflix-eureka-client</artifactId>
</dependency>

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

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

OrderController.java

package com.xing.hystrix.controller;

import com.xing.hystrix.entity.Order;
import com.xing.hystrix.feign.OrderFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Class OrderController
 * @Author 作者姓名:刘兴
 * @Version 1.0
 * @Date 创建时间:2019-09-21 10:40
 * @Direction 类说明
 */
@RestController
public class OrderController {

    @Autowired
    private OrderFeignClient orderFeignClient;

    @GetMapping("/user/{id}")
    public Order findById(@PathVariable Integer id) {
        return this.orderFeignClient.findById(id);
    }

}

OrderFeignClient.java

package com.xing.hystrix.feign;

import com.xing.hystrix.entity.Order;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/***
 * 使用Feign 集成调用服务
 */

/**
 * Feign的fallback测试
 * 使用@FeignClient的fallback属性指定回退类
 */
@FeignClient(name = "provider-core", fallback = FeignClientFallback.class/*, configuration = FeignDisableHystrixConfiguration.class*/)
public interface OrderFeignClient {

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Order findById(@PathVariable("id") Integer id);

}

/**
 * 回退类FeignClientFallback需实现Feign Client接口
 * FeignClientFallback也可以是public class,没有区别
 */
@Component
class FeignClientFallback implements OrderFeignClient {

    @Override
    public Order findById(Integer id) {
        Order order = new Order();
        order.setId( id );
        order.setOrderName("默认订单");
        order.setOrderPrice( 0.00 );
        order.setOrderCode( "0000_0000_0000" );
        return order;
    }
}

4、我们先启动看看效果,这个启动顺序我是不会说的

Spring Cloud 使用Turine 监控Feign断熔器

看看我们的Hystrix生效了没 …

Spring Cloud 使用Turine 监控Feign断熔器

Spring Cloud 使用Turine 监控Feign断熔器

这种情况使用:修改 Client Hystrix :application.yml,加入下面

ribbon:
  OkToRetryOnAllOperations: false #对所有操作请求都进行重试,默认false
  ReadTimeout: 3000               #负载均衡超时时间,默认值5000
  ConnectTimeout: 1000            #ribbon请求连接的超时时间,默认值2000
  MaxAutoRetries: 0               #对当前实例的重试次数,默认0
  MaxAutoRetriesNextServer: 1     #对切换实例的重试次数,默认1

hystrix:
  command:
    default:  #default全局有效,service id指定应用有效
      execution:
        timeout:
          #如果enabled设置为false,则请求超时交给ribbon控制,为true,则超时作为熔断根据
          enabled: true
        isolation:
          thread:
            timeoutInMilliseconds: 5000 #断路器超时时间,默认1000ms

重启我们们看看效果。

Spring Cloud 使用Turine 监控Feign断熔器

5、Turbine 

application.yml

server:
  port: 8900

spring:
  application:
    name: hystrix-turbine

euraka:
 client:
    # 将服务注册是Eureka-server
    register-with-eureka: true
    serviceUrl:
      defaultZoo: http://master:8761/eureka/
turbine:
  combine-host-port: true
  # 需要监控的应用名称:注册至Erueka Server的名词
  app-config: client-interface
  cluster-name-expression: metadata['cluster']
  aggregator:
    # 取consumer的Eureka 实例配置值:instanst: metadata-map: cluster
    cluster-config: interface
  instanceUrlSuffix: /turbine.stream

HystrixTurbineApplication.java

package com.xing.hystrix.turbine;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.turbine.EnableTurbine;

/***
 *
 * 启动后请打开此页面:http://localhost:8900/hystrix
 *
 *	http://127.0.0.1:8095/actuator/hystrix.stream
 */

@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrixDashboard //注解开启Hystrix仪表盘
@EnableTurbine			//Turbine监控-注册至Eureka server的所有hystrix服务 [注意:需要配置监控的节点名称,节点一般是consumer]
public class HystrixTurbineApplication {

	private static Logger logger = LoggerFactory.getLogger( HystrixTurbineApplication.class ) ;

	// 单台监控:在界面输入 http://localhost:8900/hystrix
	// 多台监控(通过turbine):http://localhost:8900/turbine.stream
	public static void main(String[] args) {
		SpringApplication.run(HystrixTurbineApplication.class, args);
		logger.info("spring-cloud-hystrix-turbine启动!");
	}

}

pom.xml

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>

6、模拟调用链开始运行,监控开启

Spring Cloud 使用Turine 监控Feign断熔器

Spring Cloud 使用Turine 监控Feign断熔器

输入:我们的上面单个:http://127.0.0.1:8095/actuator/hystrix.stream

Spring Cloud 使用Turine 监控Feign断熔器

单个服务处理查看图表。

我们使用集群的试试看,集群意味着你的节点要部署多个节点。

http://127.0.0.1:8900/turbine.stream   

Spring Cloud 使用Turine 监控Feign断熔器

失败了,还要继续研究 集群的Turbine 对 Hystrix的监控。弄好了,我来补上修改的信息。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/160905.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!