1、启动问题
插入了一些错误代码是方便遇到同样问题的伙伴能够搜到,请见谅~
配置内容,仅配置了服务启动的端口
1.1 同一台电脑
启动正常
2.2 不同电脑
14:31:59.930 [main] INFO o.s.c.c.c.ConfigServicePropertySourceLocator - Fetching config from server at : http://OLEOMPHMDFJN8H3:8806/
14:32:11.252 [main] INFO o.s.c.c.c.ConfigServicePropertySourceLocator - Connect Timeout Exception on Url - http://OLEOMPHMDFJN8H3:8806/. Will be trying the next url if available
14:32:11.257 [main] ERROR o.s.boot.SpringApplication - Application run failed
java.lang.IllegalStateException: Could not locate PropertySource and the fail fast property is set, failing
at org.springframework.cloud.config.client.ConfigServicePropertySourceLocator.locate(ConfigServicePropertySourceLocator.java:147)
at org.springframework.cloud.bootstrap.config.PropertySourceLocator.locateCollection(PropertySourceLocator.java:52)
at org.springframework.cloud.config.client.ConfigServicePropertySourceLocator.locateCollection(ConfigServicePropertySourceLocator.java:162)
at org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration.initialize(PropertySourceBootstrapConfiguration.java:101)
at org.springframework.boot.SpringApplication.applyInitializers(SpringApplication.java:626)
at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:370)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:314)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
at com.nld.NldProjectApplication.main(NldProjectApplication.java:41)
Caused by: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://OLEOMPHMDFJN8H3:8806/nld-common,nld-project/dev/master": OLEOMPHMDFJN8H3; nested exception is java.net.UnknownHostException: OLEOMPHMDFJN8H3
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:748)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:674)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:583)
at org.springframework.cloud.config.client.ConfigServicePropertySourceLocator.getRemoteEnvironment(ConfigServicePropertySourceLocator.java:264)
at org.springframework.cloud.config.client.ConfigServicePropertySourceLocator.locate(ConfigServicePropertySourceLocator.java:106)
... 9 common frames omitted
Caused by: java.net.UnknownHostException: OLEOMPHMDFJN8H3
2.3 解决办法
错误信息很明确,说是地址无法访问,我明明配置的是http://192.168.0.200:8806
,但是提示的是http://OLEOMPHMDFJN8H3:8806/
刚开始我以为OLEOMPHMDFJN8H3
是本机名称,但是核对了一下不是,而是confgi server
电脑的名称,问题基本可以确定是配置的问题,但是不清楚是服务还是客户端
经过跟踪调试客户端源代码org.springframework.cloud.config.client.DiscoveryClientConfigServiceBootstrapConfiguration
的refresh()
方法发现原因,部分代码如下
private void refresh() {
try {
String serviceId = this.config.getDiscovery().getServiceId();
List<String> listOfUrls = new ArrayList();
List<ServiceInstance> serviceInstances = this.instanceProvider.getConfigServerInstances(serviceId);
for(int i = 0; i < serviceInstances.size(); ++i) {
ServiceInstance server = (ServiceInstance)serviceInstances.get(i);
String url = this.getHomePage(server);
// 此处删除部分逻辑
listOfUrls.add(url);
}
String[] uri = new String[listOfUrls.size()];
uri = (String[])listOfUrls.toArray(uri);
this.config.setUri(uri);
} catch (Exception var9) {
logger.warn("Could not locate configserver via discovery", var9);
}
}
原来使用的uri并不是我们配置里面的uri,她是通过serviceId到注册中心拿到注册实例serviceInstance,去取的是server里的homePageUrl,的确是保存的地址,的确是跟配置中心内容一致
当然内网也可以配置根据机器名显示,可以查看这篇文章进行设置局域网内通过机器名访问其他机器,但是如果部署在服务器肯定只能通过域名或者IP访问,查询资料发现enurka默认是用机器名注册的服务,需要使用Ip的话得在config server加上eureka: instance: prefer-ip-address: true
用于开启基于IP注册
开启前
开启后,原来的机器名就变成了IP地址,重启服务正常
2、service-id说明
由于上面cloud是通过服务id service-id加载的实例信息,但是看到某些教程里面并没有spring.cloud.config.discovery.service-id配置也能正常启动,实际动手测试一下,发现并不能启动,根据报错信息可知默认的配置服务名为configserver
,需要把配置服务名称nld-b-config改为configserver即可
15:23:15.501 [main] INFO c.n.d.p.DiscoveryJerseyProvider - Using XML encoding codec XStreamXml
15:23:15.501 [main] INFO c.n.d.p.DiscoveryJerseyProvider - Using XML decoding codec XStreamXml
15:23:15.707 [main] INFO c.n.d.s.r.aws.ConfigClusterResolver - Resolving eureka endpoints via configuration
15:23:16.009 [main] INFO c.netflix.discovery.DiscoveryClient - Disable delta property : false
15:23:16.009 [main] INFO c.netflix.discovery.DiscoveryClient - Single vip registry refresh property : null
15:23:16.009 [main] INFO c.netflix.discovery.DiscoveryClient - Force full registry fetch : false
15:23:16.009 [main] INFO c.netflix.discovery.DiscoveryClient - Application is null : false
15:23:16.009 [main] INFO c.netflix.discovery.DiscoveryClient - Registered Applications size is zero : true
15:23:16.009 [main] INFO c.netflix.discovery.DiscoveryClient - Application version is -1: true
15:23:16.009 [main] INFO c.netflix.discovery.DiscoveryClient - Getting all instance registry info from the eureka server
15:23:16.213 [main] INFO c.netflix.discovery.DiscoveryClient - The response status is 200
15:23:16.218 [main] INFO c.netflix.discovery.DiscoveryClient - Not registering with Eureka server per configuration
15:23:16.224 [main] INFO c.netflix.discovery.DiscoveryClient - Discovery Client initialized at timestamp 1613805796223 with initial instances count: 4
15:23:16.251 [main] INFO o.s.b.a.l.ConditionEvaluationReportLoggingListener -
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
15:23:16.254 [main] ERROR o.s.boot.SpringApplication - Application run failed
java.lang.IllegalStateException: No instances found of configserver (configserver)
at org.springframework.cloud.config.client.ConfigServerInstanceProvider.getConfigServerInstances(ConfigServerInstanceProvider.java:48)
at org.springframework.cloud.config.client.DiscoveryClientConfigServiceBootstrapConfiguration.refresh(DiscoveryClientConfigServiceBootstrapConfiguration.java:102)
at org.springframework.cloud.config.client.DiscoveryClientConfigServiceBootstrapConfiguration.startup(DiscoveryClientConfigServiceBootstrapConfiguration.java:88)
at org.springframework.cloud.config.client.DiscoveryClientConfigServiceBootstrapConfiguration.onApplicationEvent(DiscoveryClientConfigServiceBootstrapConfiguration.java:80)
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:404)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:361)
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:898)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:554)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
3、后序
既然没用到spring.cloud.config.uri
,那么注释掉应该正常,测试结果是正常