参考资料: 《Spring Microservices in Action》 《Spring Cloud Alibaba 微服务原理与实战》 《B站 尚硅谷 SpringCloud 框架开发教程 周阳》
SpringCloud Config 为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置;
特点:
<!--配置中心服务--><dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId></dependency>
<!--配置中心服务-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
resources 包下的 boostrap.yaml,为配置服务器自己的配置文件;
profile
application.yml
server: port: 8888 #Spring Cloud 配置服务器将要监听的端口spring: application: name: xxx-config-server #服务的名称 profiles: active: native #用于存储配置的后端存储库(文件系统) cloud: config: server: native: search-locations: classpath:/config/ #【重要参数】配置文件的存储位置路径
server:
port: 8888 #Spring Cloud 配置服务器将要监听的端口
spring:
application:
name: xxx-config-server #服务的名称
profiles:
active: native #用于存储配置的后端存储库(文件系统)
cloud:
config:
native:
search-locations: classpath:/config/ #【重要参数】配置文件的存储位置路径
server: port: 8888 #Spring Cloud 配置服务器将要监听的端口spring: application: name: xxx-config-server #服务的名称 cloud: config: server: #使用 git 作为后端存储库 git: uri: https://gitee.com/dlhjw/config-demo #配置文件所在仓库 username: git 账号 password: git 密码 default-label: master #配置文件分支 search-paths: config #配置文件所在根目录 force-pull: true #配置文件目录
#使用 git 作为后端存储库
git:
uri: https://gitee.com/dlhjw/config-demo #配置文件所在仓库
username: git 账号
password: git 密码
default-label: master #配置文件分支
search-paths: config #配置文件所在根目录
force-pull: true #配置文件目录
@EnableConfigServer:表明该服务是个 config 配置服务器;
@SpringBootApplication:表明是一个 Spring Boot 应用;
在本地 src/main/resources 资源目录下新建一个名为 config 的包,包下的配置文件为客户端的配置文件;
这里可以新建这些配置文件:
如下图所示:
<!--配置中心--><dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId></dependency>
<!--配置中心-->
<artifactId>spring-cloud-starter-config</artifactId>
spring.application.name
spring.profiles.active
spring: application: name: xxx-client #客户端,使用服务器里的配置 profiles: active: dev #指定当前环境为开发环境 cloud: config: fail-fast: true #通过 uri 方式获取 config 配置服务器的位置 #uri: http://localhost:8888 discovery: service-id: xxx-config-server #通过服务发现的方式获取 config 配置服务器 enabled: true profile: ${spring.profiles.active} #用于定位配置信息,使用上面 spring.profiles.active 属性的值 dev label: ${spring.profiles.active} #本地存储,该参数无用---spring: profiles: deveureka: instance: prefer-ip-address: true lease-renewal-interval-in-seconds: 5 lease-expiration-duration-in-seconds: 20 metadata-map: version: v1.0 client: serviceUrl: defaultZone: http://localhost:1025/eureka registry-fetch-interval-seconds: 10---spring: profiles: prdeureka: instance: prefer-ip-address: true metadata-map: version: v1.0 client: serviceUrl: defaultZone: http://{实际环境地址}:1025/eureka
name: xxx-client #客户端,使用服务器里的配置
active: dev #指定当前环境为开发环境
fail-fast: true
#通过 uri 方式获取 config 配置服务器的位置
#uri: http://localhost:8888
discovery:
service-id: xxx-config-server #通过服务发现的方式获取 config 配置服务器
enabled: true
profile: ${spring.profiles.active} #用于定位配置信息,使用上面 spring.profiles.active 属性的值 dev
label: ${spring.profiles.active} #本地存储,该参数无用
---
profiles: dev
eureka:
instance:
prefer-ip-address: true
lease-renewal-interval-in-seconds: 5
lease-expiration-duration-in-seconds: 20
metadata-map:
version: v1.0
client:
serviceUrl:
defaultZone: http://localhost:1025/eureka
registry-fetch-interval-seconds: 10
profiles: prd
defaultZone: http://{实际环境地址}:1025/eureka
spring: application: name: xxx-client #客户端,使用服务器里的配置 profiles: active: dev #指定当前环境为 dev 开发环境 cloud: config: fail-fast: true #通过 uri 方式获取 config 配置服务器的位置 uri: http://localhost:8888 #这里也可以使用服务发现机制 profile: ${spring.profiles.active} #用于定位配置信息,使用上面 spring.profiles.active 属性的值 dev label: master #对应 git 的分支---# 下同上 3.2.1 点里的下半部分,这里省略
active: dev #指定当前环境为 dev 开发环境
uri: http://localhost:8888 #这里也可以使用服务发现机制
label: master #对应 git 的分支
# 下同上 3.2.1 点里的下半部分,这里省略
/refresh
@SpringBootApplication@RefreshScopepublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
@SpringBootApplication
@RefreshScope
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
$JAVA_HOME/jre/lib/security
export ENCRYPT_KEY=IMSYMMETRIC
encrypt
decrypt
spring.database.password:"{cipher}result"
配置 Spring Cloud Config 不要在服务器端解密属性;
spring.cloud.config.server.encrypt.enabled=false
spring-security-rsa JAR
pom.xml
<dependency> <groupld>org.springframework.security</groupld> <artifactld>spring-security-rsa</artifactld> </dependency>
<groupld>org.springframework.security</groupld>
<artifactld>spring-security-rsa</artifactld>
这样,调用配置服务器的 controller 中定义获取配置文件的接口时,返回的数据是加密的;
原文链接:http://www.cnblogs.com/dlhjw/p/15796179.html
本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728