1. 通过config 可以实现从github上获取配置
  2. 实现:
  3. 1.服务端从github上获取到信息
  4. 2.客户端从服务端读取获取过来的信息

GitHub上的配置文件

  1. #配置文件直接放在github的根目录下 名字随意以client-server为例
  2. spring:
  3. profiles:
  4. active: dev #选择文档
  5. --- #文档 dev
  6. server:
  7. port: 8201
  8. spring:
  9. profiles: dev #声明文档的名字
  10. application:
  11. name: springcloud-config-dev
  12. eureka:
  13. client:
  14. register-with-eureka: false
  15. service-url:
  16. defaultZone: http://eureka7001.com:7001/eureka/
  17. --- #文档 test
  18. server:
  19. port: 8202
  20. spring:
  21. profiles: test
  22. application:
  23. name: springcloud-config-test
  24. eureka:
  25. client:
  26. register-with-eureka: false
  27. service-url:
  28. defaultZone: http://eureka7001.com:7001/eureka/

基本实现

配置获取端

pom

<dependencies>
  <!--引入config服务端依赖-->
     <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
</dependencies>

配置文件

server:
  port: 3344
spring:
  application:
    name: springcloud-config
 #连接远程仓库配置
  cloud:
    config:
      server:
        git:
          uri: https://github.com/SkyGlad/springcloud.git #设置git地址
  #找的文件在Github根目录  http://localhost:3344/client-server-dev.yml访问
  #client-server-dev.yml application-选择的文档分支.yml

启动类

@SpringBootApplication
@EnableConfigServer //开启配置服务
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}
/**
启动后直接通过localhost:3344/client-server-dev.yml 查看 如果查到表示成功 文件名-选择分支.yml
*/

使用端

pom

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>

配置文件

#这里的文件名字叫做bootstrap.yml 和 application.yml的区别在于botstrap的优先级更高 反正读取的文件叫做application产生冲突
spring:
  cloud:
    config:
      uri: http://localhost:3344 #设置服务端地址
      name: server-client #需要去git上读取的资源名称 不用写yml
      profile: dev #选择配置 文档的名字
      label: master #选择的分支
#不用设置端口号 会使用获取到的配置文件的端口号

controller类中展示

@RestController
public class ConfigController {
  //只是为了展示用的测试有没有获取到配置 真正使用过程中不需要默认配置上
    @Value("${spring.application.name}") //获取配置文件中设置的内容
    private String applicationName;
    @Value("${eureka.client.service-url.defaultZone}")
    private String eurekaServer;
    @Value("${server.port}")
    private String port;

    @GetMapping("/config")
    public String getConfig(){
        System.out.println(applicationName);
        System.out.println(eurekaServer);
        System.out.println(port);
        return "成功";
    }
}

启动类

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

动态刷新

当github修改后使用方的配置不会重新自动进行更改需要在使用放进行配置

spring:
  cloud:
    config:
      uri: http://localhost:3344
      label: master
      name: server-client
      profile: dev
#暴露监控端点--新加
management:
  endpoints:
    web:
      exposure:
        include: "*"
在controller层加上@RefreshScope注解

需要向使用方 http://localhosyt:8201/actuator/refresh 发送一次Post请求 就会刷新了 不用重启