学习SpringCloud之配置中心Config

版权声明:本文为博主czb1n(https://www.zbin.tech)原创文章,未经博主允许不得转载。 https://blog.csdn.net/czb1n/article/details/89174214

简介

  • SpringCloudConfig是一个集中性、动态的、可拓展的配置服务,并且提供多种存储配置内容的方式,为微服务架构中的其他应用提供配置。

支持存储方式:

  • Git Backend
  • File System Backend
  • Vault Backend
  • JDBC Backend
  • Redis Backend
  • CredHub Backend

配置文件的命名格式:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

application 为配置中的spring.config.name即项目名称。
profile为spring.cloud.config.profile指定的配置类型。
label为一个git中的可选字段,默认为master

以下示例均基于SpringCloud的Greenwich.SR1版本,且需要依赖到之前介绍SpringCloud相关的文章

基础依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-dependencies</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
</dependencies>

ConfigServer

开始前,需先启动EurekaServer。

增加依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

启动时,需要在主类中增加 @EnableConfigServer 注解。

@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
class ConfigServerStarter

fun main(args: Array<String>) {
    runApplication<ConfigServerStarter>(*args)
}

修改启动配置项 application.yml

server:
  port: 6607

eureka:
  instance:
    hostname: localhost
  client:
    serviceUrl:
      defaultZone: http://localhost:6600/eureka/

spring:
  application:
    name: config-server

  cloud:
    config:
      server:
        # 以git的存储方式进行配置。
        git:
          uri: https://github.com/czb1n/learn-spring-cloud-with-kotlin
          search-paths: config-server
          default-label: master

配置指定的git库路径下中新增config-server-dev.properties文件,内容为name = czb1n
启动应用访问http://localhost:6607/name/dev
页面返回:

{
	"name": "name",
	"profiles": ["dev"],
	"label": null,
	"version": "af11b65cc6308e46a556229216950472fd4a8fda",
	"state": null,
	"propertySources": []
}

ConfigClient

与Server一样,先增加对应的依赖。

扫描二维码关注公众号,回复: 5833048 查看本文章
<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-config-client</artifactId>
 </dependency>

启动类中没有什么特别。

@SpringBootApplication
@EnableDiscoveryClient
class ConfigClientStarter

fun main(args: Array<String>) {
    runApplication<ConfigClientStarter>(*args)
}

主要是配置文件中需要指定配置中心。

  • application.yml:
server:
  port: 6608

spring:
  application:
    name: config-client
  • bootstrap.yml:
eureka:
  instance:
    hostname: localhost
  client:
    serviceUrl:
      defaultZone: http://localhost:6600/eureka/

spring:
  cloud:
    config:
      discovery:
        enabled: true
        serviceId: config-server
      profile: dev

这里有一个问题需要注意,指定配置中心的配置不能在application.yml中指定,需要在bootstrap.yml中指定才能生效。
原因是bootstrap.yml会先于applicaiton.yml执行,而这里是因为要指定配置中心,必须要优先于应用的启动配置才能使得配置中心的配置生效。

The net result of this behavior is that all client applications that want to consume the Config Server need a bootstrap.yml (or an environment variable) with the server address set in spring.cloud.config.uri (it defaults to “http://localhost:8888”).

在配置中心的git库中新增一个config-client-dev.properties文件,内容为

name = czb1n
version = 1.0

我们在新建一个类,用来测试一下读取配置中心的配置。

@RestController
class DemoController {

    @Value("\${name}")
    var name: String? = null

    @Value("\${version}")
    var version: String? = null

    @RequestMapping("/hello")
    fun hello(): String {
        return "hello $name(version $version)."
    }

}

启动应用,访问http://localhost:6608/hello,页面输出hello czb1n(version 1.0).
证明配置中心的配置已生效。

其他

示例代码地址: https://github.com/czb1n/learn-spring-cloud-with-kotlin

猜你喜欢

转载自blog.csdn.net/czb1n/article/details/89174214