场景:项目需要一个云配置中心,用于统一管理微业务模块的配置
上服务端代码:
1.引入依赖,pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
2.配置启动类,添加注解@EnableConfigServer
@SpringBootApplication
@EnableConfigServer
public class ConfigserverApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigserverApplication.class, args);
}
}
3.设置配置文件目录
server:
port: 8888
spring:
cloud:
config:
server:
native:
search-locations: classpath:/shared
profiles:
active: native
附录项目目录
search-locations: classpath:/shared:指定为当前目录下的/shared目录
product.yml:该文件命名规则按照客户端应用程序名称,例如项目应用程序名称product-service,则命名为product-service.yml
product.yml是客户端项目的application.yml.
启动项目,访问链接http://localhost:8888/product/default/master(红色字体是客户项目应用名称)
成功会在控制带打印
2023-06-07 08:49:33.443 INFO 7644 --- [)-192.168.140.1] o.s.c.c.s.e.NativeEnvironmentRepository : Adding property source: classpath:/shared/product.yml
下一篇说说配置客户端