Spring Cloud Config 分布式配置中心

38 篇文章 0 订阅
10 篇文章 0 订阅

Spring Cloud Config 分布式配置中心

作为独立微服务应用,用来连接配置仓库 为客户端提供获取配置信息、加密/解密信息等访问接口。
默认采用Git来存储配置信息。


基础搭建 config-server

  • 创建新项目config-server
  • pom.xml

    <dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Brixton.SR5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
  • 主类注释:

    @EnableConfigServer
    @SpringBootApplication
    public class ConfigServerApplication {
        public static void main(String[] args) {
            SpringApplication.run(ConfigServerApplication.class, args);
        }
    }
    
  • 配置文件:

    spring.application.name=config-server
    server.port=9001
    
    #配置Git仓库地址
    spring.cloud.config.server.git.uri=https://github.com/xiao-hang/Spring-Cloud-Test/
    #配置残酷路径下的相对搜索位置,可以多个
    spring.cloud.config.server.git.search-paths=test-config
    #分支名称 默认master
    spring.cloud.config.label=master
    #私有库需要以下两个信息
    #spring.cloud.config.server.git.username=username
    #spring.cloud.config.server.git.password=password
    #配置成本地文件系统 ,默认位置src/main/resource
    #spring.profiles.active=native
    

到这里已经完成了,服务端的配置完成

  • github上仓库的建立

    就是建立了Spring-Cloud-Test/test-config/ 这么一个库【Spring-Cloud-Test】以及下面的一个文件夹【test-config】
    在文件夹下建立了几个文件:
    git-config-dev.properties
    git-config-test.properties
    git-config.properties
    ps:内容是什么如所谓的啦 配置文件嘛╮(╯_╰)╭

  • 测试浏览配置的信息

    请求:http://localhost:8888/master/git-config.properties
    返回:from: gi-config-default-1.0   
    这个是直接返回文件信息
    
    请求:http://localhost:8888/git-config/test/master
    返回:
    {
        "name": "git-config",
        "profiles": [
            "test"
        ],
        "label": "master",
        "version": "bf5b3966eb4fda15a7337abeda45bd4b8486c3fb",
        "propertySources": [
            {
                "name": "https://github.com/xiao-hang/Spring-Cloud-Test/test-config/git-config-test.properties",
                "source": {
                    "from": "git-config-test-1.0"
                }
            },
            {
                "name": "https://github.com/xiao-hang/Spring-Cloud-Test/test-config/git-config.properties",
                "source": {
                    "from": "gi-config-default-1.0"
                }
            }
    ]
    
  • 请求url映射规则:

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

这个对应起来好麻烦,label:版本分支名称,application:文件名前缀【通常作为appname使用】,profile:文件名后缀【通常作为版本号】
(ノ`Д)ノ 反正我是看着很奇怪的感觉

  • 能这样子,说明搭建config server搭建成功了 ヾ(@^▽^@)ノ

基础搭建 config-client

  • 创建新项目config-client
  • pom.xml

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





    org.springframework.cloud
    spring-cloud-dependencies
    Brixton.SR5
    pom
    import


  • 主类注释:

    @SpringBootApplication
    public class ConfigClientApplication {

        public static void main(String[] args) {
            new SpringApplicationBuilder(ConfigClientApplication.class).web(true).run(args);
        }
    // 这两种其实都是可以的  
    //  public static void main(String[] args) {
    //      SpringApplication.run(ConfigClientApplication.class, args);
    //  }
    }
    
  • 配置文件:

    这里文件名要用bootstrap.properties,【用application.properties无法正确加载配置】

    spring.application.name=git-config
    server.port=7002
    
    spring.cloud.config.uri=http://localhost:7001/
    spring.cloud.config.label=master
    spring.cloud.config.profile=dev
    
  • RESTful接口,写一个接口测试一下

    @RefreshScope
    @RestController
    public class TestController {
        @Value("${from}")
        String from;
    
        @RequestMapping("/from")
        public String home() {
            return "from:" + this.from;
        }
    
    //    这一个方法也是可以的  跟上面主类注释掉的一同使用
    //    @Autowired
    //    private Environment env;
    //
    //    @RequestMapping("/from")
    //    public String home() {
    //        return "from:" + env.getProperty("from","undefined");
    //    }
    
    }
    

到这里已经完成了,服务端的配置完成,
调用http://localhost:7002/from 返回:from:git-config-dev-3.0
表示成功了,搞定收工咯♪(^∇^*)


但是,到这里为止,虽然可以读取远程的配置文件,不过,在远程文件被修改后,并不会动态加载的,重启生效。


健康监测

服务端spring-boot-actuator模块/health端点实现健康监测。

需要配置:

spring.cloud.config.server.health.repositories.check.name=name          #应用名
spring.cloud.config.server.health.repositories.check.label=label        #分支名
spring.cloud.config.server.health.repositories.check.profiles=profiles  #环境名
spring.cloud.config.server.health.enable=flase                          #关闭检测器

安全保护

添加spring-boot-starter-security 依赖,搞定 so easy

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

默认获取一个用户名为:user 密码为启动时的随机密码,在日志中打印出来 。【也可以配置的】

security.user.name=user
security.user.password=xiao-hang-spring-cloud-config-server-mima

客户端也需要提供对应安全信息

spring.cloud.config.username=user
spring.cloud.config.password=xiao-hang-spring-cloud-config-server-mima

但是我本地测了一下,木有用啊 ,配不配在客户端都可以访问获取 ε=(´ο`*)))唉


