SpringCloud(四) 服务跟踪分析中心

SpringCloud(四) 服务跟踪分析中心

介绍
除了可以对微服务运行情况进行监控之外,我们也可以对微服务的调用线程和运行途径进行跟踪和分析,利用springcloud 的sleuth组件的日志收集功能,并结合Zipken Server一起使用 就可以很简单的建立一个服务跟踪分析中心
代码
创建服务跟踪分析中心
在pom.xml中增加如下依赖配置

    <dependencies>
        <dependency>
        <groupId>io.zipkin.java</groupId>
        <artifactId>zipkin-server</artifactId>
    </dependency>
    <dependency>
        <groupId>io.zipkin.java</groupId>
        <artifactId>zipkin-autoconfigure-ui</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>

    <dependency>
        <groupId>com.netflix.hystrix</groupId>
        <artifactId>hystrix-core</artifactId>
    </dependency>
    <dependency>
        <groupId>com.netflix.hystrix</groupId>
        <artifactId>hystrix-metrics-event-stream</artifactId>
    </dependency>
    <dependency>
        <groupId>com.netflix.hystrix</groupId>
        <artifactId>hystrix-javanica</artifactId>
    </dependency>

    <!--mysql-->
    <dependency>
        <groupId>io.zipkin.java</groupId>
        <artifactId>zipkin-autoconfigure-storage-mysql</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid-spring-boot-starter</artifactId>
        <version>1.1.2</version>
    </dependency>
</dependencies>

这个依赖配置引用如下一些组件
Zipkin的服务器和UI组件
springcloud 工具套件中的config Eureka 和Hystrix等几个组件
mysql数据库和Druid数据库的组件

创建一个启动程序
@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
@EnableZipkinServer
public class ZipkinServerApplication {

public static void main(String[] args) throws Exception {
	new SpringApplicationBuilder(ZipkinServerApplication.class).web(true).run(args);
}

}
在前面注解中 加入注册管理中心客户端和使用断路器的功能 并且将这个设定一个Zipkin服务

在application.yml配置文件下面
server:
port: 9987
spring:
datasource:
schema: classpath:/mysql_init.sql
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/myzipkin?characterEncoding=utf8&useSSL=false
username: myzipkin
password: myzipkin
initialize: true
continueOnError: true
# 初始化大小,最小,最大
initialSize: 5
minIdle: 5
maxActive: 20
# 配置获取连接等待超时的时间
maxWait: 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
timeBetweenEvictionRunsMillis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
# 打开PSCache,并且指定每个连接上PSCache的大小
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
# 配置监控统计拦截的filters,去掉后监控界面sql将无法统计,'wall’用于防火墙
filters: stat,wall,log4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

#不对本身跟踪
sleuth.enabled: false

zipkin:
storage:
type: mysql

eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/

#ribbon config
ribbon.ConnectTimeout: 5000
ribbon.ReadTimeout: 10000
在加一个bootstrap.yml配置文件
spring:
application:
name: zipkin
cloud:
config:
uri: http://localhost:8888

rabbitmq:
addresses: amqp://47.98.133.78:5672
username: guest
password: guest

encrypt:
failOnError: false
启动项目 下面截图都是个个服务之间调用的依赖
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

扫描二维码关注公众号,回复: 3270025 查看本文章

猜你喜欢

转载自blog.csdn.net/luzhuhong1/article/details/82800061