前言
问题描述
最近事情不是很多,打算重新学习一下SpringCloud微服务的东西。在配置config-server请求远程配置文件时报错No custom http config found for URL: https://github.com/XXX/XXX/info/refs?service=git-upload-pack
技术栈
- springboot 2.0.7.RELEASE
- springcloud Finchley系列
application.yml
server:
port: 8086
spring:
application:
name: config-server
cloud:
config:
label: master
server:
git:
uri: https://github.com/forezp/SpringcloudConfig/
searchPaths: respo
eureka:
instance:
hostname: localhost
client:
fetch-registry: true
register-with-eureka: true
service-url:
defaultZone: http://localhost:8761/eureka
启动类
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
@EnableDiscoveryClient
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
解决思路
使用idea全局搜索No custom http config found for URL:
这个错误信息,查找到是哪一个类打出的日志,经过查找我们发现org.springframework.cloud.config.server.environment.HttpClientConfigurableHttpConnectionFactory
这个类的98行打印的一个warn级别的日志:log.warn(String.format("No custom http config found for URL: %s", url));
。以下是打印该日志的方法的源码:
简单解释下这一段代码的逻辑,远程的配置文件信息肯定是要通过网络发送HTTP请求获取的。(从源码上来看貌似是使用HttpClient来发送HTTP请求的,而HttpClientBuilder官方文档的解释是:Builder for [CloseableHttpClient
] instances.)所以builderMap这个map的本质是:key为获取properties文件的git地址,value为获取HttpClient实例的builder。既然控制台里面输出了No custom http config found for URL:
这一段警告日志,表明这个map为空。那么为什么这个map会为空呢?我们断点进去look look。
从debug信息中可以看出其中key为获取properties文件的git地址,然后把key和spec做比对,如果相等这对键值对就留下来,如果不等就进入while循环。每一次都截取最后一个/
所在的位置,然后再比对。问题就在这里!string.substring这个方法是通过index来截取串的。截取后的串是包左不包右的。而我们配置的git地址最后以/
结尾,永远都不能相等,故只能被筛除掉。
把这一段代码理清楚后,解决问题也就相当简单,只需要把我们自己配置的git地址最后的那个/
去掉即可。
修改后的application.yml
server:
port: 8086
spring:
application:
name: config-server
cloud:
config:
label: master
server:
git:
uri: https://github.com/forezp/SpringcloudConfig
searchPaths: respo
eureka:
instance:
hostname: localhost
client:
fetch-registry: true
register-with-eureka: true
service-url:
defaultZone: http://localhost:8761/eureka
修改完application.yml后重启server,再次访问http://localhost:8086/config-client/dev/
。发现控制台已经没有输出刚刚的那一段警告日志了,但是页面仍然没有响应出我们期望的结果,仍然是404。我们再debug进去看一下后续的操作。
可以看到这次的map不是空了,我们F8往下走。
lookupHttpClientBuilder-breakpoint3.png这一段代码我就没看懂咯,形参上的proxy是我本机的代理,但是在new HttpClientConnection时传递进去的却是一个null。看到这里我大概明白了应该是代理的问题,导致请求不成功。在网上搜了半天没有找到解决方案,不知道有没有大神教一下小弟这个地方怎么设置代理。所以我决定换一个思路,使用我本地的git仓库代替远程仓库。
再次修改后application.yml文件
server:
port: 8086
spring:
application:
name: config-server
cloud:
config:
label: master
server:
git:
# uri: https://github.com/forezp/SpringcloudConfig
# searchPaths: respo
uri: file:///D:/temp/localgit
searchPaths: /**
eureka:
instance:
hostname: localhost
client:
fetch-registry: true
register-with-eureka: true
service-url:
defaultZone: http://localhost:8761/eureka
不要忘记在本地仓库的地址下运行git init
来初始化,然后将config-client-dev.properties
文件放在这个路径下,最后重启server,访问http://localhost:8086/config-client/dev/
,终于出现了我们期望的结果:
这下又可以愉快的学习了。
good good study
day day up。
2018-12-25
yueyue
网友评论