【笔记】部署配置服务器

前言

连接eureka,从注册表得到配置中心的地址,连接远程配置中心,从配置中心下载指定的配置文件

配置服务器

  • 创建项目,提取所有的配置到配置中心目录下,并注释每个模块的所有配置

  • 在每个配置添加禁止配置中心的配置信息覆盖客户端的配置

1
2
3
4
spring:
cloud:
config:
override-none: true

推送到远端Git仓库

  • 将项目添加到本地Git仓库

  • 提交仓库(command+K

  • 推送到远端(command+shift+K

  • defind remote->填写url->push

配置中心服务端

  • 创建新模块,作为配置中心服务端

添加依赖

1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>

配置

cloud.config.server.git.uri:远程地址
cloud.config.server.git.search-paths:远程地址的目录
cloud.config.server.git.username:如果私有仓库,还需要配置用户名
cloud.config.server.git.username:指定对应用户名的密码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://gitee.com/feiju12138/springcloud1
search-paths: config
server:
port: 6001
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka

添加启动注解

  • 添加@EnableConfigServer注解
1
2
3
4
5
6
7
@EnableConfigServer
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

配置中心客户端

  • 在每个业务模块中添加配置中心客户端的配置

添加依赖

  • 编辑pom.xml配置文件
1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>

配置

  • 在每个模块创建bootstrap.yml配置文件。bootstrap.yml是引导配置文件,它先于application.yml加载

cloud.connfig.discovery.service-id:配置服务器服务端的id
匹配user-service-dev.yml配置文件

cloud.connfig.name:指定在云端的配置文件前缀
cloud.connfig.profile:指定在云端的配置文件后缀

1
2
3
4
5
6
7
8
9
10
11
12
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka
spring:
cloud:
config:
discovery:
enabled: true
service-id: config-server
name: user-service
profile: dev

完成