加密与解密

需要运行环境安装不限长度的JCE版本。
配置密钥 encrypt.key 【非对称的配置需要先生成】
配置文件参数前面加上{cipher}
【不知道这个干什么用的,正式环境需要的话在研究╮(╯_╰)╭】


高可用配置【服务模式】

在原有的基础项目上进行修改,以连接到整个架构,作为公共微服务使用。

服务端、客户端修改
  • pom.xml

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
    
  • 主类注释:

    @EnableDiscoveryClient
    
  • 配置文件:

    # 服务端
    # Eureka服务注册地址
    eureka.client.service-url.defaultZone=http://localhost:1111/eureka/
    #客户端
    spring.application.name=git-config
    server.port=7002
    # Eureka服务注册地址
    eureka.client.service-url.defaultZone=http://localhost:1111/eureka/
    #spring.cloud.config.uri=http://localhost:7001/
    spring.cloud.config.discovery.enabled=true
    spring.cloud.config.discovery.service-id=config-server
    spring.cloud.config.label=master
    spring.cloud.config.profile=dev
    

就是这个样子,完成了。so easy 的啊 ^_^
一样访问7001 client 可以去到Git上的配置信息。


失败快速响应与重试 【配置】

  • 快速失败

    spring.cloud.config.failFast=true

  • 重试【增加:spring-retry , spring-boot-starter-aop 依赖】

    <dependency>
        <groupId>org.springframework.retry</groupId>
        <artifactId>spring-retry</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
    

默认重试间隔1000毫秒,下一次为*1.1,最大2000毫秒,最大重试次数6次。
都是可以配置的:spring.cloud.config.retry.*


动态刷新配置

添加依赖即可

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

使用 post : http://localhost:7002/refresh 进行更新配置
之后再访问http://localhost:7002/from 信息已经更新了、


遇到的报错问题

报错1:

"C:\Program Files\Java\jdk1.7.0_80\bin\java
。。。。。。。。
2018-03-06 15:40:33.465 ERROR 15320 --- [nio-8888-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalStateException: Cannot clone or checkout repository] with root cause

javax.net.ssl.SSLException: Received fatal alert: protocol_version

原因:

当客户端和服务器使用的SSL协议版本不匹配时
在Java 1.8上,默认的TLS协议是v1.2。在Java 1.6和1.7上,默认是已过时的TLS1.0ssl版本不对,1.7

解决:

使用1.8+的java版本

小杭 20180314


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小_杭

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

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

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

打赏作者

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

抵扣说明:

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

余额充值