Spring Cloud Advanced Road | six: Service Gateway integrated circuit breaker monitoring (zuul + Hystrix Dashboard)

 

Foreword

Previous article Spring Cloud Advanced Road | five: Service Gateway integrated circuit breaker (zuul + hystrix) , we introduce the service gateway how to integrate breaker. However, the integrated circuit breakers and other services before, we still can not see the visual information breaker. Therefore, the need to integrate the circuit breaker monitoring component to graphically show a friendly manner breaker information.

Ready to work

Complex spend an article Spring Cloud Advanced Road | five: Service Gateway integrated circuit breaker (zuul + hystrix) in the whole project: xmall-auth, xmall-product , xmall-zuul.

 

Transformation zuul

Add dependent

Like other services, like adding circuit breaker monitoring component, adding spring-cloud-starter-netflix-hystrix-dashboard dependent.

<?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>

 

Turn the breaker monitoring

New classes start @EnableHystrixDashboard comment.

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);
    }
​
}

 

Configuration

Zuul hystrix modify the default isolation policy for 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

Endpoint release hystrix.stream

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

Complete application.yml file as follows.

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: '*'

 

Resource server security policy

Modify the resource server security policies, endpoints related to hystrix release monitoring .

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/** and other endpoints, the endpoints must be open to the public, otherwise, the circuit breaker monitoring interface will not work show, will be blocked Spring Security.

 

verification

In turn starts xmall-product, xmall-auth, xmall-zuul engineering, ports, respectively 8080,7777,5566.

Application for authorization.

Visit http: // localhost: 5566 / actuator / hystrix.stream, as expected, certainly has been ping.

At the same time, access hystrix monitoring interface is definitely loading state. Here's the relevant access interface to generate monitoring data.

Authorization step application use, access sku Interface http: // localhost: 5566 / gateway / product / sku / 1122.

At this point, hystrix.stream existing data returned.

At the same time, hystrix monitoring page graphical representation has been normal.

Wherein, R ibbonCommand thread pool name that is created zuul default hystrix, positive configuration corresponding to the previous item zuul hystrix isolation strategy will be revised to thread. As xmall-product is the default thread pool hystrix created by feign client remote invocation data.

 

 

problem

If the gateway project, no remote call logic, such as rest + ribbon, or feign, at this time, hystrix monitor interface does not show the thread pool information, it has been loading state, as shown.

The reason this case is zuul hystrix default isolation policy for semaphore, instead of thread.

If the gateway project, there is a remote call logic, no matter rest + ribbon, or feign, hystrix monitor interface does not show happens, except there is no logical reason for the remote call of the above, there may be hystrix default quarantine policy issues. hystrix default isolation policy for thread, if configured as a semaphore, the thread pool is part of the long-distance call is not created.

hystrix:
  command:
    default:
      execution:
        isolation:
          strategy: THREAD

 

You can also be individually configured quarantine policy for a service.

hystrix:
  command:
    default:
      execution:
        isolation:
          strategy: THREAD
    xmall-product:
      execution:
        isolation:
          strategy: SEMAPHORE

 

Below, the reasons for the default isolation Zuul hystrix Thread policy are not configured, the default isolation policy 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/

 

 

本文系【银河架构师】原创,如需转载请在文章明显处注明作者及出处。

微信搜索【银河架构师】,发现更多精彩内容。

 

 

发布了29 篇原创文章 · 获赞 1 · 访问量 2205

Guess you like

Origin blog.csdn.net/liuminglei1987/article/details/104226757