将 Spring Cloud 配置服务器连接到本地 git repo 失败

Posted

技术标签:

【中文标题】将 Spring Cloud 配置服务器连接到本地 git repo 失败【英文标题】:connecting spring cloud config server to local git repo failed 【发布时间】:2020-08-10 22:59:36 【问题描述】:

当我点击http://localhost:8888/limits-service/default 时,我得到:

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon Apr 27 16:40:38 IST 2020
There was an unexpected error (type=Internal Server Error, status=500).
Cannot load environment
java.lang.IllegalStateException: Cannot load environment
    at org.springframework.cloud.config.server.environment.JGitEnvironmentRepository.refresh(JGitEnvironmentRepository.java:299)
    at org.springframework.cloud.config.server.environment.JGitEnvironmentRepository.getLocations(JGitEnvironmentRepository.java:246)
    at org.springframework.cloud.config.server.environment.MultipleJGitEnvironmentRepository.getLocations(MultipleJGitEnvironmentRepository.java:146)
    at org.springframework.cloud.config.server.environment.AbstractScmEnvironmentRepository.findOne(AbstractScmEnvironmentRepository.java:57)
    at org.springframework.cloud.config.server.environment.MultipleJGitEnvironmentRepository.findOne(MultipleJGitEnvironmentRepository.java:187)
    at org.springframework.cloud.config.server.environment.CompositeEnvironmentRepository.findOne(CompositeEnvironmentRepository.java:58)
    at org.springframework.cloud.config.server.environment.EnvironmentEncryptorEnvironmentRepository.findOne(EnvironmentEncryptorEnvironmentRepository.java:61)
    at org.springframework.cloud.config.server.environment.EnvironmentController.getEnvironment(EnvironmentController.java:136)
    at org.springframework.cloud.config.server.environment.EnvironmentController.defaultLabel(EnvironmentController.java:108)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:282)
    at org.springframework.cloud.context.scope.GenericScope$LockedScopedProxyFactoryBean.invoke(GenericScope.java:499)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:691)
    at org.springframework.cloud.config.server.environment.EnvironmentController$$EnhancerBySpringCGLIB$$61a7f28e.defaultLabel(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:879)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:109)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1594)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalStateException: No directory at file:///C:lenovo G50git-localconfig-repo/limits-service.properties
    at org.springframework.util.Assert.state(Assert.java:73)
    at org.springframework.cloud.config.server.environment.JGitEnvironmentRepository.copyFromLocalRepository(JGitEnvironmentRepository.java:576)
    at org.springframework.cloud.config.server.environment.JGitEnvironmentRepository.copyRepository(JGitEnvironmentRepository.java:561)
    at org.springframework.cloud.config.server.environment.JGitEnvironmentRepository.createGitClient(JGitEnvironmentRepository.java:547)
    at org.springframework.cloud.config.server.environment.JGitEnvironmentRepository.refresh(JGitEnvironmentRepository.java:268)
    ... 72 more

这是我的 application.properties 文件:

spring.application.name=spring-cloud-config-server
server.port=8888
spring.cloud.config.server.git.uri=file:///C:\lenovo G50\git-localconfig-repo/limits-service.properties

这是我本地的 git repo 文件:

limits-service.minimum=8
limits-service.maximum=888

这是我的主要课程:

package com.in28minutes.microservices.springcloudconfigserver;

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

@EnableConfigServer
@SpringBootApplication
public class SpringCloudConfigServerApplication 
    public static void main(String[] args) 
        SpringApplication.run(SpringCloudConfigServerApplication.class, args);
    

请帮我解决这个问题。

【问题讨论】:

【参考方案1】:

堆栈跟踪中的最后一个条目显示了无效的资源位置。

java.lang.IllegalStateException: No directory at file:///C:lenovo G50git-localconfig-repo/limits-service.properties

由于这是在 JVM 中运行的,因此您不需要在路径中使用反斜杠。在这种情况下,反斜杠用于转义紧跟在斜杠后面的字符。将反斜杠更改为正斜杠,这应该可以让您克服此错误。这与启动和运行配置服务器没有直接关系,因此不能保证这会解决您的所有问题。

另外,最好将堆栈跟踪格式化为代码,以使其对其他用户更具可读性。

【讨论】:

尝试您的建议后出现相同的错误(java.lang.IllegalStateException:文件中没有目录:\\\C:lenovo G50\git-localconfig-repo\limits-service.properties)请帮助我如何解决这个问题 @Naga 请仔细阅读错误消息和您得到的答案:不要使用反斜杠,改用正斜杠。 请帮我解决以下问题@Robert【参考方案2】:

也从位置路径中删除所有反斜杠并将其替换为正斜杠。永远记得在位置路径之前使用file://

例如:file://C:/Users/google/Desktop/SpringLearning/Mocroservice_new_June/git-localconfig-repo

这样试试

【讨论】:

【参考方案3】:

替换application.properties:

spring.cloud.config.server.git.uri=file:///C:\lenovo G50\git-localconfig-repo/limits-service.properties

spring.cloud.config.server.git.uri=file://C:/lenovo G50/git-localconfig-repo

【讨论】:

我认为你的一张照片坏了。并且请将文本信息显示为文本,而不是文本的(链接到)图片。 除了有问题的帮助图片之外,您的帖子在现有答案之上提供了哪些额外的见解? 欢迎来到 Stack Overflow!请务必阅读How to Answer 并将您发布的图片替换为实际文本。 永远,永远发布代码或错误消息的图像。此外,如上所述,当您的答案已经存在时,请不要再次发布它,而是对有帮助的正确答案进行投票。谢谢。【参考方案4】:

您应该删除 file:// 并尝试这样:

C:/lenovo G50/git-localconfig-repo/

【讨论】:

【参考方案5】:

我希望你现在已经得到了答案。 我自己也面临同样的问题。想通了异常说

文件中没有目录

这意味着您没有提供目录,而是提供了文件的路径:

file:///C:\lenovo G50\git-localconfig-repo/limits-service.properties

只需从路径中删除文件:

file:///C:\lenovo G50\git-localconfig-repo/

干杯!

【讨论】:

【参考方案6】:

文件名中不能有空格。

删除空格,将文件夹名称从“lenovo G50”更改为“lenovo_G50”,并且不需要指向它从使用的URL中提取的属性文件。

去掉空格后使用下面的uri:

spring.cloud.config.server.git.uri=C:/lenovo_G50/git-localconfig-repo

【讨论】:

以上是关于将 Spring Cloud 配置服务器连接到本地 git repo 失败的主要内容,如果未能解决你的问题,请参考以下文章

Spring Cloud Config:客户端不尝试连接到配置服务器

Spring GCP 服务未连接到 Cloud SQL 数据库

如何将 spring gcp PubSubTemplate 连接到本地实例?

将 Spring Boot 应用程序连接到 Google Cloud 中的 MySQL 数据库

Spring Cloud Config Server 无法通过 Docker 中的 ssh 连接到 git

如何在本地使用 spring-cloud-starter-aws 运行应用程序?