美文网首页
springcloud-config

springcloud-config

作者: 白敏鸢 | 来源:发表于2017-10-30 22:02 被阅读0次

概述:springcloud-config是一个利用git.svn等进行微服务的分布式配
  中心的东西,既然springcloud做的是分布式,那么对于不同的微服
务之间的配置读取就不能anzspringboot中的方式啦,config就是做
这个的

怎么用?

step1:构建远程仓库,这里我选择git,svn不推荐使用
国外有github,国内有码云,我这里选择国内的,
https://gitee.com/chinalijun/springcloud/
构建一个仓库config-repo
然后作为测试在里面放3个配置文件
neo-config-dev.properties
neo-config-pro.properties 
neo-config-test.properties
ok

step2:开始我们的springcloud-server吧

配置文件
application.properties
spring.application.name=config-server
server.port=7001
# git
spring.cloud.config.server.git.uri=https://gitee.com/chinalijun/springcloud/
spring.cloud.config.server.git.search-paths=config-repo
spring.cloud.config.server.git.username=***
spring.cloud.config.server.git.password=***

写好我们的app
package com.example.configserver;

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

@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

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

ok,现在测试一下http://localhost:7001/neo-config/dev,ok,可以看到
一些json字符串(自己脑补=-=)

step3:搞一个springcloudclient来测试一下
配置文件
bootstrap.properties
spring.application.name=neo-config
spring.cloud.config.label=master
spring.cloud.config.profile=dev
spring.cloud.config.uri=http://localhost:7001/
server.port=7002

搞一个controller
package com.example.configclient2;
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;
@RestController
public class TestController3 {

    @Value("${neo.hello}")
    private String version;

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    @RequestMapping("/version")
    public String name() {
        return this.version;
    }


}
最后是我们的app
package com.example.configclient2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ConfigClient2Application {

    public static void main(String[] args) {
        SpringApplication.run(ConfigClient2Application.class, args);
    }
}
ok.run一下我们可以看到已经读出来了,又是一些json,脑补ing=-=;
ps:有几个坑注意一下,
  1:bootstrap.properties中的spring.cloud.config.label=master;不是Null;虽然我们在server中测试是null;
  2:client中的取值是在我们git上面的配置文件取值。

遗留的问题:
1:config中占位符号,有点类似正则表达式
2:访问权限
3:actuator模块可以用来做health检测;
4:security:可以配合做权限
5:config的加密:对称加密:jce,非对称加密:rsa
6:高可用配置;
method1:多个client注册同一个server
method2:把client与server注册到eureka
7:使用actuator可以做动态的配置刷新

相关文章

网友评论

      本文标题:springcloud-config

      本文链接:https://www.haomeiwen.com/subject/svgmpxtx.html