spring cloud config基本使用

spring cloud config 是一个分布式配置中心,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,它分为服务端与客户端两个部分。其中服务端用来连接配置仓库并为客户端提供获取配置信息,加密/解密信息等访问接口;而客户端则是微服务架构中的各个微服务应用或基础设施,它们通过指定的配置中心来管理应用资源与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息,spring cloud config 默认采用git

来存储配置信息,它也提供了对其他存储方式的支持,比如SVN仓库,本地化,jdbc等backend。

上面这些属性必须配置在bootstrap.properties中,这样config-server中的配置信息才能被正确加载

简单使用

版本 spring boot 1.5.14

注册中心

配置文件

server.port=1111

eureka.instance.hostname=localhost

#由于该应用为注册中心,所有设置为false,代表不向注册中心注册自己

eureka.client.register-with-eureka=false

#由于注册中心的职责就是维护服务实例,它并不需要去检索服务,所以也设置为false

eureka.client.fetch-registry=false

eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

启动类

package springcloud.eurekaserver;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer // 注解启动一个服务注册中心

@SpringBootApplication

public class EurekaServerApplication {

public static void main(String[] args) {

SpringApplication.run(EurekaServerApplication.class, args);

}

}

配置中心服务端

配置文件

在码云 上创建 一个 spring_cloud_config_server 的仓库

在该仓库下创建spring_cloud_in_action/config-repo这样的层级目录

并在 config-repo 目录下创建 一下属性文件

shendu.properties

shendu-dev.properties

shendu-test.properties

shendu-prod.properties

在这4个配置文件中均设置一个from属性

例如 from=shendu

spring.application.name=config-server

server.port=7001

spring.cloud.config.server.git.uri=https://gitee.com/shenduedu/spring_cloud_config_server/

# 配置仓库路径下的相对搜索位置,可以配置多个

spring.cloud.config.server.git.search-paths=spring_cloud_in_action/config-repo

spring.cloud.config.server.git.username=username

spring.cloud.config.server.git.password=password

eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

启动类

package springcloud.config_server;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

import org.springframework.cloud.config.server.EnableConfigServer;

@EnableDiscoveryClient

@EnableConfigServer

@SpringBootApplication

public class ConfigServerApplication {

public static void main(String[] args) {

SpringApplication.run(ConfigServerApplication.class, args);

}

}

启动 config 服务端后,就可以通过POSTMAN等工具直接访问我们的配置内容了,访问配置信息的

url与配置文件的关系如下

/{application}/{profile}{/label}

/{application}-{profile}.yml

/{label}/{application}-{profile}.yml

/{application}-{profile}.properties

/{label}/{application}-{profile}.properties

比如

在master分支下

shendu-dev.properties

那么 shendu 就是 {application}

dev 是 {profile}

master 分支名 就是{label}

比如 http://localhost:7001/shendu/prod/master

结果

配置中心客户端

配置文件 bootstrap.properties

spring.application.name=shendu

spring.cloud.config.profile=dev

spring.cloud.config.label=master

#spring.cloud.config.uri=http://localhost:7001/

server.port=7002

eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

# 开启通过服务来访问config server的功能

spring.cloud.config.discovery.enabled=true

# 指定注册的服务名

spring.cloud.config.discovery.service-id=config-server

启动类

package springcloud.config_client;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.builder.SpringApplicationBuilder;

import org.springframework.cloud.client.SpringCloudApplication;

import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient

@SpringBootApplication

public class ConfigClientApplication {

public static void main(String[] args) {

new SpringApplicationBuilder(ConfigClientApplication.class).web(true).run(args);

}

}

controller

package springcloud.config_client;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.cloud.context.config.annotation.RefreshScope;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

@RefreshScope

@RestController

public class TestController {

@Value("${from}")

private String from;

@RequestMapping("/from")

public String from(){

return this.from;

}

}

结果:

猜你喜欢

转载自blog.csdn.net/weixin_39639119/article/details/81531659