spring cloud微服务实践四

spring cloud的hystrix还有一个配搭的库hystrix-dashboard,它是hystrix的一款监控工具,能直观的显示hystrix响应信息,请求成功率等.但是hystrix-dashboard只能查看单机和集群的信息,如果需要将多台的信息汇总起来的话就需要使用turbine.

注:这一个系列的开发环境版本为 java1.8, spring boot2.x, spring cloud Greenwich.SR2, IDE为 Intelli IDEA

hystrix-dashboard

hystrix-dashboard只要在上一篇的hystrix的基础上稍微修改下就可以了.

添加依赖

依赖文件pom.xml需要添加一些信息.

        <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.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

需改启动类

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients(basePackages = "com.xingyys.hystrix.remote")
// 添加以下注解
@EnableHystrixDashboard
@EnableCircuitBreaker
public class HystrixApplication {

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

}

修改配置文件

spring cloud 2.x版本和1.x版本不同,需要修改配置文件

# ......
# application.properties
management.endpoints.web.exposure.include=hystrix.stream
management.endpoints.web.base-path=/

测试

重新编译后开始测试
浏览器访问http://127.0.0.1:9002/hystrix出现以下页面:

浏览器访问: http://127.0.0.1:9002/hystrix.stream出现以下信息

ping: 

data: {...}

data: {...}

同时在http://192.168.1.13:9002/hystrix页面中检测http://127.0.0.1:9002/hystrix.stream,点击monitor stream跳转页面:

hystrix-dashboard显示的各项信息含义:

到这里,单节点的监控就完成了.

注:如果一直显示Loading..., 刷新 http://127.0.0.1:9002/hello/xxx页面即可.

turbine

接下来我们来看看在多台节点中的监控工具Turbine是如何配置.

创建工程

首先我们还是先来创建一个工程应用,命名为turbine

依赖文件

修改依赖文件pom.xml

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-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-server</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>
        </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>
        </dependencies>
    </dependencyManagement>

配置文件

修改配置文件application.propertoes

spring.application.name=turbine

server.port=8081
# 配置Eureka中的serviceId列表,表明监控哪些服务

turbine.app-config=node01,node02
# 指定聚合哪些集群,多个使用”,”分割,默认为default。可使用http://.../turbine.stream?cluster={clusterConfig之一}访问

turbine.aggregator.cluster-config=default
# 1. clusterNameExpression指定集群名称,默认表达式appName;此时:turbine.aggregator.clusterConfig需要配置想要监控的应用名称;
# 2. 当clusterNameExpression: default时,turbine.aggregator.clusterConfig可以不写,因为默认就是default;
# 3. 当clusterNameExpression: metadata[‘cluster’]时,假设想要监控的应用配置了eureka.instance.metadata-map.cluster: ABC,
# 则需要配置,同时turbine.aggregator.clusterConfig: ABC
turbine.cluster-name-expression=new String("default")

eureka.client.service-url.defaultZone=http://localhost:8000/eureka

# spring cloud 2.x版本需要作的改动
management.endpoints.web.exposure.include=turbine.stream
management.endpoints.web.base-path=/

启动类

@SpringBootApplication
@EnableHystrixDashboard
// 激活对turbine的支持
@EnableTurbine
public class TurbineApplication {

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

}

测试

开始测试前,需要对hystrix应用做修改,添加两个配置文件application-node01.properitesapplication-node02.properties.

application-node01.properites

spring.application.name=node01

server.port=9003
# 其他和consumer相同,主要是hystrix的配置
feign.hystrix.enabled=true

eureka.client.service-url.defaultZone=http://localhost:8000/eureka/

management.endpoints.web.exposure.include=hystrix.stream
management.endpoints.web.base-path=/

application-node02.properites

spring.application.name=node02

server.port=9004
# 其他和consumer相同,主要是hystrix的配置
feign.hystrix.enabled=true

eureka.client.service-url.defaultZone=http://localhost:8000/eureka/

management.endpoints.web.exposure.include=hystrix.stream
management.endpoints.web.base-path=/

hystrix启动类

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients(basePackages = "com.xingyys.hystrix.remote")
@EnableHystrixDashboard
@EnableCircuitBreaker
public class HystrixApplication {
    // spring cloud 2.x需要自己指定
    @Bean(name = "HystrixMetricsStreamServlet")
    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;
    }

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

}

以此启动应用:

java -jar target/discovery-0.0.1-SNAPSHOT.jar
java -jar target/hystrix-0.0.1-SNAPSHOT.jar --spring.profiles.active=node01
java -jar target/hystrix-0.0.1-SNAPSHOT.jar --spring.profiles.active=node02
java -jar target/turbine-0.0.1-SNAPSHOT.jar

访问http://127.0.0.1:8081/turbine.stream,返回

: ping

: ping

访问http://127.0.0.1:8081/hystrix并填写表单,出现以下页面:

注:假如一直在loading,请刷新node01或node02节点的/hello/neo页面,但要保证服务的提供应用关闭.

原文地址:https://www.cnblogs.com/xingyys/p/11337047.html

