一 简介
Config Server可以从本地仓库读取配置文件,也可以从远处Git仓库读取。本地仓库是指
将所有的配置文件统一写在Config Server工程目录下。Config Server暴露Http API接口,Config
Client通过调用Config Server的Http API接口来读取配置文件。
二配置
1.创建子服务工程
2.修改pom文件
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>
</dependencies>
3.创建启动类
package com.fsp.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer//开启ConfigServer功能
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
4.修改application.yml
spring:
#服务名称
application:
name: fsp-config
profiles:
active:
- native #代表从本地文件读取
cloud:
config:
server:
native:
search-locations:
- classpath:/shared #配置文件路径
#服务端口
server:
port: 8762
eureka:
client:
service-url:
defaultZone: http://administrator:123456@10.0.199.19:8761/eureka/
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
5.在resources下创建shared文件夹,这个名称和application中配置的路径一致
6.在shared文件夹下创建配置文件
#服务端口
server:
port: 8082
eureka:
client:
service-url:
defaultZone: http://administrator:123456@10.0.199.19:8761/eureka/
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
7.config客户端获取测试
客户端在他的pom文件添加
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
然后修改application.yml为bootstrap.yml内容(bootstrap优先于application加载)
spring:
#服务名称
application:
name: fsp-test
profiles:
active:
- dev
cloud:
config:
uri:
- http://10.0.199.19:8762
fail-fast: true