SpringCloud config (配置中心)
Spring Cloud Config为服务端和客户端提供了分布式系统的外部化配置支持。配置服务器为各应用的所有环境提供了一个中心化的外部配置。一般外部配置文件存在github.com上。
下面是config的代码实现:
新建配置中心模块:
配置pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
配置application.yml:
server:
port: 8888
spring:
cloud:
config:
server:
git:
#存放外部配置文件的路径
uri: https://github.com/panjiejieNB/configstu
路径https://github.com/panjiejieNB/configstu的源文件
server:
port: 8761
eureka:
instance:
hostname: localhost
prefer-ip-address: true
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
配置main方法
package cn.pjj;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
//配置中心服务注解
@EnableConfigServer
public class ConfigServer {
public static void main(String[] args) {
SpringApplication.run(ConfigServer.class, args);
}
}
启动配置中心(运行main方法)就成功了
其他模块调用外置配置:
使用bootstrap.yml替换之前application
spring:
application:
#服务名称
name: userservice
profiles:
#环境切换
active: dev
# 配置中心 的ip和端口
# 这个文件等价于 http://localhost:8888/userservice-dev.yml
cloud:
config:
uri: http://localhost:8888
运行即可!!!