SpringCloud Config 配置中心
- Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持。使用Config Server,您可以在所有环境中管理应用程序的外部属性。客户端和服务器上的概念映射与Spring Environment和PropertySource抽象相同,因此它们与Spring应用程序非常契合,但可以与任何以任何语言运行的应用程序一起使用。随着应用程序通过从开发人员到测试和生产的部署流程,您可以管理这些环境之间的配置,并确定应用程序具有迁移时需要运行的一切。服务器存储后端的默认实现使用git,因此它轻松支持标签版本的配置环境,以及可以访问用于管理内容的各种工具。可以轻松添加替代实现,并使用Spring配置将其插入。
Config Server 服务端
-
Maven依赖
-
<!--config server 配置中心依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <!--eureka 注册中心依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- 用于服务注入验证 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
- 使用版本为:2.1.1.RELEASE
-
-
application.yml配置:
-
server.port: 5001 spring: application: name: hyq-config-server # git服务器配置:目前使用明文,可以使用加密传输 cloud: config: server: git: uri: https://gitee.com/hyman.hu/springcloud-config-server.git search-paths: config # username: 865320390@qq.com # 秘钥对称加密 # password: '{cipher}c001ec7b0da3114aa0ca7566b41a5f324c388cce6e9edd8f01c1838e5970b369' username: 865320390@qq.com # 证书非对称加密 password: '{cipher}AQCMhVjvU9crqy5KiNdSOl6kCxfgP6I2q7FQPUZLMm9qUKTCgvjdThv8y+sSnZodG9zgw+/eP7HZrs//ELg9ieC5kaKyble1fT6ot29WKrGWFK/a+lXhcW/sLx0LtnogZy/uJi5daydfBfleWvWJFrc3i5GmG+Cs+HuShI5v0RbZhcwAchwrBMHgc8X65Y3aMQE5HtNIrqhJn98ml/6QhkdZheSs8sQbgYV8NcORso1iT6nUd0oVii7eXkgKNR1Oi2tHMn9UtlmEDa0hu/e7VxJy6xUf7nHC80w2LMWVeKvdZHEuT0wQYSKgnCLTW5PP8tlf/Y8WQ39GbUw5e9OfqwuS8ETe6O349Ox2Oy1hRBSCJYRAzSzCYR/X4htYB/+/vY4=' label: master profiles: active: dev ## 安全认证 security: user: name: admin # 秘钥对称加密 # password: '{cipher}aa02cad7256610ff3650fbf52b853b411afc872b1e270936f7b5dafb656f4979' # 证书非对称加密 password: '{cipher}AQArjTpxXnnEZvSYDrZvJCbRDJsSeKtX4eyMyAo/LMIIXgbXl3uyxKiywkwAwQsmFG6PHbztT2iyW/1Sbze34COHeD/FSAhZxyzpfsf051LCNBr7UC/Gm6O+yH2/GWkXlbYEF5OyEmaZ6lvqxYfT2UHJZGipVJZL15htAAV6+PxHwzHCblAAt94vLVzP9iBqrKuHytDbKvIMTA2mtyhMSI4Wlpra/uwjy0ROr5AYI8gr9fnr7LJySsP0FTaf9/jktsDmCz2wN1Vo12q9CeCy75B4AB/wtYCOmH2fvjy3Su2eEeeHG4Xu/fXH2U09+/cv92RTuBa2fFuMmi09aBfLbRZLj5RexEgB61eMzjcnnD+pwm5CaRJjIpBkchtXqoPW2+M=' # setting up spring eureka service discovery eureka: instance: prefer-ip-address: true instance-id: ${spring.application.name}:${server.port} client: serviceUrl: # defaultZone: http://localhost:8081/eureka/,http://localhost:8082/eureka/,http://localhost:8083/eureka/ defaultZone: http://admin:admin@localhost:8081/eureka/
-
-
bootstrap.yml配置:
-
# 秘钥 只能放在 bootstrap.yml文件中配置 encrypt: key: hyq # 配置中心非对称加密 # 证书生产方法: #keytool -genkeypair -alias mytestkey -keyalg RSA \ # -dname "CN=Web Server,OU=Unit,O=Organization,L=City,S=State,C=US" \ # -keypass changeme -keystore server.jks -storepass letmein # 生成证书后,将其放到bootstrap.yml文件同级目录下即可 keyStore: location: classpath:/server.jks password: letmein alias: mytestkey secret: changeme
-
-
启动类配置
-
@SpringBootApplication @EnableEurekaClient @EnableConfigServer
-
Config Client 客户端
- Maven依赖
-
<!--web server--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--Config 配置中心依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!--健康监控,用于配置文件refresh--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--eureka 注册中心依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
-
- aplication.yml 配置文件
-
server.port: 5002 spring: application: name: hyq-config-client profiles: active: dev # setting up spring eureka service discovery eureka: instance: prefer-ip-address: true instance-id: ${spring.application.name}:${server.port} client: serviceUrl: # defaultZone: http://localhost:8081/eureka/,http://localhost:8082/eureka/,http://localhost:8083/eureka/ defaultZone: http://admin:admin@localhost:8081/eureka/ # 暴露 refresh 接口:默认暴露health,info # 刷新地址:http://localhost:5002/actuator/refresh management: endpoints: web: exposure: include: health, info, refresh
-
- bootstrap.yml 配置
-
# config 配置中心配置-->只能放在bootstrap.yml文件中才能生效 spring: cloud: config: uri: http://localhost:5001 profile: dev label: master username: admin password: admin
-