ConfigServer
比如说有N个微服务的application.yml是相同的,我们要是想修改application.yml需要修改N个这时候我们就需要ConfigServer来帮我们解决这个问题,在配置中心修改一次众多个微服务生效
1.ConfigServer是什么
SpringCloud Config为服务器架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置
2.已码云为例,GitHub都是一样的
首先在码云创建个仓库复制https链接。
2.1在码云上传application.yml
spring:
profiles:
active: dev
---
spring:
profiles: prod
application:
name: config-single-client
cloud:
config:
uri: http://localhost:8848
label: master
profile: prod
---
spring:
profiles: test
application:
name: config-single-client
cloud:
config:
uri: http://localhost:6666
label: master
profile: test
3.Config配置中心搭建
Maven依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>2.2.4.RELEASE</version>
</dependency>
SpringBoot主启动
package com.dazuizui.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@EnableConfigServer
@SpringBootApplication
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
3.1公共仓库访问
application.yml
server:
port: 3344
spring:
application:
name: config-single-server # 应用名称
cloud:
config:
server:
git:
uri: https://gitee.com/dazuizui/dazuiblog---config.git #码云https/http链接
# 如果您没有控制中心可以删除以下的代码
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用 负载均衡
fetch-registry: true
service-url:
#注册中心地址
defaultZone: http://127.0.0.1:7001/eureka/
instance:
prefer-ip-address: true
3.2访问私有仓库
application.yml与访问共有仓库一样
application.properties
spring.cloud.config.server.git.username=您的账号
spring.cloud.config.server.git.password=您的密码
出现的问题,我也不知道为什么写在yml就不生效,写咋子properties就生效
4.通过配置中心访问码云的配置文件
比如我们要访问test环境url: http://127.0.0.1:3344/master/application-test.yml
注意application-{环境的昵称}.yml,比如我们访问test环境 就是applicable-test.yml
5.实战使用案例
上传码云的配置文件
spring:
profiles:
active:
---
server:
port: 8001
spring:
profiles: dev
application:
name: User
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: 您的数据库url
username: 您的数据库账号
password: 您的数据库密码
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用 负载均衡
fetch-registry: true
service-url:
#注册中心地址
defaultZone: http://127.0.0.1:7001/eureka/
instance:
prefer-ip-address: true
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
---
server:
port: 8001
spring:
profiles: test
application:
name: User
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: 您的数据库url
username: 您的数据库账号
password: 您的数据库密码
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用 负载均衡
fetch-registry: true
service-url:
#注册中心地址
defaultZone: http://127.0.0.1:7001/eureka/
instance:
prefer-ip-address: true
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
所需要的Maven依赖
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-config-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
如果其他微服务使用使用Config Server加载配置文件需要建立个bootstrap.yml
bootstrap.yml代码
spring:
cloud:
config:
name: user-config #配置文件名字 比如全昵称叫做user-config.yml 写user-config就可以
lable: master #分支
profile: dev #什么环境版本
uri: http://127.0.0.1:3344 #配置中心ip+端口