高并发架构实践指南——资源部署(熔断器Hystrix)

开发Hystrix Dashboard

Core Annotation

package tech.foolfish.monitor;

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

@SpringBootApplication
@EnableHystrixDashboard
@EnableTurbine
public class MicroserviceMonitorHystrixDashboardApplication {

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

}

application.yml

eureka:
  client:
    fetchRegistry: true
    registerWithEureka: true
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
hystrix:
  dashboard:
    enable-ignore-connection-close-header: true
    proxy-stream-allow-list:
    - localhost
server:
  port: 7979
spring:
  application:
    name: microservice-monitor-hystrix-dashboard
turbine:
#  aggregator:
#    cluster-config:
#    - STANDARDEXPRESS-SHIPONLINEORDER
  app-config: STANDARDEXPRESS-SHIPONLINEORDER
  clusterNameExpression: "'default'"

pom.xml

<?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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.12.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>tech.foolfish</groupId>
    <artifactId>microservice-monitor-hystrix-dashboard</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>microservice-monitor-hystrix-dashboard</name>
    <description>Demo project for Spring Cloud</description>
    <properties>
        <java.version>11</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.12.RELEASE</spring-boot.version>
        <spring-cloud.version>Hoxton.SR12</spring-cloud.version>
    </properties>
    <dependencies>
        <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>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

部署Hystrix Dashboard

打包并启动

xuej@dev-server:~/Documents/sts4/microservice-monitor-hystrix-dashboard$ mvn clean package -DskipTests=true
xuej@dev-server:~/Documents/sts4/microservice-monitor-hystrix-dashboard$ java -jar target/microservice-monitor-hystrix-dashboard-0.0.1-SNAPSHOT.jar

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::       (v2.3.12.RELEASE)

2022-08-04 04:22:20.483  INFO 24241 --- [           main] erviceMonitorHystrixDashboardApplication : No active profile set, falling back to default profiles: default
2022-08-04 04:22:21.406  WARN 24241 --- [           main] o.s.boot.actuate.endpoint.EndpointId     : Endpoint ID 'service-registry' contains invalid characters, please migrate to a valid format.
2022-08-04 04:22:21.551  INFO 24241 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=f83c0399-e68a-33ec-9fd9-baa5330f969e
2022-08-04 04:22:21.917  INFO 24241 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 7979 (http)
2022-08-04 04:22:21.927  INFO 24241 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-08-04 04:22:21.927  INFO 24241 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.46]
2022-08-04 04:22:22.002  INFO 24241 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-08-04 04:22:22.002  INFO 24241 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1491 ms
2022-08-04 04:22:22.135  WARN 24241 --- [           main] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
2022-08-04 04:22:22.135  INFO 24241 --- [           main] c.n.c.sources.URLConfigurationSource     : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2022-08-04 04:22:22.148  INFO 24241 --- [           main] c.netflix.config.DynamicPropertyFactory  : DynamicPropertyFactory is initialized with configuration sources: com.netflix.config.ConcurrentCompositeConfiguration@23aae55
2022-08-04 04:22:22.412  WARN 24241 --- [           main] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
2022-08-04 04:22:22.413  INFO 24241 --- [           main] c.n.c.sources.URLConfigurationSource     : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2022-08-04 04:22:22.711  INFO 24241 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2022-08-04 04:22:23.078  INFO 24241 --- [           main] DiscoveryClientOptionalArgsConfiguration : Eureka HTTP Client uses Jersey
2022-08-04 04:22:23.161  WARN 24241 --- [           main] ockingLoadBalancerClientRibbonWarnLogger : You already have RibbonLoadBalancerClient on your classpath. It will be used by default. As Spring Cloud Ribbon is in maintenance mode. We recommend switching to BlockingLoadBalancerClient instead. In order to use it, set the value of `spring.cloud.loadbalancer.ribbon.enabled` to `false` or remove spring-cloud-starter-netflix-ribbon from your project.
2022-08-04 04:22:23.200  INFO 24241 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 2 endpoint(s) beneath base path '/actuator'
2022-08-04 04:22:23.231  INFO 24241 --- [           main] o.s.c.n.turbine.SpringAggregatorFactory  : Could not find monitors: []
2022-08-04 04:22:23.326  INFO 24241 --- [           main] c.n.t.monitor.cluster.ClusterMonitor     : Registering event handler for cluster monitor: StaticListener_For_Aggregator
2022-08-04 04:22:23.328  INFO 24241 --- [           main] c.n.t.handler.TurbineDataDispatcher      : 

