【SpringCloud基础】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.jpeony</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.jpeony.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配置
 *
 * @author yihonglei
 */
@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.jpeony.controller;

import com.jpeony.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
 *
 * @author yihonglei
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private OrderApi orderApi;

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

        return orderApi.queryOrdersByUserId();
    }
}

6、EurekaConsumerFeignHystrixDashboardApplication

package com.jpeony;

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 服务发现与注册,当应用启动时,将应用注册到配置的注册中心
 *
 * @author yihonglei
 */
@EnableDiscoveryClient
@SpringBootApplication
@EnableCircuitBreaker
@EnableFeignClients(basePackages = "com.jpeony")
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.jpeony.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 服务发现与注册,当应用启动时,将应用注册到配置的注册中心
 *
 * @author yihonglei
 */
@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进入界面,然后就可以看到控制界面。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值