zoukankan      html  css  js  c++  java
  • Hystrix + Hystrix Dashboard搭建(Spring Cloud 2.X)

    Hystrix + Hystrix Dashboard搭建(Spring Cloud 2.X)

     

    本机IP为  192.168.1.102

    一、搭建Hystrix Dashboard

    1.   新建 Maven 项目  hystrix-dashboard

    2. pom.xml

    复制代码
    <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 
            http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
    
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.java</groupId>
        <artifactId>hystrix-dashboard</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <name>${project.artifactId}</name>
    
        <!-- 配置版本常量 -->
        <properties>
            <jdk.version>1.8</jdk.version>
            <spring.cloud.version>2.0.0.RELEASE</spring.cloud.version>
        </properties>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.5.RELEASE</version>
        </parent>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
                <version>${spring.cloud.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
                <version>${spring.cloud.version}</version>
            </dependency>
    
        </dependencies>
    
        <build>
            <finalName>${project.artifactId}</finalName>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>${jdk.version}</source>
                        <target>${jdk.version}</target>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
    
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </project>
    复制代码

    3.   application.yml

    server:
      port: 9999

    4.   HystrixDashboardStarter.java

    复制代码
    package com.java.hystrix.dashboard;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
    import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
    
    @SpringBootApplication
    @EnableHystrixDashboard
    public class HystrixDashboardStarter extends SpringBootServletInitializer {
    
        public static void main(String[] args) {
            SpringApplication.run(HystrixDashboardStarter.class, args);
        }
    
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
            return builder.sources(HystrixDashboardStarter.class);
        }
    }
    复制代码

    5.  运行 HystrixDashboardStarter.java

    浏览器打开URL

    http://192.168.1.102:9999/hystrix/

    http://127.0.0.1:9999/hystrix/

    截图如下:

     Hystrix Dashboard搭建成功!

    二、搭建 Hystrix 

    1.   新建 Maven 项目  hystrix

    2.  pom.xml

    复制代码
    <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 
            http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
    
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.java</groupId>
        <artifactId>hystrix</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <name>${project.artifactId}</name>
    
        <!-- 配置版本常量 -->
        <properties>
            <jdk.version>1.8</jdk.version>
        </properties>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.5.RELEASE</version>
        </parent>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
                <version>2.0.0.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
                <version>2.0.0.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
    
            <!-- 热部署 -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>springloaded</artifactId>
                <version>1.2.8.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
            </dependency>
    
        </dependencies>
    
        <build>
            <finalName>${project.artifactId}</finalName>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>${jdk.version}</source>
                        <target>${jdk.version}</target>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
    
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
    
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <configuration>
                        <delimiters>
                            <delimit>$</delimit>
                        </delimiters>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    复制代码

    3.   application.yml

    复制代码
    server:
      port: 8888
    
    spring:
      application:
        name: hystrix
    
    eureka:
      client:
        service-url:
          defaultZone: http://192.168.1.102:8080/eureka 
      instance:
        instance-id: hystrix.java.com
        prefer-ip-address: true #访问路径可以显示IP地址    
    
    
    info:
      app.name: hystrix
      app.update: 2018-10-07
      build.groupId: $project.groupId$
      build.artifactId: $project.artifactId$
      build.version: $project.version$
    复制代码

    4.   HostController.java

    复制代码
    package com.java.hystrix.controller;
    
    import java.net.InetAddress;
    import java.net.UnknownHostException;
    import java.util.HashMap;
    import java.util.Map;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
    
    @RestController
    public class HostController {
    
        @GetMapping("/getHostMessage/{id}")
        @HystrixCommand(fallbackMethod = "getHostMessageFallback")
        public Map<String, Object> getHostMessage(@PathVariable String id) {
    
            if ("error".equals(id)) {
                throw new RuntimeException("测试异常演习!");
            }
    
            Map<String, Object> map = new HashMap<>();
            try {
                InetAddress serverHost = InetAddress.getLocalHost();
                map.put("hostname", serverHost.getHostName());
                map.put("hostAddress", serverHost.getHostAddress());
                map.put("id", id);
    
                return map;
            } catch (UnknownHostException e) {
                e.printStackTrace();
                map.put("msg", e.getMessage());
                throw new RuntimeException(e.getMessage());
            }
    
        }
    
        public Map<String, Object> getHostMessageFallback(@PathVariable String id) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", id);
            map.put("description", "异常演习Fallback!");
            return map;
    
        }
    
    }
    复制代码

    5.   ConfigBean.java

    复制代码
    package com.java.hystrix.config;
    
    import org.springframework.boot.web.servlet.ServletRegistrationBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
    
    @Configuration
    public class ConfigBean {
    
        @Bean
        public ServletRegistrationBean<HystrixMetricsStreamServlet> getServlet() {
            HystrixMetricsStreamServlet servlet = new HystrixMetricsStreamServlet();
            ServletRegistrationBean<HystrixMetricsStreamServlet> bean = new ServletRegistrationBean<>(servlet);
            bean.addUrlMappings("/hystrix.stream");
            bean.setName("HystrixMetricsStreamServlet");
            return bean;
        }
    
    }
    复制代码

    6.   HystrixStarter.java

    复制代码
    package com.java.hystrix;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
    import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    
    @EnableEurekaClient
    @EnableCircuitBreaker
    @EnableDiscoveryClient
    @SpringBootApplication
    public class HystrixStarter extends SpringBootServletInitializer {
    
        public static void main(String[] args) {
            SpringApplication.run(HystrixStarter.class, args);
        }
    
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
            return builder.sources(HystrixStarter.class);
        }
    
    }
    复制代码

    7.   运行测试

    启动  Eureka   服务注册中心,参考  https://www.cnblogs.com/jonban/p/eureka.html

    运行 HystrixStarter.java

    浏览器输入URL

    http://192.168.1.102:8888/getHostMessage/hello

    http://127.0.0.1:8888/getHostMessage/hello

    返回数据如下:

    {"hostname":"F6RK2EXYAFARPPS","hostAddress":"192.168.1.102","id":"hello"

    截图如下:

    Hystrix 搭建成功!

    三、使用 Hystrix Dashboard 监控 Hystrix 服务

    在 Hystrix Dashboard 监控页面 (http://127.0.0.1:9999/hystrix/)输入Hystrix 的监控地址

    http://192.168.1.102:8888/hystrix.stream

    截图如下:

    单击【Monitor Stream】按钮,开始监控,截图如下:

    连续多次请求Hystrix 服务的URL

    http://192.168.1.102:8888/getHostMessage/hello

    切换到仪表盘页面观察,监控开始变化。

    监控仪表盘功能正常

    搭建完毕!

    技术要点提炼

    只要在被监控项目上加入相关依赖和配置并开启注解即可

    内容如下:

    复制代码
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        <version>2.0.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
        <version>2.0.5.RELEASE</version>
    </dependency>
    复制代码
    @EnableCircuitBreaker
    @EnableDiscoveryClient
    复制代码
    package com.java.hystrix.config;
    
    import org.springframework.boot.web.servlet.ServletRegistrationBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
    
    @Configuration
    public class ConfigBean {
    
        @Bean
        public ServletRegistrationBean<HystrixMetricsStreamServlet> getServlet() {
            HystrixMetricsStreamServlet servlet = new HystrixMetricsStreamServlet();
            ServletRegistrationBean<HystrixMetricsStreamServlet> bean = new ServletRegistrationBean<>(servlet);
            bean.addUrlMappings("/hystrix.stream");
            bean.setName("HystrixMetricsStreamServlet");
            return bean;
        }
    
    }
    复制代码
  • 相关阅读:
    使用sql语句查询表结构
    plsql出现录相机后卡屏解决方法
    oracle的“ORA-01480:STR绑定值的结尾Null字符缺失”错误
    oracle创建表空间并对用户赋权
    Scrapy安装错误(error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools)
    震惊你不知道的python
    django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb'
    python3 ImportError: No module named 'ConfigParser'
    python import报错
    No migrations to apply(django不能创建数据库中的表的问题)
  • 原文地址:https://www.cnblogs.com/telwanggs/p/12606342.html
Copyright © 2011-2022 走看看