ConfigServer作为微服务向Eureka注册
配置中心使用注解@EnableConfigServer
,并使用@EnableDiscoveryClient
使其作为一个微服务
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigserverApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigserverApplication.class, args);
}
}
ConfigServer微服务从git服务器同步配置文件可以采取文件名命名规则方式也可以采取分支(labe)方式
文件名规则方式,微服务默认会缓存一份config.yml后再缓存-后缀的配置文件
ConfigServer的配置,gitlab配置uri后缀加上.git
eureka:
client:
service-url:
defaultZone: http://localhost:18761/eureka
instance:
hostname: localhost
spring:
application:
name: configserver
cloud:
config:
server:
git:
uri: 路径.git
username: git用户名
password: git密码
server:
port: 17080
访问微服务配置文件的方式可通过改后缀微服务会自动整理好格式
.yml
.json
.properties
使用配置中心
先引入依赖
org.springframework.cloud
spring-cloud-config-client
将application.yml改为bootstrap.yml,配置项增加spring.cloud.config配置项,配置配置中心的微服务
eureka:
client:
service-url:
defaultZone: http://localhost:18761/eureka
spring:
application:
name: configtest # 与配置文件名一致
cloud:
config:
discovery:
enabled: true
service-id: CONFIGSERVER # 配置中心的微服务id
profile: test # 对应环境的配置文件