1.创建git仓库,上传一个配置文件{application}-{profile}.yml
2.创建config-server项目工程,作为配置中心
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
server:
port: 9003
spring:
application:
name: config
cloud:
config:
server:
git:
uri: #仓库地址
username:
password:
search-paths:
- #仓库名字
label: master
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: always
eureka:
instance:
hostname: localhost
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@
client:
service-url:
defaultZone: http://localhost:8761/eureka/
register-with-eureka: true
fetch-registry: true
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class,args);
}
}
3.访问http://locahost:9003/master/{application}-{profile}.yml,查看上传的文件内容
4.创建config-client工程,改造之前工程就可以
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
添加bootstrap.yml或者将application.yml文件改成bootstrap.yml文件
spring:
application:
name: find
cloud:
config:
name: {application} #要获取的git仓库的文件名
profile: {profile}
label: master
uri: http://localhost:9003