<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
@EnableConfigServer @SpringBootApplication public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
spring.application.name=config-server server.port=7001 #git路径,精确到具体仓库位置 spring.cloud.config.server.git.uri=https://gitee.com/dongspace/config-file/ #git uri下的相对搜索位置,能够配置多个,以","分隔 spring.cloud.config.server.git.search-paths=config-client,demo spring.cloud.config.server.git.username=*** spring.cloud.config.server.git.password=***
在config-client下新建配置文件application.properties、application-stg1.properties,并添加以下配置java
from=master #stg1中的配置 from=master-stg1
在demo文件夹下新建dongspace.properties、dongspace-stg1.properties,并添加以下配置git
from=dong-master #stg1中的配置 from=dong-master-stg1
配置信息的URL与配置文件的映射关系以下:spring
上面的URL对应{application}-{profile}.properties,其中{application}对应配置文件名,{label}对应代码分支,默认为default
以上经过浏览器访问的路径对应为:bootstrap
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
server.port=7002 spring.application.name=config-client spring.cloud.config.profile=stg1 spring.cloud.config.label=master spring.cloud.config.uri=http://localhost:7001/
@RestController public class TestConfigController { @Value("${from}") private String from; @RequestMapping("/testConfig") public Object testConfig() { return from; } }
实现多个服务对应一个仓库中的不一样目录浏览器
spring.cloud.config.server.git.uri=https://gitee.com/dongspace/config-file/ #{application}对应访问服务的应用名 spring.cloud.config.server.git.search-paths={application}
实现一个服务对应目录下的一个仓库安全
spring.cloud.config.server.git.uri=https://gitee.com/dongspace/{application}-conf
客户端应用从config-server中获取配置信息听从下面的执行流程:网络
当使用占位符配置URI时,Config Server会默认加载application为app的仓库,根据以前的配置规则,服务端的健康检测器会不断检查https://gitee.com/dongspace/a... 仓库是否能够连通,这时访问配置中心的/health端点时返回的服务状态为"DOWN",当服务化配置中心时,将影响它的可用性判断,此时有两种解决方案:1.建立名为app-config的仓库;2.改变健康监测的配置以下架构
spring.cloud.config.server.health.repositories.check.name=app2 spring.cloud.config.server.health.repositories.check.label=master spring.cloud.config.server.health.repositories.check.profiles=default
在pom.xml文件中添加eureka依赖,以加入Eureka的服务治理体系。app
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency>
在入口类上添加@EnableDiscoveryClient注解,表示这是一个Eureka客户端。ide
@SpringBootApplication @EnableConfigServer @EnableDiscoveryClient public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
在application.properties中配置eureka注册中心地址。
spring.application.name=config-server server.port=7001 eureka.client.service-url.defaultZone=http://localhost:1111/eureka/ #spring.cloud.config.server.git.uri=https://gitee.com/dongspace/{application}-conf spring.cloud.config.server.git.uri=https://gitee.com/dongspace/config-file/ #git uri下的相对搜索位置,能够配置多个 spring.cloud.config.server.git.search-paths={application} spring.cloud.config.server.git.username=416974870@qq.com spring.cloud.config.server.git.password=a1991122911 # 从新设定git仓库链接性检测的地址,需新建命名为app2的仓库 #spring.cloud.config.server.health.repositories.check.name=app2 #spring.cloud.config.server.health.repositories.check.label=master #spring.cloud.config.server.health.repositories.check.profiles=default
前两步同config-server的改造,最后修改bootstrap.properties配置文件
server.port=7002 spring.application.name=config-client eureka.client.service-url.defaultZone=http://localhost:1111/eureka/ spring.cloud.config.profile=stg1 spring.cloud.config.label=master # 开启经过服务名来访问配置中心 spring.cloud.config.discovery.enabled=true # 配置中心服务名为config-server spring.cloud.config.discovery.service-id=config-server #spring.cloud.config.uri=http://localhost:7001/
分别启动eureka-server、config-server、config-client,访问http://localhost:1111,能够看到两个应用已经成功注册:
测试config-client的/testConfig接口,结果以下:
config-client在启动时,若是访问不到config-server会报错,若是不作任务额外配置,客户端的响应较慢,在报错时已经打印出不少日志。
添加以下配置,可让客户端在启动时优先检查配置中心的链接状况:
spring.cloud.config.fail-fast=true
config-client启动没法链接配置中心时,咱们但愿客户端能重试链接几回,这样能够避免因网络抖动而致使应用没法启动的状况。
首先确保客户端的快速响应机制打开,再添加以下重试配置:
# 配置重试次数,默认为6 spring.cloud.config.retry.max-attempts=6 # 间隔乘数,默认1.1,如第一次间隔为1000ms,则第二次间隔为1100ms spring.cloud.config.retry.multiplier=1.1 # 初始重试间隔时间,默认1000ms spring.cloud.config.retry.initial-interval=1000 # 最大间隔时间,默认2000ms spring.cloud.config.retry.max-interval=2000
在Config Server中引入Spring Security便可实现配置中心的密码访问。
首先在pom.xml文件中添加依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
而后在application.properties中配置用户名密码:
security.user.name=admin security.user.password=admin
最后在配置中心客户端配置用户名和密码:
security.user.name=admin security.user.password=admin
1、初始提交码云时报push to origin/master was rejected错误
解决方案以下:
1.切换到本身项目所在的目录,右键选择GIT BASH Here,Idea中可以使用Alt+F12
2.在terminl窗口中依次输入命令:
git pull
git pull origin master
git pull origin master --allow-unrelated-histories
3.在idea中从新push本身的项目,成功!!!
1.《Spring Cloud微服务实战》