社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
以及Eureka的,就不贴了;
2. 注册至服务中心 eureka.client.service-url.defaultZone=EurekaUrl
3. Application添加@EnableConfigServer
4. 添加Git仓库地址 :
spring.cloud.config.server.git.uri= Git Repository Url
spring.cloud.config.server.git.basedir=config-repo
spring.cloud.config.server.git.password=yourpassword
spring.cloud.config.server.git.username= yourusername
公有仓库无需配置,仅地址即可;
2. SSH 密钥验证配置:
spring.cloud.config.server.git.ignore-local-ssh-settings=true
spring.cloud.config.server.git.strict-host-key-checking=false
#重点,注意格式,这里仅贴几行
spring.cloud.config.server.git.private-key=-----BEGIN RSA PRIVATE KEY-----n
MIIEogIBAAKCAQEAlwBdaROcsgEaO2cxxU9/0LzixFunoNHWoBaCCu//yv26/T3O
DaNNo+zYkDCUF4nIAUdSYWO669lz7vnTg9YKURMvI61mT9Zd41GA5J0noZe+Xudj
9EqiuHj+OjfUSRycq/z7iolhACVtXCcpya1okHcTAhOC2Vx5gH8=n
-----END RSA PRIVATE KEY-----
Config服务默认仅搜索Git仓库根目录,当配置文件较多,多个项目共用一个配置仓库时,分文件夹是必然的;服务端需要额外添加配置:
spring.cloud.config.server.git.search-paths=clien1,clien2,client3
此时Config服务就会到各个文件夹下搜索对应配置文件,文件命名也需遵循规则,如:
client1-dev.properites,client1-local.properites,client3-prod.properties
同一个项目可以同时有多个环境的不同配置文件,但必须在对应名称的文件夹下;
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
#配置中心服务端在eureka的注册名称;
spring.cloud.config.discovery.service-id=server name
#启用配置服务发现
spring.cloud.config.discovery.enabled=true
#客户端在服务端搜索配置时的名称,可以与spring.application.name不一致
spring.cloud.config.name=client1
#标识,local,dev,prod等,随意
spring.cloud.config.profile=dev
#分支名称
spring.cloud.config.label=master
http://config server ip: server port/client1/dev/master
#config server 地址
spring.cloud.config.uri= server address
但是既然使用微服务了,那么当然要用Eureka咯
发送POST请求(bus自动刷新除外):
security.basic.enabled=true
security.user.name=configUserName
security.user.password=configPWD
-
3. 客户端 :添加安全包依赖
4. 客户端 :添加属性
#服务端配置的密码
spring.cloud.config.username=configUserName
spring.cloud.config.password=configPWD
刷新需要的pom依赖:
如果启动时未看见如下图日志,则需要添加依赖,否则不需要;
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>1.5.9.RELEASE</version>
</dependency>
因为其实spring-cloud-config-client包中其实已经有此依赖,只是版本不同,有时需要额外添加;
如有问题欢迎私信博主或邮件machuang0104@126.com交流
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!