Just added and starting handler tuple: StaticListener_For_Aggregator
2022-08-04 04:22:23.328  INFO 24241 --- [           main] c.n.t.monitor.cluster.ClusterMonitor     : All event handlers for cluster monitor: [StaticListener_For_Aggregator]
2022-08-04 04:22:23.328  INFO 24241 --- [           main] c.n.t.monitor.cluster.ClusterMonitor     : Starting up the cluster monitor for default_agg
2022-08-04 04:22:23.331  INFO 24241 --- [           main] c.n.t.discovery.InstanceObservable       : Starting InstanceObservable at frequency: 60000 millis
2022-08-04 04:22:23.331  INFO 24241 --- [        Timer-0] o.s.c.n.t.CommonsInstanceDiscovery       : Fetching instance list for apps: [STANDARDEXPRESS-SHIPONLINEORDER]
2022-08-04 04:22:23.332  INFO 24241 --- [        Timer-0] o.s.c.n.turbine.EurekaInstanceDiscovery  : Fetching instances for app: STANDARDEXPRESS-SHIPONLINEORDER
2022-08-04 04:22:23.347  INFO 24241 --- [        Timer-0] o.s.c.n.eureka.InstanceInfoFactory       : Setting initial instance status as: STARTING
2022-08-04 04:22:23.431  INFO 24241 --- [        Timer-0] com.netflix.discovery.DiscoveryClient    : Initializing Eureka in region us-east-1
2022-08-04 04:22:23.661  INFO 24241 --- [        Timer-0] c.n.d.provider.DiscoveryJerseyProvider   : Using JSON encoding codec LegacyJacksonJson
2022-08-04 04:22:23.661  INFO 24241 --- [        Timer-0] c.n.d.provider.DiscoveryJerseyProvider   : Using JSON decoding codec LegacyJacksonJson
2022-08-04 04:22:23.831  INFO 24241 --- [        Timer-0] c.n.d.provider.DiscoveryJerseyProvider   : Using XML encoding codec XStreamXml
2022-08-04 04:22:23.832  INFO 24241 --- [        Timer-0] c.n.d.provider.DiscoveryJerseyProvider   : Using XML decoding codec XStreamXml
2022-08-04 04:22:23.986  INFO 24241 --- [        Timer-0] c.n.d.s.r.aws.ConfigClusterResolver      : Resolving eureka endpoints via configuration
2022-08-04 04:22:24.002  INFO 24241 --- [        Timer-0] com.netflix.discovery.DiscoveryClient    : Disable delta property : false
2022-08-04 04:22:24.002  INFO 24241 --- [        Timer-0] com.netflix.discovery.DiscoveryClient    : Single vip registry refresh property : null
2022-08-04 04:22:24.002  INFO 24241 --- [        Timer-0] com.netflix.discovery.DiscoveryClient    : Force full registry fetch : false
2022-08-04 04:22:24.002  INFO 24241 --- [        Timer-0] com.netflix.discovery.DiscoveryClient    : Application is null : false
2022-08-04 04:22:24.002  INFO 24241 --- [        Timer-0] com.netflix.discovery.DiscoveryClient    : Registered Applications size is zero : true
2022-08-04 04:22:24.002  INFO 24241 --- [        Timer-0] com.netflix.discovery.DiscoveryClient    : Application version is -1: true
2022-08-04 04:22:24.002  INFO 24241 --- [        Timer-0] com.netflix.discovery.DiscoveryClient    : Getting all instance registry info from the eureka server
2022-08-04 04:22:24.193  INFO 24241 --- [        Timer-0] com.netflix.discovery.DiscoveryClient    : The response status is 200
2022-08-04 04:22:24.197  INFO 24241 --- [        Timer-0] com.netflix.discovery.DiscoveryClient    : Starting heartbeat executor: renew interval is: 30
2022-08-04 04:22:24.199  INFO 24241 --- [        Timer-0] c.n.discovery.InstanceInfoReplicator     : InstanceInfoReplicator onDemand update allowed rate per min is 4
2022-08-04 04:22:24.204  INFO 24241 --- [        Timer-0] com.netflix.discovery.DiscoveryClient    : Discovery Client initialized at timestamp 1659558144203 with initial instances count: 4
2022-08-04 04:22:24.206  INFO 24241 --- [        Timer-0] o.s.c.n.turbine.EurekaInstanceDiscovery  : Received instance list for app: STANDARDEXPRESS-SHIPONLINEORDER, size=2
2022-08-04 04:22:24.206  INFO 24241 --- [           main] o.s.c.n.e.s.EurekaServiceRegistry        : Registering application MICROSERVICE-MONITOR-HYSTRIX-DASHBOARD with eureka with status UP
2022-08-04 04:22:24.207  INFO 24241 --- [           main] com.netflix.discovery.DiscoveryClient    : Saw local status change event StatusChangeEvent [timestamp=1659558144207, current=UP, previous=STARTING]
2022-08-04 04:22:24.208  INFO 24241 --- [        Timer-0] c.n.t.discovery.InstanceObservable       : Retrieved hosts from InstanceDiscovery: 2
2022-08-04 04:22:24.208  INFO 24241 --- [        Timer-0] c.n.t.discovery.InstanceObservable       : Found hosts that have been previously terminated: 0
2022-08-04 04:22:24.208  INFO 24241 --- [        Timer-0] c.n.t.discovery.InstanceObservable       : Hosts up:2, hosts down: 0
2022-08-04 04:22:24.214  INFO 24241 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_MICROSERVICE-MONITOR-HYSTRIX-DASHBOARD/10.0.2.15:microservice-monitor-hystrix-dashboard:7979: registering service...
2022-08-04 04:22:24.251  INFO 24241 --- [        Timer-0] c.n.t.monitor.instance.InstanceMonitor   : Url for host: http://10.0.2.15:8082/actuator/hystrix.stream default
2022-08-04 04:22:24.252  INFO 24241 --- [        Timer-0] c.n.t.handler.TurbineDataDispatcher      : 

Just added and starting handler tuple: default_agg_aggClusterEventHandler
2022-08-04 04:22:24.253  INFO 24241 --- [        Timer-0] c.n.turbine.data.AggDataFromCluster      : Per handler dispacher started for: default_agg_aggClusterEventHandler
2022-08-04 04:22:24.298  INFO 24241 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 7979 (http) with context path ''
2022-08-04 04:22:24.302  INFO 24241 --- [        Timer-0] c.n.t.monitor.instance.InstanceMonitor   : Url for host: http://10.0.2.15:8081/actuator/hystrix.stream default
2022-08-04 04:22:24.313  INFO 24241 --- [           main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 7979
2022-08-04 04:22:24.387  INFO 24241 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_MICROSERVICE-MONITOR-HYSTRIX-DASHBOARD/10.0.2.15:microservice-monitor-hystrix-dashboard:7979 - registration status: 204
2022-08-04 04:22:24.393  INFO 24241 --- [           main] erviceMonitorHystrixDashboardApplication : Started MicroserviceMonitorHystrixDashboardApplication in 5.052 seconds (JVM running for 5.601)

部署效果展示

访问http://localhost:7979/hystrix