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可以在日志中发现:向其地址读取配置文件

 

访问地址: http://localhost:8085/hi
此时是在controller中读取的值

 

 

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

 待补充......

 

转载于:https://www.cnblogs.com/Mrchengs/p/10654761.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值