springcloud-config服务器,同样的配置在linux情况下不见效 ...

打印 上一主题 下一主题

主题 510|帖子 510|积分 1530

原来在windows下能夺取的获取远程配置但是摆设到linux上死活都没有内容,然后开始了远程调试,这里顺带讲授下获取配置文件假如利用的是Git源,config service是如何相应接口并返回配置信息的。先说问题,我的服务名原来是abc-abc-abc这种,因为configService 在获取配置的时候答应利用 -来指定profiles,导致这种服务名会被认为 abc 是appname abc-abc是profiles。而精确的应该是abc-abc 是appname,abc是profiles。所有反面我把服务名改成了驼峰定名即 abcAbc 如许在获取的时候就可以识别。并且在git仓库中把配置文件的名字写成了abcAbc.properties,而远程仓库名在新建的时候仓库地址会默认给全部转换为小写

一开始我还没太在意这个事情,我是用的策略是一个微服务一个仓库的方式举行管理
  1. spring.cloud.config.server.git.uri=http://your_git_space/your_org_name/{application}-config.git
复制代码
但是由于git Url是小写,利用 http://your_git_space/your_org_name/abcAbc-config.git 无法访问到仓库,所以我在config client中配置了name
  1. spring:
  2.   cloud:
  3.     config:
  4.       enabled: true
  5.       discovery:
  6.         enabled: true #是否启动config server服务发现
  7.         service-id: CONFIG-SERVICE #配置服务名称
  8.       profile: ${spring.profiles.active}
  9.       name: abcabc#获取配置是使用的application name,默认是spring.application.name
  10.       label: master
复制代码
config client 在拉取config的时候会以如下的方式举行访问
http://CONFIG-SERVICE/abcabc/dev/master
   tip 对应的格式为 http://CONFIG-SERVICE/{appname}/{profile}/{branch|commit id}
  在configService中,对应的JGitEnvironmentRepository会按照 spring.cloud.config.server.git.uri的配置将{xxx}提示替换即,终极生成的git url为 http://your_git_space/your_org_name/abcabc-config.git 如允许以确保git url是一个精确的git仓库地址。然后神奇的事情来了 同样的配置,和git仓库地址。在linux上不行,在windows上可以。
   这里有个必要注意的东西,spring.cloud.config.server.git.basedir=file:///E:/config-repo 在windows下必要是 file:/// 在linux下是file:/
    其次在linux情况中假如没有利用file:开头,则spring会把当地配置文件的地址修改为 runpath+spring.cloud.config.server.git.basedir 配置的值,好比linux情况下配置
spring.cloud.config.server.git.basedir=~/config-service/config-repo,那么给到spring 配置文件加载的File地址就会酿成 /home/user/config-server/~/config-service/config-repo
  具体的官方文档形貌在这里 file_system_backend
排除上面的注意点之后依然不可以,先来看看具体是怎么获取到配置文件的,访问http://CONFIG-SERVICE/fxdanmugw/dev/master,暴露该端点的是org.springframework.cloud.config.server.environment.EnvironmentController#labelled
  1.         @RequestMapping(path = "/{name}/{profiles}/{label:.*}",
  2.                         produces = MediaType.APPLICATION_JSON_VALUE)
  3.         public Environment labelled(@PathVariable String name, @PathVariable String profiles,
  4.                         @PathVariable String label) {
  5.                 return getEnvironment(name, profiles, label, false);
  6.         }
复制代码
然后通过EnvironmentEncryptorEnvironmentRepository#findOne(java.lang.String, java.lang.String, java.lang.String, boolean)
···>CompositeEnvironmentRepository#findOne(java.lang.String, java.lang.String, java.lang.String, boolean)
···>CompositeEnvironmentRepository#findOne(java.lang.String, java.lang.String, java.lang.String, boolean)
···>MultipleJGitEnvironmentRepository#findOne
···>AbstractScmEnvironmentRepository#findOne(java.lang.String, java.lang.String, java.lang.String, boolean)
   该超类的方法是通过 JGitEnvironmentRepository的实例调用的
  好的焦点逻辑到了我们看下源码
  1.         public synchronized Environment findOne(String application, String profile,
  2.                         String label, boolean includeOrigin) {
  3.                 NativeEnvironmentRepository delegate = new NativeEnvironmentRepository(
  4.                                 getEnvironment(), new NativeEnvironmentProperties());
  5.                 Locations locations = getLocations(application, profile, label);
  6.                 delegate.setSearchLocations(locations.getLocations());
  7.                 Environment result = delegate.findOne(application, profile, "", includeOrigin);
  8.                 result.setVersion(locations.getVersion());
  9.                 result.setLabel(label);
  10.                 return this.cleaner.clean(result, getWorkingDirectory().toURI().toString(),
  11.                                 getUri());
  12.         }
