zoukankan      html  css  js  c++  java
  • cloudConfigCentre

      应用服务除了实现系统功能,还需要连接资源和其他应用,经常有很多需要在外面配置的数据用于调整应用的行为,如切换不同的数据库,设置功能开关等。微服务的使用,就意味者要管理很多的服务实例,

    但是如果没有配置中心那么配置的更新会变得很麻烦。

      spring-cloud-config就是一个配置中心,为外部配置提供了客户端和服务端的支持。基于config服务器就可以集中管理各种环境下的各种应用的配置信息。在springconfig中客户端和服务端与spring中的Environment和ProprtyuSource概念相匹配。config服务器默认的存储实现是GIT,这能够很容易的支持配置环境的标签版本,而且有各种工具方便的管理这些配置内容。Config配置服务还支持多种仓库的实现。

    配置GIT:

      本来我是准备在我的云服务器上配置的,但是我想到了码云,所以我就在码云上开了个仓库,通过windows本地提交文件到这个仓库,因为”Please make sure you have the correct access rights and the repository exists“错误,所以记录下正确的流程。

    1. 在码云上创建一个仓库。

    2. 在本地init一个git仓库。

    3.本地clone远程仓库地址。

    4.生成pubkey,这一步什么也不用填写,一路回车即可。

     5.根据路径打开id_rsa.pub复制全部内容到码云的ssh公钥添加处,中间会有中文也没有关系,添加上即可。

     6.验证是否成功。

    7.将配置文件提交到本地然后推到远程仓库。

     

     8.结果:

     配置ConfigServer:

     1 spring:
     2   application:
     3     name: configCentre
     4   cloud:
     5     config:
     6       server:
     7         git:
     8           uri: git@gitee.*/coreConfigCentre.git
     9           searchPaths: core
    10           username: *11           
            password: *

     然后报错:

    1 ***************************
    2 APPLICATION FAILED TO START
    3 ***************************
    4 
    5 Description:
    6 
    7 Failed to bind properties under 'spring.cloud.config.server.git' to org.springframework.cloud.config.server.environment.MultipleJGitEnvironmentProperties:
    8 
    9     Reason: com.jcraft.jsch.JSchException

    之前还报错了connectionfactory是关于http连接的需要加上两个依赖:

     1      <!-- https://mvnrepository.com/artifact/org.eclipse.jgit/org.eclipse.jgit -->
     2         <dependency>
     3             <groupId>org.eclipse.jgit</groupId>
     4             <artifactId>org.eclipse.jgit</artifactId>
     5             <version>5.7.0.202003110725-r</version>
     6         </dependency>
     7         <!-- https://mvnrepository.com/artifact/com.jcraft/jsch -->
     8         <dependency>
     9             <groupId>com.jcraft</groupId>
    10             <artifactId>jsch</artifactId>
    11             <version>0.1.55</version>
    12         </dependency>
    1 2020-03-24 08:46:09.772 DEBUG 17080 --- [nio-8091-exec-1] .s.c.c.s.s.GitCredentialsProviderFactory : Constructing UsernamePasswordCredentialsProvider for URI https://gitee.com/YangShengQiangWWw/coreConfigCentre.git
    2 2020-03-24 08:46:11.018  WARN 17080 --- [nio-8091-exec-1] .c.s.e.MultipleJGitEnvironmentRepository : Error occured cloning to base directory.
    3 
    4 Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

    上面的错误很常见,是https连接无法验证证书:

      1.导出证书从https://gitee.com/YangShengQiangWWw/coreConfigCentre.git。

      2.进入C:Program FilesJavajdk1.8.0_201jrelibsecurity,并执行下面的命令:

    1           keytool -import -alias cacerts 
    2                 -keystore cacerts #注意如果是全路径包含空格会报错,需要cd到cacerts路径下,然后直接查找,否则找不到
    3                 -file H:gitee.cer #你的证书位置
    4                 -trustcacerts #要求输入密钥,密钥为changeit

    ok,如果你的用户名和密码没有问题,那么此时进入配置服务器拉去配置信息就会得到如下的内容:

    但是这样是有问题的,propertysources中应该显示居体的详细信息,但是为空,修改配置文件如下:

       再次拉取propertysources属性仍然为空。

    Spring Cloud Config changes the files of Git to REST interfaces,so when i want access http://127.0.0.1:8091/cloud/dev,i should see the infomation of propertySources'version,but i had‘t see it, why?

     1 public class JGitEnvironmentRepository extends AbstractScmEnvironmentRepository implements EnvironmentRepository, SearchPathLocator, InitializingBean {
     2     public static final String MESSAGE = "You need to configure a uri for the git repository.";
     3     private static final String FILE_URI_PREFIX = "file:";
     4     private static final String LOCAL_BRANCH_REF_PREFIX = "refs/remotes/origin/";
     5     private int timeout;
     6     private int refreshRate = 0;
     7     private long lastRefresh;
     8     private boolean cloneOnStart;
     9     private JGitEnvironmentRepository.JGitFactory gitFactory = new JGitEnvironmentRepository.JGitFactory();
    10     private String defaultLabel;
    11     private GitCredentialsProviderFactory gitCredentialsProviderFactory = new GitCredentialsProviderFactory();
    12     private TransportConfigCallback transportConfigCallback;
    13     private boolean forcePull;
    14     private boolean initialized;
    15     private boolean deleteUntrackedBranches;
    16     private boolean skipSslValidation;

      spring cloud config 支持git,svn,本地文件系统,jdbc,Valut,所以找到git的configuration class:org.springframework.cloud.config.server.environment.JGitEnvironmentRepository,上图,看看是不是有哪些配置属性有误。经检查无误,是我的理解有误,正确的访问应该是下面:

     Config支持我们使用的请求的参数规则为:

    • / { 应用名 } / { 环境名 } [ / { 分支名 } ]
    • / { 应用名 } - { 环境名 }.yml
    • / { 应用名 } - { 环境名 }.properties
    • / { 分支名 } / { 应用名 } - { 环境名 }.yml
    • / { 分支名 } / { 应用名 } - { 环境名 }.properties

     上面这句话在刚开始我没太明白,后来仔细看了下明白了,这个规则指的是你配置文件的命名规则,因为searchPaths在application.yml中已经声明了,所以这里只需要关注接口的命名规则,对这里的命名规则其实就是config应用给我们指定的接口命名规则。

       比如你的配置文件叫a-b-c.yml,那么它对应的规则就是第一个规则,应用名a,环境名b,分支名c,http://127.0.0.1:8091/dev/cloud中dev对应的是接口的命名规则,cloud是你的前缀,ok服务器搭建完成。

     配置客户端:

      新建bootstrap.yml 添加如下内容:

     1 spring:
     2   cloud:
     3     config:
     4 
     5       discovery:
     6         enabled: true
     7         service-id: configCentre #配置中心的服务类型
     8       enabled: true
     9       fail-fast: true
    10       profile: cloud
    11       name: dev

      之前我在上面的第四行加上了leable:master,然后该服务请求的路径就按照上面的接口规则在最后面加上了/master,导致请求错误,所以你的配置文件yml名字中没有lable,就不要加上leable:master。按章你定义接口规则的方式取按需在bootstrap中添加属性,你每添加一个属性,springboot都会把它们自动比照规则去拼接get路径,代码永远是死的,所以它会把路径拼的乱七八糟,所以按照你命名时规则去添加spring.cloud.config下的属性。

     

     

    好了客户端也配置好了。

    springcloudConfig原理:

       Bootstrap上下文是springcloud提供的加载外部资源的配置文件,它的优先级高于应用上下文,但是不包括bootstrap.yml中声明的属性。

      在bootstrap.yml中声明的属性优先级非常低,可以被应用上下文属性所覆盖,可以作为默认值。

      但是bootstrap引用外部的资源文件的属性值优先性极高,理论上只有命令行参数可以覆盖该属性,

      如果想让应用上下文可以覆盖bootstrap上下文需要在外部属性源中设置:spring.cloud.config.allowOverride=true。注意这个属性只能在远程属性源中设置,在本地设置无效。

           springcloud中默认的bootstrap属性源是spring cloud config server,这个东西就是配置中心。

  • 相关阅读:
    2019-8-31-C#-控制台使用-UAC-权限
    2019-8-31-C#-控制台使用-UAC-权限
    2018-8-10-WPF-程序生成类库错误
    2018-8-10-WPF-程序生成类库错误
    2018-11-8-WPF-获取下载内容长度
    2018-11-8-WPF-获取下载内容长度
    2018-11-19-win10-uwp-使用-AppCenter-自动构建
    2018-11-19-win10-uwp-使用-AppCenter-自动构建
    Java实现 LeetCode 560 和为K的子数组(某著名排序大法改编)
    Java实现 LeetCode 560 和为K的子数组(某著名排序大法改编)
  • 原文地址:https://www.cnblogs.com/YsirSun/p/12552842.html
Copyright © 2011-2022 走看看