config是springcloud的组件之一,顾名思义,是配置中心的意思。
一开始我不知道config是个项目,看到一堆微服务项目的配置文件都不在本项目下还感到很奇怪,想想也是可笑。

文章目录

  • ​​pom.xml引入依赖​​
  • ​​bootstrap.yml配置文件​​
  • ​​代码中加入@EnableConfigServer​​
  • ​​从页面访问​​
  • ​​其他应用连接配置中心的配置​​
  • ​​后端配置​​
  • ​​服务端​​
  • ​​客户端​​
  • ​​报错: If you are using the git profile, you need to set a Git URI in your configuration. If you are using a native profile and have spring.cloud.config.server.bootstrap=true, you need to use a composite configuration.​​

pom.xml引入依赖

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
</parent>

<properties>
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
</properties>

<dependencies>
<!-- Eureka-Client 依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- Config-Server 依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<!-- SpringCloud 版本控制依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

bootstrap.yml配置文件

配置文件如下:

spring:
application:
name: config-server # 应用名
profiles:
active: native
cloud:
config:
server:
native:
search-locations: ${APP_HOME}/logs/config-files/ # 配置文件路径 APP_HOME 一般在启动文件中定义

server:
port: 8888

eureka:
client:
serviceUrl:
defaultZone: http://register:8889/eureka # config也是一个应用,当然也要放到注册中心了
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ipAddress}:${server.port}

security:
user:
name: root
password: 1234

代码中加入@EnableConfigServer

@SpringBootApplication
@EnableConfigServer // 激活该应用为配置文件服务器:读取远程配置文件,转换为rest接口服务
public class ConfigServerApplication {
}

从页面访问

配置中心是个单独的应用,当然可以页面访问了。

访问首页:
​​​ http://localhost:8888​

访问某个具体的配置文件:
​​​ http://localhost:8888/config-server/dev​

可用的写法为:

http://localhost:8888/config-server/dev/master
http://localhost:8888/config-server/prod
http://localhost:8888/config-server-dev.yml
http://localhost:8888/config-server-prod.yml
http://localhost:8888/master/config-server-prod.yml

其他应用连接配置中心的配置

spring:
cloud:
config:
discovery:
enabled: false # 是否读取配置中心配置,false为不读取
service-id: config-server # 配置中心的id名称,要和配置中心的application.name一直,否则找不到配置文件
profile: ${spring.profiles.active} #如果是本地开发,把这个变量名修改成dev
username: root
password: 1234
label: dev # 远程用来存放配置文件的git的分支
name: master #要拉取的分支名称
eureka:
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ipAddress}:${server.port}
client:
service-url:
defaultZone: http://localhost:8889/eureka

后端配置

主要有2种:
1、VCS(version control system版本控制系统)(git,svn等)
2、本地文件

VCS配置(这里用git):

spring:
cloud:
config:
server:
git:
uri: https://gitee.com/1234/config-server
username: root@qq.com
password: 1234

本地文件配置:

spring:
cloud:
config:
server:
native:
search-locations: /data/config-files/
# 当然yml文件中也可以使用变量传入目录
# search-locations: ${APP_HOME}/config-files/

服务端

客户端

报错: If you are using the git profile, you need to set a Git URI in your configuration. If you are using a native profile and have spring.cloud.config.server.bootstrap=true, you need to use a composite configuration.

使用git的时候不报错,使用native的时候容易报错。

1、bootstraps.xml文件一定不能少,主配置要写在这里面。
2、spring.profiles 记得改为native
3、native.search-locations 后面的路径有2种写法:

# 1、classpath写法
classpath:/config
# 2、绝对路径写法
/data/config

4、如果实在没办法,那么就用git吧,比较稳妥。