复制代码
大致流程新建了一个NativeEnvironmentRepository 去读取当地文件,getLocations里面会根据application,profile,label 刷新对应的git仓库,保证仓库的version版本和远程git仓库一致,然后启动一个SpringApplication容器 利用配置–spring.config.path=file:/xxxx 的情势让这个容器去加载配置文件,在从上下文中取出所有和当前 application,profile,label 符合的配置返回出去,application=abcabc,profile=dev,label=master。有点不可思议完全没想到会利用SpringApplicationBuilder 构建一个容器举行配置读取,这个懒投的可以,固然大概是有其他必要缘故原由导致不得不这么做,下面来看具体部门代码
git 仓库刷新部门之前博客有讲过,简朴过一下
JGitEnvironmentRepository#getLocations ···> JGitEnvironmentRepository#refresh
delegate.setSearchLocations(locations.getLocations());locations就是过滤出来的当地仓库地址它的实际值大概是如许的

application被我马赛克了,理解成abcabc吧,然后依靠NativeEnvironmentRepository#findOne(java.lang.String, java.lang.String, java.lang.String, boolean)来获取Environment,具体代码如下
  1. @Override
  2.         public Environment findOne(String config, String profile, String label,
  3.                         boolean includeOrigin) {
  4.                 SpringApplicationBuilder builder = new SpringApplicationBuilder(
  5.                                 PropertyPlaceholderAutoConfiguration.class);
  6.                 ConfigurableEnvironment environment = getEnvironment(profile);
  7.                 builder.environment(environment);
  8.                 builder.web(WebApplicationType.NONE).bannerMode(Mode.OFF);
  9.                 if (!logger.isDebugEnabled()) {
  10.                         // Make the mini-application startup less verbose
  11.                         builder.logStartupInfo(false);
  12.                 }
  13.                 String[] args = getArgs(config, profile, label);
  14.                 // Explicitly set the listeners (to exclude logging listener which would change
  15.                 // log levels in the caller)
  16.                 builder.application()
  17.                                 .setListeners(Arrays.asList(new ConfigFileApplicationListener()));
  18.                 try (ConfigurableApplicationContext context = builder.run(args)) {
  19.                         environment.getPropertySources().remove("profiles");
  20.                         return clean(new PassthruEnvironmentRepository(environment).findOne(config,
  21.                                         profile, label, includeOrigin));
  22.                 }
  23.                 catch (Exception e) {
  24.                         String msg = String.format(
  25.                                         "Could not construct context for config=%s profile=%s label=%s includeOrigin=%b",
  26.                                         config, profile, label, includeOrigin);
  27.                         String completeMessage = NestedExceptionUtils.buildMessage(msg,
  28.                                         NestedExceptionUtils.getMostSpecificCause(e));
  29.                         throw new FailedToConstructEnvironmentException(completeMessage, e);
  30.                 }
  31.         }
复制代码
重要看args的值,如下
  1. args[]数值
  2. 0 = "--spring.config.name=application,abcabc"
  3. 1 = "--spring.cloud.bootstrap.enabled=false"
  4. 2 = "--encrypt.failOnError=false"
  5. 3 = "--spring.config.location=file:/E:/config-repo/config-repo-6098804444752826688/"
复制代码
注意看这里的–spring.config.name用的是 接口请求传进来的appname。在windows和linux中文件系统对文件名的大小写敏感程度差异,举个例子

在linux是答应大小写差异的文件存在的

罪魁祸首出现了,问题就是出现在这里,windows的文件系统与linux的差异导致了,他们表现上的差异,我的处置惩罚方式是,将git url的地址改成和服务一样的驼峰大小写,再删除掉config client中配置的name,或者直接不实用 特定的名称来编写配置文件,全部写道application.yml 里面

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

风雨同行

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表