SpringCloud项目(九)Config 配置中心
3.9 Config 配置中心
总体思路:1.部分配置项的配置文件在gitlab上维护;2.springcloud config server工程与gitlab建立关联,从gitlab获取配置信息;3.微服务在bootstrap.yml中配置config服务地址和选项,通过springcloud config server获取配置信息。
3.9.1 在Gitlab上添加配置文件
在当前项目的gitlab仓库下添加config-reposity目录,在目录下添加2个配置文件
注:config-reposity目录下如果存在application.yml,访问其他任何一个配置文件,都会将application.yml的配置项合并。这里为按模块分多个配置文件管理,组合使用。为避免干扰,暂不添加application.yml。
1.base.yml(基础配置,eureka服务地址等)
注意以下配置项中虽然没有spring的子项需要配置,仍然需要配置至少一个spring.profiles项,否则环境(开发、测试、生产等)的匹配失效,否则只有最后一个环境的(这里是test)eureka配置会生效,前面的配置都会被覆盖。
spring:
profiles:
active:
- dev
---
spring:
profiles: dev
eureka:
profiles: dev #开发环境
client: #客户端注册进eureka服务列表内
service-url:
defaultZone: http://eureka8051.com:8051/eureka/,http://eureka8052.com:8052/eureka/
---
spring:
profiles: test
eureka:
profiles: test #测试环境
client:
service-url:
defaultZone: http://eureka.klfont.com:8051/eureka/,http://eureka2.klfont.com:8052/eureka/
---
spring:
profiles: prod
eureka:
profiles: prod #生产环境
client:
service-url:
defaultZone: http://eureka.klfont.com:8051/eureka/,http://eureka2.klfont.com:8052/eureka/
2.datasource.yml(数据源相关,mybatis、mysql等)
spring:
profiles:
active:
- dev
---
spring:
profiles: dev
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包
url: jdbc:mysql://www.klfront.com:3306/klblog?useUnicode=true&characterEncoding=utf8 # 数据库名称
username: king
password: King@0415
dbcp2:
min-idle: 5 # 数据库连接池的最小维持连接数
initial-size: 5 # 初始化连接数
max-total: 5 # 最大连接数
max-wait-millis: 200 # 等待连接获取的最大超时时间
mybatis:
profiles: dev
config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径
type-aliases-package: com.klfront.klblog.entity # 所有Entity别名类所在包
mapper-locations:
- classpath:mybatis/mapper/**/*.xml # mapper映射文件
---
spring:
profiles: test
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包
url: jdbc:mysql://www.klfront.com:3306/klblog-test?useUnicode=true&characterEncoding=utf8 # 数据库名称
username: king
password: King@0415
dbcp2:
min-idle: 5 # 数据库连接池的最小维持连接数
initial-size: 5 # 初始化连接数
max-total: 5 # 最大连接数
max-wait-millis: 200 # 等待连接获取的最大超时时间
mybatis:
profiles: test
config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径
type-aliases-package: com.klfront.klblog.entity # 所有Entity别名类所在包
mapper-locations:
- classpath:mybatis/mapper/**/*.xml # mapper映射文件
3.9.2 构建config服务工程
1.创建maven module工程klblog-config,配置pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
2.配置application.yml
server:
port: 8059
spring:
application:
name: klblog-config
cloud:
config:
server:
git:
# 配置gitlab仓库的地址,注意,此处必须以.git结尾
uri: http://git.klfront.com/klblog/klblog-server.git
# gitlab仓库地址下的相对地址,可以配置多个,用,分割。
search-paths: /config-reposity username: kinglong # gitlab仓库的账号
password: xxxxxxx # gitlab仓库的密码
如果配置文件放置在Git存储库的根目录下,则无需使用searchPaths参数,本例中的配置文件在config-reposity目录中,因此使用searchPaths参数提示Config服务器搜索config-reposity子目录。
3.创建主启动类,添加@EnableConfigServer 注解
@SpringBootApplication
@EnableConfigServer
public class ConfigMainApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigMainApplication.class, args);
}
}
4.启动工程,并在浏览器输入地址访问
访问方式:
/{application}/{profile}[/{label}
/{label}/{application}-{profile}.yml
label是分支名
注:我的配置文件放在develop分支下,如果是在master分支下,则访问
http://localhost:8059/develop/base-dev.yml
或
http://localhost:8059/base/dev/develop
5.问题处理 配置文件下载到本地了,但是加载失败
报错:Failed to load property source from location
org.yaml.snakeyaml.scanner.ScannerException: mapping values are not allowed here
这个错误的是因为yaml格式不正确,谷歌定义的yaml格式太严格了:
1.每个冒号后面都必须带有空格, 2.文件需是utf-8格式,ANSI格式也无法成功加载
按照上述格式修复之后,正常了。
3.9.3 改进klbog-provider从klblog-config获取配置
1.修改pom.xml配置
添加SpringCloud Config客户端依赖
<!-- SpringCloud Config客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
2.添加bootstrap.yml文件
在src/main/resources下添加bootstrap.yml文件,内容如下:
spring:
cloud:
config:
name: base,datasource
profile: dev
label: develop
uri: http://localhost:8059
上述配置表示:通过springcloud config服务(http://localhost:8059),从gitlab的develop分支下的两个配置文件base.yml和datasource.yml加载dev环境的配置项。
3.处理报错
运行报错;
java.lang.IllegalStateException: Failed to load property source from location 'classpath:/application.yml'
提示mapper-locations: - classpath:mybatis/mapper/**/*.xml 附近有错误
mapper-locations: - classpath:mybatis/mapper/**/*.xml改为以下(- classpath:冒号后不能有空格)
mapper-locations:
- classpath:mybatis/mapper/**/*.xml # mapper映射文件
错误: 找不到或无法加载主类 com.klfront.klblog.ProviderMainApplication
project--clean
重新运行,OK。