前言
上一篇文章Spring Cloud进阶之路 | 十五:服务网关集成断路器(zuul + hystrix)中,介绍了服务网关如何集成断路器。但是,与之前其它服务集成断路器一样,我们始终无法直观的看到断路器相关信息。因此,需要集成断路器监控组件,来以图形化的方式友好地展示断路器信息。
准备工作
复用上一篇文章Spring Cloud进阶之路 | 十五:服务网关集成断路器(zuul + hystrix)中的全部工程:xmall-auth、xmall-product、xmall-zuul。
改造zuul
添加依赖
如同其它服务添加断路器监控组件一样,添加spring-cloud-starter-netflix-hystrix-dashboard依赖。
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.luas.cloud</groupId>
<artifactId>java-boot-parent-2.1</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../java-boot-parent-2.1</relativePath>
</parent>
<groupId>com.luas.xmall</groupId>
<artifactId>xmall-zuul</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>xmall-zuul</name>
<description>网关服务</description>
<properties>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</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-oauth2</artifactId>
</dependency>
<!-- nacos cloud -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
开启断路器监控
启动类新增@EnableHystrixDashboard注解。
package com.luas.xmall.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableHystrixDashboard
@EnableFeignClients
@EnableCircuitBreaker
@EnableZuulProxy
@SpringBootApplication
public class XmallZuulApplication {
public static void main(String[] args) {
SpringApplication.run(XmallZuulApplication.class, args);
}
}
配置
修改zuul hystrix默认隔离策略为thread。
zuul:
prefix: /gateway
sensitive-headers:
routes:
auth:
path: /auth/**
service-id: xmall-auth
strip-prefix: true
product:
path: /product/**
service-id: xmall-product
strip-prefix: true
ribbon-isolation-strategy: thread
放开hystrix.stream端点
management:
endpoints:
web:
exposure:
include: '*'
完整的application.yml文件如下。
server:
port: 5566
zuul:
prefix: /gateway
sensitive-headers:
routes:
auth:
path: /auth/**
service-id: xmall-auth
strip-prefix: true
product:
path: /product/**
service-id: xmall-product
strip-prefix: true
ribbon-isolation-strategy: thread
security:
oauth2:
resource:
user-info-uri: http://localhost:7777/oauth/user
prefer-token-info: false
feign:
hystrix:
enabled: true
management:
endpoints:
web:
exposure:
include: '*'
资源服务器安全策略
修改资源服务器安全策略,放开与hystrix监控相关的端点。
package com.luas.xmall.gateway.configuration;
import com.luas.xmall.gateway.oauth2.endpoint.AuthorizationServerEndpoints;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
@Configuration
@EnableResourceServer
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
private String prefixAntPattern;
public ResourceServerConfiguration(ZuulProperties zuulProperties) {
this.prefixAntPattern = StringUtils.isBlank(zuulProperties.getPrefix()) ? "/*" : zuulProperties.getPrefix() + "/*";
}
@Override
public void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers(this.prefixAntPattern + AuthorizationServerEndpoints.TOKEN_ENDPOINT).permitAll()
.antMatchers(this.prefixAntPattern + AuthorizationServerEndpoints.TOKEN_KEY_ENDPOINT).permitAll()
.antMatchers(this.prefixAntPattern + AuthorizationServerEndpoints.CHECK_TOKEN_ENDPOINT).permitAll()
.antMatchers("/favicon.ico").permitAll()
.antMatchers("/proxy.stream").permitAll()
.antMatchers("/hystrix.stream").permitAll()
.antMatchers("/actuator/**").permitAll()
.antMatchers("/webjars/**").permitAll()
.antMatchers("/hystrix/**").permitAll()
.anyRequest().authenticated()
;
}
}
/favicon.ico、/proxy.stream、/hystrix.stream、/actuator/**、/hystrix/**、/webjars/**等端点,必须开放为公共端点,否则,断路器监控界面将不能正常展示,会被Spring Security拦截。
验证
依次启动xmall-product、xmall-auth、xmall-zuul工程,端口分别为8080、7777、5566。
申请授权。
访问http://localhost:5566/actuator/hystrix.stream,不出所料,肯定是一直ping。
与此同时,访问hystrix监控界面肯定是loading状态。下面就访问相关接口,产生监控数据。
使用上一步申请的授权,访问sku接口http://localhost:5566/gateway/product/sku/1122。
此时,hystrix.stream已有数据返回。
与此同时,hystrix监控页面图形化展示已正常。
其中,RibbonCommand为线程池名称,此名称为zuul默认的hystrix创建,正对应了之前配置项中将zuul hystrix隔离策略修改为thread。至于xmall-product则是默认的hystrix创建的线程池,通过feign客户端远程调用产生数据。
问题
如果网关工程中,没有远程调用逻辑,如rest + ribbon、或者feign,此时,hystrix监控界面不展示线程池信息,一直是loading状态,如图。
此种情况的原因为zuul hystrix默认隔离策略为semaphore,而不是thread。
如果网关工程中,存在远程调用逻辑,无论rest + ribbon、或者feign,出现hystrix监控界面不展示情况,除了上述不存在远程调用逻辑的原因之外,还有可能是hystrix默认隔离策略的问题。hystrix默认隔离策略为thread,如果配置为semaphore,则远程调用部分的线程池则不会创建。
hystrix:
command:
default:
execution:
isolation:
strategy: THREAD
也可单独为某服务配置隔离策略。
hystrix:
command:
default:
execution:
isolation:
strategy: THREAD
xmall-product:
execution:
isolation:
strategy: SEMAPHORE
如下图情况,原因为zuul hystrix默认隔离策略未配置为thread,而hystrix默认隔离策略为thread。
如下图情况,原因为zuul hystrix默认隔离策略为thread,而hystrix默认隔离策略为SEMAPHORE。
上述两种异常情况,工程均存在feign远程调用。
源码
github
https://github.com/liuminglei/SpringCloudLearning/tree/master/16/
gitee
https://gitee.com/xbd521/SpringCloudLearning/tree/master/16/
本文系【银河架构师】原创,如需转载请在文章明显处注明作者及出处。
微信搜索【银河架构师】,发现更多精彩内容。