最近在学习spring cloud ,记录下在整个框架搭建注意事项。
版本:
SpringBoot : 2.0.9.RELEASE
SpringCloud : Finchley.SR4
一 、说明:
使用SpringCloud全家桶组件:
eureka(注册中心) + config(动态配置中心) + zuul( 网关 ) + openfeign(服务通讯) + hystrix(限流熔断)+ dashboard(限流熔断界面化监控) + sleuth (调用链追踪)+ Zipkin(调用链追踪界面化监控) + logback-gelf (日志收集) + grayLog (日志统一管理) +rabbit (链路追踪信息发送使用 rabbt mq 异步化 + mysql存储 )
二 、项目模块:
order + product
这里是一个非常简单的功能 : 根据id查询订单。请求先经过 zuul-- > order server---->product server 。
测试连接:
http://localhost:9090/order/v1/findOrderById
eureka :
zipkin 监控:
hystrix 监控:
zipkin 监控:
grayLog 日志监控:
项目:
三、问题总结:
1. 配置eureka 一定要加上
register-with-eureka: false
fetch-registry: false
否则启动会报错
fetch-registry: 检索服务选项,当设置为True(默认值)时,会进行服务检索,注册中心不负责检索服务。
register-with-eureka: 服务注册中心也会将自己作为客户端来尝试注册自己,为true(默认)时自动生效
2.配置 hystrix-dashboard 要暴露下流传传输路径:
management:
endpoints:
web:
exposure:
include: "*"
3.要配置prefer-ip-address 否则发布项目到doker后通讯的ip获取错误
eureka:
instance:
prefer-ip-address: true
7.zuul 默认是将注册中心获取到的全部服务接口进行发布代理,详情启动控制台会打印。
可以设置相关的敏感头信息不传送给后端的服务,如果cookie。
8 . 配置zuul调用后台服务的超时时间的话,配置ribbon的超时时间及可
ribbon:
ReadTimeout: 5000
ConnectTimeout: 5000
9,使用sleuth+ zipkin时,整个调用链(从gateway开始)服务器都要配置下starter-zipkin相关配置信息,这样在zipkin控制台才能看到整个调用链的服务名称。
最好使用mq异步传输日志信息。zipkin服务端建议用docker安装。zipkin的服务器时间要和服务的时间一样。
10. config 配置中心最好配置下baseUri 指定将配置文件保存到本机的位置。
因为生产环境有文件权限问题。当使用配置中心时, 要将客户端服务的application.yml改成 bootstrap.yml,使配置文件优先加载后再去加载其他的。
每次重启config服务都会去git上拉取最新的配置到本地。
如果使用自动更新配置,需要将config服务进行 bus-refresh 方法暴露
management:
endpoints:
web:
exposure:
include: "*"
然后调用下这个/actuator/bus-refresh(post)的接口。 也可用使用 webHooks自动调用
config-client端要将被自动刷新的类上增加@RefreshScope 注解。
11.config-client 中的bootstrap.yml配置文件不需要指定配置文件名,因为会自动根据 appliction.name 的value 拼接 cloud.profile的value 去取对应的配置文件
config-client bootstrap配置文件中一定要配置eureka,因为是先去eureka获取config服务的信息,再去config中拉取配置启动项目的。
四、zipkin server docker 安装
1.创建文件 docker-compose.yml
# This file uses the version 2 docker-compose file format, described here:
# https://docs.docker.com/compose/compose-file/#version-2
#
# This runs the zipkin and zipkin-mysql containers, using docker-compose's
# default networking to wire the containers together.
#
# Note that this file is meant for learning Zipkin, not production deployments.
version: '2'
services:
zipkin:
image: openzipkin/zipkin
container_name: zipkin
# Environment settings are defined here https://github.com/openzipkin/zipkin/tree/1.19.0/zipkin-server#environment-variables
environment:
- STORAGE_TYPE=mysql
# Point the zipkin at the storage backend
- MYSQL_DB=shop_zipkin
- MYSQL_USER=root
- MYSQL_PASS=root
- MYSQL_HOST=192.168.43.212
- MYSQL_TCP_PORT=3306
# Uncomment to enable scribe
# - SCRIBE_ENABLED=true
# Uncomment to enable self-tracing
# - SELF_TRACING_ENABLED=true
# Uncomment to enable debug logging
# - JAVA_OPTS=-Dlogging.level.zipkin=DEBUG -Dlogging.level.zipkin2=DEBUG
- RABBIT_ADDRESSES=192.168.0.10:5672
- RABBIT_USER=guest
- RABBIT_PASSWORD=guest
- RABBIT_QUEUE=zipkin
- RABBIT_VIRTUAL_HOST=/
ports:
# Port used for the Zipkin UI and HTTP Api
- 9411:9411
# Uncomment if you set SCRIBE_ENABLED=true
# - 9410:9410
2.msql 创建zipkin 数据库和表
DROP TABLE IF EXISTS `zipkin_annotations`;
CREATE TABLE `zipkin_annotations` (
`trace_id_high` bigint(20) NOT NULL DEFAULT '0' COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit',
`trace_id` bigint(20) NOT NULL COMMENT 'coincides with zipkin_spans.trace_id',
`span_id` bigint(20) NOT NULL COMMENT 'coincides with zipkin_spans.id',
`a_key` varchar(255) NOT NULL COMMENT 'BinaryAnnotation.key or Annotation.value if type == -1',
`a_value` blob COMMENT 'BinaryAnnotation.value(), which must be smaller than 64KB',
`a_type` int(11) NOT NULL COMMENT 'BinaryAnnotation.type() or -1 if Annotation',
`a_timestamp` bigint(20) DEFAULT NULL COMMENT 'Used to implement TTL; Annotation.timestamp or zipkin_spans.timestamp',
`endpoint_ipv4` int(11) DEFAULT NULL COMMENT 'Null when Binary/Annotation.endpoint is null',
`endpoint_ipv6` binary(16) DEFAULT NULL COMMENT 'Null when Binary/Annotation.endpoint is null, or no IPv6 address',
`endpoint_port` smallint(6) DEFAULT NULL COMMENT 'Null when Binary/Annotation.endpoint is null',
`endpoint_service_name` varchar(255) DEFAULT NULL COMMENT 'Null when Binary/Annotation.endpoint is null',
UNIQUE KEY `trace_id_high` (`trace_id_high`,`trace_id`,`span_id`,`a_key`,`a_timestamp`) COMMENT 'Ignore insert on duplicate',
UNIQUE KEY `trace_id_high_4` (`trace_id_high`,`trace_id`,`span_id`,`a_key`,`a_timestamp`) COMMENT 'Ignore insert on duplicate',
KEY `trace_id_high_2` (`trace_id_high`,`trace_id`,`span_id`) COMMENT 'for joining with zipkin_spans',
KEY `trace_id_high_3` (`trace_id_high`,`trace_id`) COMMENT 'for getTraces/ByIds',
KEY `endpoint_service_name` (`endpoint_service_name`) COMMENT 'for getTraces and getServiceNames',
KEY `a_type` (`a_type`) COMMENT 'for getTraces and autocomplete values',
KEY `a_key` (`a_key`) COMMENT 'for getTraces and autocomplete values',
KEY `trace_id` (`trace_id`,`span_id`,`a_key`) COMMENT 'for dependencies job',
KEY `trace_id_high_5` (`trace_id_high`,`trace_id`,`span_id`) COMMENT 'for joining with zipkin_spans',
KEY `trace_id_high_6` (`trace_id_high`,`trace_id`) COMMENT 'for getTraces/ByIds',
KEY `endpoint_service_name_2` (`endpoint_service_name`) COMMENT 'for getTraces and getServiceNames',
KEY `a_type_2` (`a_type`) COMMENT 'for getTraces and autocomplete values',
KEY `a_key_2` (`a_key`) COMMENT 'for getTraces and autocomplete values',
KEY `trace_id_2` (`trace_id`,`span_id`,`a_key`) COMMENT 'for dependencies job'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPRESSED;
-- ----------------------------
-- Table structure for zipkin_dependencies
-- ----------------------------
DROP TABLE IF EXISTS `zipkin_dependencies`;
CREATE TABLE `zipkin_dependencies` (
`day` date NOT NULL,
`parent` varchar(255) NOT NULL,
`child` varchar(255) NOT NULL,
`call_count` bigint(20) DEFAULT NULL,
`error_count` bigint(20) DEFAULT NULL,
PRIMARY KEY (`day`,`parent`,`child`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPRESSED;
-- ----------------------------
-- Table structure for zipkin_spans
-- ----------------------------
DROP TABLE IF EXISTS `zipkin_spans`;
CREATE TABLE `zipkin_spans` (
`trace_id_high` bigint(20) NOT NULL DEFAULT '0' COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit',
`trace_id` bigint(20) NOT NULL,
`id` bigint(20) NOT NULL,
`name` varchar(255) NOT NULL,
`remote_service_name` varchar(255) DEFAULT NULL,
`parent_id` bigint(20) DEFAULT NULL,
`debug` bit(1) DEFAULT NULL,
`start_ts` bigint(20) DEFAULT NULL COMMENT 'Span.timestamp(): epoch micros used for endTs query and to implement TTL',
`duration` bigint(20) DEFAULT NULL COMMENT 'Span.duration(): micros used for minDuration and maxDuration query',
PRIMARY KEY (`trace_id_high`,`trace_id`,`id`),
KEY `trace_id_high` (`trace_id_high`,`trace_id`) COMMENT 'for getTracesByIds',
KEY `name` (`name`) COMMENT 'for getTraces and getSpanNames',
KEY `remote_service_name` (`remote_service_name`) COMMENT 'for getTraces and getRemoteServiceNames',
KEY `start_ts` (`start_ts`) COMMENT 'for getTraces ordering and range',
KEY `trace_id_high_2` (`trace_id_high`,`trace_id`) COMMENT 'for getTracesByIds',
KEY `name_2` (`name`) COMMENT 'for getTraces and getSpanNames',
KEY `remote_service_name_2` (`remote_service_name`) COMMENT 'for getTraces and getRemoteServiceNames',
KEY `start_ts_2` (`start_ts`) COMMENT 'for getTraces ordering and range'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPRESSED;
3.客户端配置
1.1 pom 引入rabiit mq jar
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
</dependency>
1.2 客户端配置rabiit mq
rabbitmq:
host: localhost
username: guest
password: guest
port: 5672
virtual-host: /
###开启消息确认机制 confirms
publisher-confirms: true
publisher-returns: true
4 .启动容器:
docker-compose up -d
注意,要关闭防火墙,关闭防火墙后docker要启动下
五、graylog安装:
1. 创建文件 : docker-compose.yml
version: '2'
services:
mongodb:
image: mongo:3
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch-oss:6.6.1
environment:
- http.host=0.0.0.0
- transport.host=localhost
- network.host=0.0.0.0
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
mem_limit: 1g
graylog:
image: graylog/graylog:3.0
environment:
- GRAYLOG_PASSWORD_SECRET=somepasswordpepper
- GRAYLOG_ROOT_PASSWORD_SHA2=8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918
- GRAYLOG_HTTP_EXTERNAL_URI=http://127.0.0.1:9000/
- GRAYLOG_ROOT_TIMEZONE=Asiz/Shanghai
links:
- mongodb:mongo
- elasticsearch
depends_on:
- mongodb
- elasticsearch
ports:
- 9000:9000
- 1514:1514
- 1514:1514/udp
- 12201:12201
- 12201:12201/udp
2. 启动: docker-compose up -d
3.进入管理页面配置:
admin admin
system---> inputs --> select input 输入 udp 选择 GELF UDP ---> LAUNCH NEW INPU --- 勾选global--->save
4. spring boot 客服端集成graylog 发送日志
https://github.com/osiegmar/logback-gelf
六:源码地址:
https://github.com/lyy2002/spring-cloud-demo