Spring Cloud 搭建

发布时间:2026/7/28 19:42:27
Spring Cloud  搭建 最近在学习spring cloud 记录下在整个框架搭建注意事项。版本SpringBoot : 2.0.9.RELEASESpringCloud : 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/findOrderByIdeureka :zipkin 监控hystrix 监控zipkin 监控grayLog 日志监控项目三、问题总结1. 配置eureka 一定要加上register-with-eureka: falsefetch-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: true7.zuul 默认是将注册中心获取到的全部服务接口进行发布代理详情启动控制台会打印。可以设置相关的敏感头信息不传送给后端的服务如果cookie。8 . 配置zuul调用后台服务的超时时间的话配置ribbon的超时时间及可ribbon:ReadTimeout: 5000ConnectTimeout: 50009使用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-refreshpost的接口。 也可用使用 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-composes # 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_TYPEmysql # Point the zipkin at the storage backend - MYSQL_DBshop_zipkin - MYSQL_USERroot - MYSQL_PASSroot - MYSQL_HOST192.168.43.212 - MYSQL_TCP_PORT3306 # Uncomment to enable scribe # - SCRIBE_ENABLEDtrue # Uncomment to enable self-tracing # - SELF_TRACING_ENABLEDtrue # Uncomment to enable debug logging # - JAVA_OPTS-Dlogging.level.zipkinDEBUG -Dlogging.level.zipkin2DEBUG - RABBIT_ADDRESSES192.168.0.10:5672 - RABBIT_USERguest - RABBIT_PASSWORDguest - RABBIT_QUEUEzipkin - RABBIT_VIRTUAL_HOST/ ports: # Port used for the Zipkin UI and HTTP Api - 9411:9411 # Uncomment if you set SCRIBE_ENABLEDtrue # - 9410:94102.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 ) ENGINEInnoDB DEFAULT CHARSETutf8 ROW_FORMATCOMPRESSED; -- ---------------------------- -- 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) ) ENGINEInnoDB DEFAULT CHARSETutf8 ROW_FORMATCOMPRESSED; -- ---------------------------- -- 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 ) ENGINEInnoDB DEFAULT CHARSETutf8 ROW_FORMATCOMPRESSED;3.客户端配置1.1 pom 引入rabiit mq jar dependency groupIdorg.springframework.amqp/groupId artifactIdspring-rabbit/artifactId /dependency 1.2 客户端配置rabiit mq rabbitmq: host: localhost username: guest password: guest port: 5672 virtual-host: / ###开启消息确认机制 confirms publisher-confirms: true publisher-returns: true4 .启动容器docker-compose up -d注意要关闭防火墙关闭防火墙后docker要启动下五、graylog安装1. 创建文件 docker-compose.ymlversion: 2 services: mongodb: image: mongo:3 elasticsearch: image: docker.elastic.co/elasticsearch/elasticsearch-oss:6.6.1 environment: - http.host0.0.0.0 - transport.hostlocalhost - network.host0.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_SECRETsomepasswordpepper - GRAYLOG_ROOT_PASSWORD_SHA28c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918 - GRAYLOG_HTTP_EXTERNAL_URIhttp://127.0.0.1:9000/ - GRAYLOG_ROOT_TIMEZONEAsiz/Shanghai links: - mongodb:mongo - elasticsearch depends_on: - mongodb - elasticsearch ports: - 9000:9000 - 1514:1514 - 1514:1514/udp - 12201:12201 - 12201:12201/udp2. 启动 docker-compose up -d3.进入管理页面配置http://localhost:9000admin adminsystem--- inputs -- select input 输入 udp 选择 GELF UDP --- LAUNCH NEW INPU --- 勾选global---save4. spring boot 客服端集成graylog 发送日志https://github.com/osiegmar/logback-gelf六源码地址https://github.com/lyy2002/spring-cloud-demo