Spring中心化配置官网教程(翻译)

Centralized Configuration

这份教程会教你如何建立SpringCloudConfigServer并从上面获取配置信息。(中心化配置)

 

What youll build

你会搭建起一个配置中心,然后建立一个Client,该Client在启动时会从配置中心去获取配置,并且刷新配置时不需要重启Client。

 

 

What youll need

How to complete this guide

跟其他的guide一样,你可以一点点来,抄就完事有bug改就行了,或者你可以跳过一些你已经熟悉的部分。如此,你最终能获取到一份跑的通的代码。

如果你要从“抄就完事了”,从下面的Build with Gradle开始抄代码

如果想跳过开始的配置部分,去下面的git直接下载一份配置好了代码,然后开始接下来的教程

 

 

Stand up a Config Server

你首先需要一个Config Service在你的Spring应用中去充当一个中间人,通常情况下,这个中间人是个带版本控制的文件系统(git,svn之类的)。你可以通过Spring Cloud的@EnableConfigServer去搭建一个其他Spring应用能与之交流的配置中心(实现Server-Client模式,这里是搭建Server,Server访问文件系统,Client访问Client)。以下是一个常规的由注释搭建的配置中心的Spring Boot应用。

 

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

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

 

@EnableConfigServer

@SpringBootApplication

public class ConfigServiceApplication {

 

    public static void main(String[] args) {

        SpringApplication.run(ConfigServiceApplication.class, args);

    }

}

 

 

这个配置中心(Config Server)需要知道其配置来自于哪个文件系统(repository),你可以把它很轻松的把github或者gitlab的仓库关联上。比如在git中,你可以先git init,然后创建一个叫比如说a-bootiful-client.properties的文件,commit完了之后,你可以连接一个比如叫做a-bootiful-client的Spring-boot应用。ConfigServer就是通过这样的方式知道哪些配置是发送到client的。

当然,你配置的git地址中任何名字叫做application.properties或者application.yml的文件都会被读取,不过跟Config Server名字一致的那个文件会覆盖其中的相同字段的内容。

 

在刚刚搞的git配置文件中中新建一个键值对message=Hello world,然后commit提交。

 

在Spring的配置文件中通过spring.cloud.config.server.git.uri指定地址

 

确保server.port别冲突了。

比如:

server.port=8888

 

spring.cloud.config.server.git.uri=${HOME}/Desktop/config

 

在这个例子中,我们用到了一个git地址,你也可以创建一个新目录,然后提交比如YAML文件之类的东西,如下:

$ cd ~/Desktop/config

$ find .

./.git

...

./application.yml

 

当然你也可以创建一个远程的git仓库地址,比如在github上,当然你得把配置文件中的地址换成github上的

 

Reading Configuration from the Config Server using the Config Client

 

那现在你已经搭建好一个Config Server了,让我们试试建个新的Spring应用去读取ConfigServer上的配置,而且牛逼的是,配置更新不需要重新JVM。

把org.springframework.cloud:spring-cloud-starter-config依赖添加到ConfigServer中。Spring就会跟加载本地配置文件一样加载ConfigServer上的配置文件

 

ConfigClient的名字和以及ConfigServer地址的配置文件必须配到bootstrap.properties里面(因为其会比application.properties先加载)

 

我们也希望通过开放/refresh这个端口,这样我们就可以动态加载配置文件啦(更新配置不用重启),这个可以写在application.properties里面

 

management.endpoints.web.exposure.include=*

 

这样,这个client就可以像传统的机制那样获取ConfigServer中的参数了。

 

接下来我们试试吧!创建一个Spring MVC REST Controller来测试一下,这个Controller返回一个message,不过这个message值是从配置文件中动态获取的。

 

默认情况下,配置文件中的值只会在client启动的时候读取一次,不会反复读取。你可以强制刷新配置文件,/refresh一下就好了,client就会从ConfigServer中强制获取配置文件。

 

当然你也可以通过@RefreshScope注解,让一个Controller在被访问时会自动刷新配置文件。

 

代码如下~:

package hello;

 

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

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

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

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

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

 

@SpringBootApplication

public class ConfigClientApplication {

 

    public static void main(String[] args) {

        SpringApplication.run(ConfigClientApplication.class, args);

    }

}

 

@RefreshScope

@RestController

class MessageRestController {

 

    @Value("${message:Hello default}")

    private String message;

 

    @RequestMapping("/message")

    String getMessage() {

        return this.message;

    }

}

Test the application

测一下。

先开启Server,再开启Client,访问Client中刚写的Controller,你会看到一个Hello world。(上面代码就那么些的,配置文件里陪的Hello world

 

然后在git仓库的a-bootiful-client.properties文件中,把message的值改一下,比如改成Hello Spring~。你可以通过访问ConfigServer的/default确认一下。

 

Client中你需要通过refresh来刷新一下配置文件。Spring Boot Actuator会把/refresh端口暴露出来,当然还有其他检查的端口(看Actuator的教程啦),当然你需要在工程里引入actuator依赖。

 

浏览器里直接访问一下,或者curl一下

 

 

$ curl localhost:8080/actuator/refresh -d {} -H "Content-Type: application/json"

 

我们之前是通过设置management.endpoint.web.exposure.include=*去实现client的开启/refresh端口(从spring2.0开始Actuator的端口不是默认开放了)。如果不想这么设置的话,你依然可以通过JMX的方式去访问这个端口。

 

 

Summary

恭喜恭喜,你现在实现了中心化配置了!

__________________________________________________________

 

可以把ServerConfig理解成一个配置文件服务器,用于存储读取自git的各种配置文件。然后Client机器从Server上按照名字获取其想要的配置文件并读取,这样。

转载于:https://www.cnblogs.com/callmegaga/p/10096818.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值