配置中心-基础
Server
将 配置 服务 注册到Eureka 即可
配置文件
server:
  port: 8888
spring:
  application:
    name: ConfigServer
  cloud:
    config:
      server:
        git:
          # 配置git仓库地址
          uri: https://gitee.com/_LK/test_work.git
          # 访问git仓库的用户名
#          username:
          # 访问git仓库的用户密码 如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写
#          password:
          #支持带{application}和{profile}({label}如果需要)占位符的搜索路径
          search-paths: '{application}'
      # 配置仓库的分支,可不配
      label: master
eureka:
  instance:
    hostname: localhost
    prefer-ip-address: true
  client:
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:8761/eureka/
配置 git 仓库地址,添加 eureka 相关配置
依赖
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
主类
添加 @EnableEurekaClient @EnableConfigServer 注解
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}
Client
依赖
添加 spring-cloud-starter-netflix-eureka-client
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
bootstrap.yml
建立bootstrap.yml 配置 configServer 微服务名称 和 eureka 相关配置
spring:
  application:
    name: configclient
  cloud:
    config:
      discovery:
        enabled: true
        service-id: CONFIGSERVER
      label: master
      profile: dev
server:
  port: 10001
eureka:
  instance:
    hostname: localhost
    prefer-ip-address: true
  client:
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:8761/eureka/
配置文件
加上@RefreshScope注解,git.value 会从配置中心读取值
加上@EnableEurekaClient 注册到 eureka
@SpringBootApplication
@RefreshScope
@RestController
@EnableEurekaClient
public class ConfigClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }
    @Value("${git.value}")
    private String gitValue;
    @RequestMapping("/")
    public String git() {
        return gitValue;
    }
}
自动刷新
在 client 添加 spring-boot-starter-actuator 依赖
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
暴露管理
management:
  endpoints:
    web:
      exposure:
        include: '*'
刷新地址 POST http://127.0.0.1:10001/actuator/refresh
调用URL 会自动刷新配置
