Spring Cloud--Hystrix配置Dashboard

前言

Github:https://github.com/yihonglei/thinking-in-springcloud

Eureka注册中心:eureka-server

服务提供者(订单服务):eureka-provider-order

Feign-api(服务接口抽象):eureka-feign-api

Feign客户端消费(含hystrix和dashboard):eureka-consumer-feign-hystrix-dashboard

仪表盘:eureka-hystrix-dashboard

一 Feign、Hystrix、dashboard

Feign是一个声明式的伪RPC的REST客户端,基于接口的注解方式,很方便客户端配置。

Spring Cloud集成Ribbon和Eureka以在使用Feign时提供负载均衡的http客户端。

Hystrix基于开源框架Netflix实现了Spring Cloud Hystrix,该框架的目标在于通过控制哪些访问远程系统、

服务等,从而对于网络延迟和故障提供更强大的容错能力。

dashboard是hystrix监控仪表盘。

二 启动eureka-server(注册中心)

执行eureka-server项目EurekaServerApplication类的main方法。

三 启动eureka-provider-order(服务提供者)

执行eureka-provider-order项目EurekaOrderApplication类的main方法。

四 eureka-feign-api(feign抽象接口)

打成jar包,以供别的项目使用。

五 eureka-consumer-feign-hystrix-dashboard

feign通讯,hystrix熔断,hystrix开启dashboard。

1、项目结构

2、pom.xml

<!-- 监控 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<!-- 引入断路器依赖 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

<!--加入依赖-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>

还需要引入feigin-api的jar包。

<!-- feign-api -->
<dependency>
    <groupId>com.lanhuigu</groupId>
    <artifactId>eureka-feign-api</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

  3、application.yml

# 注册到eureka服务端的微服务名称
spring:
  application:
    name: eureka-consumer-feign-hystrix-dashboard
# 服务提供端口
server:
  port: 8009
# 注册到eureka服务端的地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:9000/eureka/
  # 显示指定微服务的名称,默认ip:应用名称:端口(192.168.1.7:eureka-consumer-feign-hystrix:8009)
  instance:
    instance-id: eureka-consumer-feign-hystrix-dashboard-8009
    prefer-ip-address: true
# 开启feign支持hystrix,默认关闭
feign:
  hystrix:
    enabled: true

feign.hystrix.enabled=true开启feign支持hystrix。 

 4、ServerConfig

package com.lanhuigu.config;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * server配置
 *
 * @auther: yihonglei
 * @date: 2019-07-08 19:10
 */
@Configuration
public class ServerConfig {
    @Bean
    public ServletRegistrationBean getServlet(){

        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);  //系统启动时加载顺序
        registrationBean.addUrlMappings("/actuator/hystrix.stream");//路径
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }
}

Spring Cloud F版本之后需要显示配置hystrix的监控。 

5、UserController

package com.lanhuigu.controller;

import com.lanhuigu.order.OrderApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @RestController这个注解等价于spring mvc用法中的@Controller+@ResponseBody
 *
 * @auther: yihonglei
 * @date: 2019-06-17 22:30
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private OrderApi orderApi;

    @RequestMapping(value = "/queryUserInfo", method = {RequestMethod.GET, RequestMethod.POST})
    public String queryUserInfo() {
        String orderInfo = orderApi.queryOrdersByUserId();

        return orderInfo;
    }
}

6、EurekaConsumerFeignHystrixDashboardApplication

package com.lanhuigu;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @SpringBootApplication 启动一个Spring Boot应用程序
 * @EnableDiscoveryClient 服务发现与注册,当应用启动时,将应用注册到配置的注册中心
 *
 * @auther: yihonglei
 * @date: 2019-06-19 11:27
 */
@EnableDiscoveryClient
@SpringBootApplication
@EnableCircuitBreaker
@EnableFeignClients(basePackages = "com.lanhuigu")
public class EurekaConsumerFeignHystrixDashboardApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaConsumerFeignHystrixDashboardApplication.class, args);
    }
}

六 服务说明

1、注册中心

http://localhost:9000

看到eureka-consumer-feign-hystrix-dashboard-8009和eureka-provider-order-8001服务。

2、访问监控

http://localhost:8009/actuator/hystrix.stream 

页面一直打印ping:但是没有东西,因为还没有对服务进行访问。

访问http://localhost:8009/user/queryUserInfo,触发服务访问。

当访问服务后,再看监控台,可以看到监控的内容,以json格式输出,

json看起来不方便,所以引入了仪表盘,即监控的图形界面。

七 eureka-hystrix-dashboard

1、项目结构

2、pom.xml

<!--加入依赖-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>

3、application.yml

server:
  port: 9001
spring:
  application:
    name: eureka-hystrix-dashboard

4、EurekaHystrixDashboardApplication

package com.lanhuigu.hystrix.dashboard;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

/**
 * @SpringBootApplication 启动一个Spring Boot应用程序
 * @EnableDiscoveryClient 服务发现与注册,当应用启动时,将应用注册到配置的注册中心
 *
 * @auther: yihonglei
 * @date: 2019-06-19 11:27
 */
@SpringBootApplication
@EnableHystrixDashboard
public class EurekaHystrixDashboardApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaHystrixDashboardApplication.class, args);
    }
}

@EnableHystrixDashboard开启Hystrix-dashboard。 

5、访问仪表盘

http://localhost:9001/hystrix/

6、将要监控地址配置入仪表盘

然后点击Monitor Stream进入界面,然后就可以看到控制界面。

发布了502 篇原创文章 · 获赞 358 · 访问量 118万+

猜你喜欢

转载自blog.csdn.net/yhl_jxy/article/details/95329627