一、目的
一方面是统一配置管理,这样可以更方便的管理和更改。另一方面是在配置中心更改后可以同步更新。而且有的配置文件例如数据库密码等是不能够让所有程序员都可见的。
二、实现步骤
1、在原有的maven工程上,我们再创建一个名为config-server的Maven Module
2、pom.xml文件添加jar包
<dependencies>
<!-- 配置中心服务端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- Eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
3、写启动类ConfigServerApplication
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
4、写application.yml配置文件
server:
port: 8080 #端口号
#eureka注册与发现,注册到8761端口的eureka上
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
instance:
hostname: localhost
#配置应用的名字
spring:
application:
name: config-server
#统一配置中心
cloud:
config:
uri:
server:
git:
uri: #仓库地址
username: #仓库登录账号
password: #仓库登录密码
basedir: ./git #存储在git文件夹下
5、启动服务中心eureka1
6、启动ConfigServerApplication
7、访问http://localhost:8761,可以看到服务挂上了服务中心
8、访问http://localhost:8080/xxx-a.yml
其中xxx表示在仓库中创建的文件夹的名字,例如创建的文件名为order.yml,那么访问地址为http://localhost:8080/order-a.yml。-a是因为接口规定的。也可以是-dev等。