乐趣区

微服务的集中化配置 | 从0开始构建SpringCloud微服务(13)

照例附上项目 github 链接。
本项目实现的是将一个简单的天气预报系统一步一步改造成一个 SpringCloud 微服务系统的过程。本章主要讲解微服务的集中化配置。

微服务为什么需要集中化配置

微服务数量多,配置多
手工管理配置繁琐

使用 Config 实现 Server 端的配置中心
集成 Config Server
添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>

@EnableConfigServer 注解

//@ServletComponentScan(basePackages=”com.demo.web.servlet”)
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class Sifoudemo02Application {

public static void main(String[] args) {
SpringApplication.run(Sifoudemo02Application.class, args);
}
}

修改配置文件
spring.application.name: msa-weather-config-server
server.port= 8088

eureka.client.serviceUrl.defaultZone: http://localhost:8761/eureka/

spring.cloud.config.server.git.uri=https://github.com/ShimmerPig/spring-cloud-config-server-test01
spring.cloud.config.server.git.searchPaths=config-repo
这里指定的是我 github 上面的一个仓库

将服务端启动后,发送请求进行访问,可以查看在云端配置中心的配置文件,当以后需要使用繁多的配置时,我们可以通过此方法对服务进行集中化的配置。

退出移动版