springCoud config当server和client不在同一台电脑报错排查

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.DiscoveryClientConfigServiceBootstrapConfigurationrefresh()方法发现原因,部分代码如下

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,那么注释掉应该正常,测试结果是正常
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值