微服务学习笔记九 Spring Cloud Config本地配置中心

Spring Cloud Config,通过服务端可以为多个客户端提供配置服务。
Spring Cloud Config可以将配置文件存储在本地,也可以将存储文件存储到远程
Git仓库。
创建Config Serve,通过它管理所有的配置文件。

本地文件系统
nativeconfigserver -> application 声明这是存放本地配置文件系统的模块,
具体的存放文件为:nativeconfigserver->configclient-dev.yml。
nativeconfigclient->bootstrap.yml获取configclient-dev.yml中的配置信息
在这里插入图片描述
在这里插入图片描述
创建maven工程,pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
        <version>2.0.2.RELEASE</version>
    </dependency>
</dependencies>

创建application.yml

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

注解说明:
profiles.active:表示的是文件的获取方式
cloud.config.server.native.search-location:本地配置文件存放的路径

resources路径下创建shared文件夹,并在此路径下创建configclient-dev.yml。

server:
  port: 8070
foo: foo version 1

创建启动类

package com.shuang;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

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

注解说明:
@EnableConfigServer:声明配置中心
创建客户端,来读取本地配置中心的配置文件
创建maven工程,pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
        <version>2.0.2.RELEASE</version>
    </dependency>
</dependencies>

创建 bootstrap.yml,配置读取本地配置中心的相关信息。

spring:
  application:
    name: configclient
  profiles:
    active: dev
  cloud:
    config:
      uri: http://localhost:8762
      fail-fast: true

注解说明;
cloud.config.uri:本地Config Server 的访问路径
cloud.config.fail-fast:设置客户端的优先判断Config Server获取是否正常。
通过 spring.application.name 结合 spring.profiles.active拼接目标配置
文件名,configclient-dev.yml,去Config Server 中查找该文件

创建客户端的启动类:

package com.shuang;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

Handler

package com.shuang.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping ("/native")
public class NativeConfigHandler {

    @Value("${server.port}")
    private String port;

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

    @GetMapping("/index")
    public String index(){
        return this.port+"-"+this.foo;
    }
}

依次启动:
在这里插入图片描述
在这里插入图片描述
configclient通过读取configserver的文件,得知端口号为:8070

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

希境

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值