AllData数据中台:alldata-document.readthedocs.io/zh/latest/R...
注册中心模块-DataxEurekaApplication
这就是SpringCloud里面的注册中心组件,使用了eureka,在pom.xml
引入了eureka:
pom.xml
<!--服务中心-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
启动类上使用如下注解开启了eureka:
启动类
@EnableEurekaServer
基本没有什么代码,核心就是一个bootstrap.yml
文件):
这段配置是通用的,每一个模块都有;在正式的代码中,localhost都是16gslave
注册中心配置yml
eureka:
instance:
# 表示Eureka客户端(即当前服务)向Eureka服务器发送心跳的时间间隔,单位为秒。
# 这里设置为20秒,即每20秒发送一次心跳,告知Eureka服务器当前服务仍然存活。
lease-renewal-interval-in-seconds: 20
# 表示Eureka服务器在多久没有收到客户端的心跳后,会将该服务实例从注册列表中删除,单位为秒。
# 这里设置为60秒,即如果Eureka服务器在60秒内没有收到心跳,则认为该服务实例已下线。
lease-expiration-duration-in-seconds: 60
# 表示在注册到Eureka服务器时,使用IP地址而不是主机名(hostname)。这通常用于避免
# 主机名解析问题。
prefer-ip-address: true
# 指定当前服务实例的IP地址。这里设置为localhost,表示当前服务运行在本机。
# 如果是生产环境,通常需要设置为实际的外网或内网IP地址。
ip-address: localhost
client:
# 表示当前服务是否注册到Eureka服务器。这里设置为false,表示当前服务不会注册到Eureka服务。
# 通常用于单机测试或不需要注册的场景。
register-with-eureka: false
# 表示当前服务是否从Eureka服务器获取服务注册表。这里设置为false,表示当前服务不会从Eureka
# 服务器获取其他服务的注册信息。通常用于单机测试或不需要服务发现的场景。
fetch-registry: false
# 表示Eureka客户端向服务器同步实例信息的时间间隔,单位为秒。这里设置为30秒,即每30秒同步一次
# 实例信息。
instance-info-replication-interval-seconds: 30
# 指定Eureka服务器的地址。这里设置为http://localhost:8610/eureka/,表示Eureka服务器运行在
# 本机的8610端口。defaultZone是Eureka的默认区域配置,
# 可以配置多个Eureka服务器地址,用逗号分隔。
service-url:
defaultZone: http://localhost:8610/eureka/
该配置文件主要用于配置Eureka客户端的行为,包括心跳间隔、服务注册、服务发现等。当前配置是一个典型的单机测试配置:
- 服务不会注册到Eureka服务器(
register-with-eureka: false
)。 - 服务不会从Eureka服务器获取其他服务的注册信息(
fetch-registry: false
)。 - Eureka 服务器地址为本机的8610端口(
defaultZone: http://localhost:8610/eureka/
)。
如果是生产环境,通常需要调整以下配置:
- 将
register-with-eureka
和fetch-registry
设置为true
。
在config模块开启了服务发现,本模块仅仅用于启动注册中心
- 将
ip-address
设置为实际的外网或内网 IP 地址。 - 将
defaultZone
设置为实际的 Eureka 服务器地址。
配置中心模块-DataxConfigApplication
SpringCloud中的配置中心,在pom.xml
中引入starter:
这个项目使用了SpringCloud Config作为配置中心,和nacos是一个级别
pom.xml引入配置中心
<!--配置中心-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
启动类上使用如下注解开启了配置中心以及服务发现:
启动类
@EnableDiscoveryClient
@EnableConfigServer
bootstrap.yml
关注如下配置:
bootstrap.yml
spring:
profiles:
# native表示本地配置文件模式,是Spring Cloud Config Server的一种模式,表示从本地文件系统加
# 载配置文件,而不是从远程Git仓库或其他存储后端加载。
active: native
# 指定Spring Cloud Config Server在本地文件系统中查找配置文件的路径。
cloud:
config:
server:
native:
search-locations: classpath:/config/
eureka:
client:
# 尤其是要注意,config模块和后续的所有模块在此处注册到了eureka上,且启用了服务发现
register-with-eureka: true
fetch-registry: true
# 暴露监控端点,这是Spring Boot Actuator的相关设置,用于管理和监控Spring Boot应用程序
management:
endpoints:
web:
exposure:
include: '*'
# 此处配置了com.data.cloud包下所有日志都是info级别,但是项目里没有这个包,很奇怪
logging:
level:
com.data.cloud: info
关于Spring Boot Actuator,在整体的pom.xml上,项目引入了该启动类,作用是监控和管理Spring Boot应用。
以config模块为例,config模块在8611端口启动,那么默认就可以通过http://localhost:8611/actuator查看所有的endpoint。
默认会开启/health和/info端点,上面的配置项设置include为*就是开启所有的端点。
关于SpringBootActuator的知识点,详见:blog.csdn.net/m0_46312862...
在classpath/config
下,包含了大量的系统配置文件。
- 在
application-common-dev.yml
中,配置了全局的MySql、Redis和RabbitMQ的配置信息:
相当于定义了全局变量,后续各个微服务模块的配置文件使用这些全局变量
application-common-dev.yml
common:
rabbitmq:
host: localhost
username: guest
password: guest
port: 5672
redis:
host: localhost
password: 123456
port: 6379
mysql:
primary:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/studio?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8
master:
driver: com.mysql.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://localhost:3306/studio?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8
flowable:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/studio?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true
quartz:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/studio?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8
druid:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/studio?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8
dts:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://localhost:3306/studio?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8
- 在
application-dev.yml
中,包含了Spring相关配置、feign、hystrix、ribbon的配置。
可以理解为公共配置
application-dev.yml
# Spring 相关
spring:
# 如果Spring MVC找不到处理器,会抛出NoHandlerFoundException异常。这通常用于全局异常处理,以
# 便自定义404错误的响应格式或日志记录。默认不会抛出异常。
mvc:
throw-exception-if-no-handler-found: true
# 将前端传递的时间数据转换为东八区,从而解决前端时间数据少8小时的问题
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
# feign配置
feign:
# 启用hystrix,feign客户端可以利用hystrix的断路器模式来防止服务调用失败时的级联故障,并提供
# 降级机制。
hystrix:
enabled: true
# 启用okHttp作为feign客户端的底层HTTP客户端。okHttp是一个高效的HTTP客户端,
# 支持HTTP/2和连接池化,可以提高网络请求的性能。
okhttp:
enabled: true
# 禁用Apache HttpClient作为feign客户端的底层HTTP客户端。
httpclient:
enabled: false
client:
config:
default:
# feign客户端的默认连接超时时间为120秒
connectTimeout: 120000
# feign客户端的默认读取超时时间是120秒
readTimeout: 120000
compression:
# 启用feign客户端的请求压缩功能。通过压缩请求数据,可以减少网络传输的数据量,提高通信效率。
request:
enabled: true
# 启用feign客户端的响应压缩功能。通过压缩响应数据,可以减少网络传输的数据量,提高通信效率。
response:
enabled: true
# hystrix 配置
hystrix:
command:
default: #default全局有效,service id指定应用有效
execution:
timeout:
# 启用执行超时机制。当Hystrix命令的执行时间超过指定的超时时间时,会触发超时异常。
enabled: true
isolation:
# 设置隔离策略为信号量模式。信号量模式(SEMAPHORE):使用信号量来限制并发执行的线程数,而不是
# 为每个命令分配独立的线程池。这种模式适用于I/O密集型任务,因为它可以减少线程切换的开销。
# 与线程池模式(THREAD)相比,信号量模式更适合轻量级任务,因为它不会创建额外的线程。
strategy: SEMAPHORE
thread:
# 断路器超时时间,默认1000ms,这里设置为了120毫秒
timeoutInMilliseconds: 120000
dataSqlConsoleHystrix: #sql工作台(这是一个微服务模块)方法的超时时间60s
# 当命令执行失败(如超时、异常)时,会触发降级逻辑,返回一个默认值或备用逻辑,以保证系统的可用性。
fallback:
enabled: true
execution:
# 启用执行超时机制
timeout:
enabled: true
isolation:
thread:
timeoutInMilliseconds: 120000 #断路器超时时间,默认1000ms
dataApiMappingHystrix: #api调用方法的超时时间 60s(这是一个微服务模块)
fallback:
enabled: true
execution:
timeout:
enabled: true
isolation:
thread:
timeoutInMilliseconds: 120000 #断路器超时时间,默认1000ms
# 启用安全上下文共享。在Hystrix命令执行时,共享当前线程的安全上下文(如用户身份、权限等)。
# 这通常用于Spring Security等安全框架,确保在异步执行时能够正确传递用户认证信息。
shareSecurityContext: true
# 请求处理的超时时间
# 建议hystrix的超时时间为:(1 + MaxAutoRetries + MaxAutoRetriesNextServer) * ReadTimeout
ribbon:
# 指定需要热加载的服务列表。这些服务在应用启动时会被ribbon立即加载,确保它们可以被快速访问。
eager-load:
enabled: true
clients:
- service-system
- service-data-system
- service-data-metadata
- service-data-console
- service-data-market
- service-data-mapping
- service-data-integration
- service-data-visual
- service-data-masterdata
- service-data-standard
- service-data-compare
- service-quartz
- service-workflow
- service-data-dts
# 对所有操作请求都进行重试,默认false
OkToRetryOnAllOperations: false
# 负载均衡超时时间,默认值5000,这里是120秒
ReadTimeout: 120000
# ribbon请求连接的超时时间,默认值2000,这里是6秒
ConnectTimeout: 6000
# 对当前实例的重试次数,默认0
MaxAutoRetries: 0
# 对切换实例的重试次数,默认1
MaxAutoRetriesNextServer: 1
gateway-dev.yml
,包含了网关的相关配置:
gateway-dev.yml
spring:
# 激活和使用application-common-dev.yml
profiles:
include: common-dev
cloud:
gateway:
# 配置全局跨域支持,允许所有来源(allowedOrigins: "*")
# 、所有方法(allowedMethods: "*") 和所有头信息(allowedHeaders: "*") 的请求,
# 并允许携带凭证(allowCredentials: true)。
globalcors:
corsConfigurations:
'[/**]':
allowedOrigins: "*"
allowedMethods: "*"
allowedHeaders: "*"
allowCredentials: true
# 启用从服务注册中心动态创建路由的功能,并将服务ID转为小写
discovery:
locator:
enabled: true
lower-case-service-id: true
# 路由规则配置,下面只展示了一个模块
routes:
# 系统基础设置中心
- id: service-system # 路由唯一标识
uri: lb://service-system # 通过负载均衡访问名为service-system的服务
# 路由断言,定义请求匹配条件。这里通过Path=/system/**表示所有以/system/开头的请求
# 都会被路由到service-system
predicates:
- Path=/system/**
# 自定义过滤器
filters:
- SwaggerHeaderFilter
- StripPrefix=1
- name: Hystrix
args:
name: systemHystrix
fallbackUri: forward:/fallback
- 剩余配置文件都是各个微服务模块具体的配置文件,后续描述。