时间: 2024-07-29 16:16:44

spring cloud微服务实践四的相关文章

spring cloud微服务实践七

在spring cloud 2.x以后,由于zuul一直停滞在1.x版本,所以spring官方就自己开发了一个项目 Spring Cloud Gateway.作为spring cloud微服务的网关组件. 注:这一个系列的开发环境版本为 java1.8, spring boot2.x, spring cloud Greenwich.SR2, IDE为 Intelli IDEA spring cloud gateway 入门 根据官方的简介,它是spring mvc基础之上,旨在提供一个简单有效的

spring cloud微服务实践一

最近在学习spring框架.其中spring cloud在微服务方面很火,所以在学习过程中,也做一些记录. 注:这一个系列的开发环境版本为 java1.8, spring boot2.x. IDE为 Intelli IDEA spring cloud的简介 关于spring cloud是什么,做什么的问题这里就不再详细说明了.需要的可以看 这篇文章[http://www.ityouknow.com/springcloud/2017/05/01/simple-springcloud.html] (

Spring Cloud微服务实践之路- Eureka Server 中的第一个异常

EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY'RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE

在阿里云容器服务上开发基于Docker的Spring Cloud微服务应用

本文为阿里云容器服务Spring Cloud应用开发系列文章的第一篇. 一.在阿里云容器服务上开发Spring Cloud微服务应用(本文) 二.部署Spring Cloud应用示例 三.服务发现 四.服务间通信与集成 五.服务智能路由 六.集中配置管理 七.高可用和容错 八.监控和日志 九.服务的部署和发布策略 微服务概述 单体应用通常指在一个程序中满足多个业务或技术领域的需求,不同的需求领域内化为模块.假定我们要开发一个Web应用,通常的MVC模式可以满足要求.针对不同领域有不少代码生成工具

Spring Cloud微服务架构实现+Guava缓存+redis+数据库设计+微服务原理改造房产销售

Spring Cloud微服务架构实现+Guava缓存+redis+数据库设计+微服务原理改造房产销售 一.分布式服务框架的发展 1.1 第一代服务框架 代表:Dubbo(Java).Orleans(.Net)等 特点:和语言绑定紧密 1.2 第二代服务框架 代表:Spring Cloud等 现状:适合混合式开发(例如借助Steeltoe OSS可以让ASP.Net Core与Spring Cloud集成),正值当年 1.3 第三代服务框架 代表:Service Mesh(服务网格) => 例如

Spring Cloud微服务Sentinel+Apollo限流、熔断实战

在Spring Cloud微服务体系中,由于限流熔断组件Hystrix开源版本不在维护,因此国内不少有类似需求的公司已经将眼光转向阿里开源的Sentinel框架.而以下要介绍的正是作者最近两个月的真实项目实践过程,这中间被不少网络Demo示例级别水文误导过,为了以正视听特将实践过程加以总结,希望能够帮到有类似需要的朋友! 一.Sentinel概述 在基于Spring Cloud构建的微服务体系中,服务之间的调用链路会随着系统的演进变得越来越长,这无疑会增加了整个系统的不可靠因素.在并发流量比较高

关于Spring Cloud微服务架构

微服务架构 Spring Cloud解决的第一个问题就是:服务与服务之间的解耦.很多公司在业务高速发展的时候,服务组件也会相应的不断增加.服务和服务之间有着复杂的相互调用关系,经常有服务A调用服务B,服务B调用服务C和服务D ...,随着服务化组件的不断增多,服务之间的调用关系成指数级别的增长,这样最容易导致的情况就是牵一发而动全身.经常出现由于某个服务更新而没有通知到其它服务,导致上线后惨案频发.这时候就应该进行服务治理,将服务之间的直接依赖转化为服务对服务中心的依赖.Spring Cloud

spring cloud微服务分布式云架构 - Spring Cloud简介

Spring Cloud是一系列框架的有序集合.利用Spring Boot的开发模式简化了分布式系统基础设施的开发,如服务发现.注册.配置中心.消息总线.负载均衡.断路器.数据监控等(这里只简单的列了一部分),都可以用Spring Boot的开发风格做到一键启动和部署.Spring Cloud将目前比较成熟.经得起实际考验的服务框架组合起来,通过Spring Boot风格进行再封装,屏蔽掉了复杂的配置和实现原理,最终整合出一套简单易懂.易部署和易维护的分布式系统架构平台. Spring Clou

(一)spring cloud微服务分布式云架构 - Spring Cloud简介

Spring Cloud是一系列框架的有序集合.利用Spring Boot的开发模式简化了分布式系统基础设施的开发,如服务发现.注册.配置中心.消息总线.负载均衡.断路器.数据监控等(这里只简单的列了一部分),都可以用Spring Boot的开发风格做到一键启动和部署.Spring Cloud将目前比较成熟.经得起实际考验的服务框架组合起来,通过Spring Boot风格进行再封装,屏蔽掉了复杂的配置和实现原理,最终整合出一套简单易懂.易部署和易维护的分布式系统架构平台. Spring Clou