一、Config Server
1.创建一个maven工程config_server,在项目的pom.xml添加依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2.application.yml配置:
server:
port: 8100
eureka:
client:
service-url:
defaultZone: http://localhost:8022/eureka/
instance:
prefer-ip-address: true
3.在config_server中新建配置文件bootstrap.yml,内容如下:
spring:
application:
name: config_server
cloud:
config:
server:
git:
#Git仓库地址
uri:https://github.com/qingyuancloud/qydcosbe-ms-config
username: ***** #Git仓库账户
password: ***** #Git仓库密码
security:
user:
name: admin #安全认证账号
password: 123 #安全认证密码
4.编写启动类
在启动类上添加@EnableConfigServer和@EnableEurekaClient注解,声明这是一个Config Server并将其注册到Eureka Service。
二、Config Client
1.复制项目eureka_client,将ArtifactId修改为config_client,在项目的pom.xml添加依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
2.application.yml配置:
server:
port:8011
3.将注册服务的配置放到GitHub的iaas-worker.yml文件中,在config_client中新建配置文件bootstrap.yml,内容如下:
spring:
application:
name: iaas #对应Config Server所获取的配置文件的{application}
cloud:
config:
uri: http://localhost:2007/
profile: worker #对应Config Server所获取的配置文件中的{profile}
label: master #Git仓库分支,对应配置文件的{label}
username: admin #访问Config Server的帐号
password: 123 #访问Config server的密码
注:Config Server的端点与配置文件的映射规则如下:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
例如Git仓库master分支的iaas-worker文件,可以通过http://localhost:2007/iaas/worker访问。
4.编写启动类
在启动类上添加@EnableEurekaClient注解,将其注册到Eureka Server。
5.启动以下服务
(1)启动eureka_service
(2)启动config_server
(3)启动config_client
6.测试配置服务
访问http://localhost:8761/,config_client成功注册到Eureka Server。