8、Spring Cloud-配置中心 Spring Cloud Config(待补充)

8.1、Config Server 本地读取配置文件

Config Server 可以从本地仓库读取配置文件,也可以从远处 Git 仓库读取。
 
本地仓库是指将所有的配置文件统 写在 Config Server 工程目录下。
Config Sever 暴露 HttpAPI 接口, Config Client 通过调用
Config Sever 的Http API 接口来读取配置文件
 
Config Server:
新建的工程目录:

pom文件一如的依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

 
application.yml

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        native:
          search-locations: classpath:/shared
  profiles:
    active: native

server:
  port: 8769
 
spring. profiles.active=native 来配置 onfig Server 本地读取配置,读取配置
的路径为 classpath 下的 hared 目录。
 
#Spring Cloud Config也提供本地存储配置的方式。
#我们只需要设置属性spring.profiles.active=native,
#Config Server会默认从应用的src/main/resource目录下检索配置文件。
#也可以通过spring.cloud.config.server.native.searchLocations=file:F:/properties/属性来指定配置文件的位置。
#虽然Spring Cloud Config提供了这样的功能,但是为了支持更好的管理内容和版本控制的功能,还是推荐使用git的方式
  • spring.application.name:对应前配置文件中的{application}部分
  • spring.cloud.config.profile:对应前配置文件中的{profile}部分
  • spring.cloud.config.label:对应前配置文件的git分支
  • spring.cloud.config.uri:配置中心的地址

config-client-dev.yml

server:
  port: 8085

foo: foo verssion 1
主配置类:
@EnableConfigServer:开启 Config Server 的功能
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

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

  

Config Client
工程的目录:

pom文件的依赖:

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

bootstrap.yml 

spring:
  application:
    name: config-client
  cloud:
    config:
      uri: http://localhost:8769
      fail-fast : true

  profiles:
    active: dev
其配置文件bootstrap.yml 中做程序的配置
bootstrap 相对 application 有优先的执行顺序
bootstrap.yml 配置文件中指定了程序名为config-cleint
 
向URL地址为http://localhost:8769的config-server读取配置文件
如果没有读取成功,则快速失败(fail-fast)读取的是dev文件
 
bootstrap.yml配置文件中的变量 spring. application.name 和变 量sprig.profiles.active }
两者以“-”相连,构成了向Config Server读取的配置文件名,所以本实例中:读取的是config-client-dev. yml

TestController.java

@RestController
public class TestController {

    @Value("${foo}")
    String foo;

    @GetMapping("/hi")
    public  String hi(){
        return foo;
    }
}
启动config-server服务
启动config=client可以在日志中发现:向其地址读取配置文件

 

此时是在controller中读取的值

 

 8.2、ContigSe刚从远程侃仓库读取配置文件

 待补充......

原文地址:https://www.cnblogs.com/Mrchengs/p/10654761.html