dyc87112 / blog-comments

5 stars 1 forks source link

Spring Cloud构建微服务架构(四)分布式配置中心(续) | 程序猿DD #137

Open dyc87112 opened 6 years ago

dyc87112 commented 6 years ago

http://blog.didispace.com/springcloud4-2/

本文接之前的《Spring Cloud构建微服务架构(四)分布式配置中心》,继续来说说Spring Cloud Config的使用。 先来回顾一下,在前文中我们完成了什么: 构建了config-server,连接到Git仓库 在Git上创建了一个config-repo目录,用来存储配置信息 构建了config-cli

jndxfjz commented 6 years ago

请教:我根据您的方法配置了负载均衡,服务治理使用的是consul,发现启动config-client的时候,都是从其中一个server获取配置文件,但是,如果停掉这个服务,会从另一个server获取配置文件,我怎么验证这个是正确的配置,还是错误的?谢谢

ghost commented 6 years ago

按照你文档中的方式配置了config-server 端,但是启动后一直报异常呢? at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:134) ~[httpclient-4.5.6.jar:4.5.6] at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:610) ~[httpclient-4.5.6.jar:4.5.6] at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:445) ~[httpclient-4.5.6.jar:4.5.6] at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:835) ~[httpclient-4.5.6.jar:4.5.6] at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:118) ~[httpclient-4.5.6.jar:4.5.6] at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56) ~[httpclient-4.5.6.jar:4.5.6] at com.sun.jersey.client.apache4.ApacheHttpClient4Handler.handle(ApacheHttpClient4Handler.java:173) ~[jersey-apache-client4-1.19.1.jar:1.19.1] ... 30 common frames omitted

2018-11-29 13:19:32.879 WARN 14524 --- [freshExecutor-0] c.n.d.s.t.d.RetryableEurekaHttpClient : Request execution failed with message: java.net.ConnectException: Connection refused: connect 2018-11-29 13:19:32.879 ERROR 14524 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_HELLOCONFIGSERVER-1/IBM650-PC02E51Z.cn.ibm.com:helloConfigServer-1:7001 - was unable to refresh its cache! status = Cannot execute request on any known server

com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server at com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.execute(RetryableEurekaHttpClient.java:112) ~[eureka-client-1.9.8.jar:1.9.8] at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.getApplications(EurekaHttpClientDecorator.java:134) ~[eureka-client-1.9.8.jar:1.9.8] at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$6.execute(EurekaHttpClientDecorator.java:137) ~[eureka-client-1.9.8.jar:1.9.8] at com.netflix.discovery.shared.transport.decorator.SessionedEurekaHttpClient.execute(SessionedEurekaHttpClient.java:77) ~[eureka-client-1.9.8.jar:1.9.8] at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.getApplications(EurekaHttpClientDecorator.java:134) ~[eureka-client-1.9.8.jar:1.9.8] at com.netflix.discovery.DiscoveryClient.getAndStoreFullRegistry(DiscoveryClient.java:1051) [eureka-client-1.9.8.jar:1.9.8] at com.netflix.discovery.DiscoveryClient.fetchRegistry(DiscoveryClient.java:965) [eureka-client-1.9.8.jar:1.9.8] at com.netflix.discovery.DiscoveryClient.refreshRegistry(DiscoveryClient.java:1479) [eureka-client-1.9.8.jar:1.9.8] at com.netflix.discovery.DiscoveryClient$CacheRefreshThread.run(DiscoveryClient.java:1446) [eureka-client-1.9.8.jar:1.9.8] at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_152] at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_152] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_152] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_152] at java.lang.Thread.run(Thread.java:748) [na:1.8.0_152]

2018-11-29 13:19:33.115 ERROR 14524 --- [tbeatExecutor-0] c.n.d.s.t.d.RedirectingEurekaHttpClient : Request execution error. endpoint=DefaultEndpoint{ serviceUrl='http://localhost:8888/eureka/}

com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect at com.sun.jersey.client.apache4.ApacheHttpClient4Handler.handle(ApacheHttpClient4Handler.java:187) ~[jersey-apache-client4-1.19.1.jar:1.19.1] at com.sun.jersey.api.client.filter.GZIPContentEncodingFilter.handle(GZIPContentEncodingFilter.java:123) ~[jersey-client-1.19.1.jar:1.19.1] at com.netflix.discovery.EurekaIdentityHeaderFilter.handle(EurekaIdentityHeaderFilter.java:27) ~[eureka-client-1.9.8.jar:1.9.8] at com.sun.jersey.api.client.Client.handle(Client.java:652) ~[jersey-client-1.19.1.jar:1.19.1] at com.sun.jersey.api.client.WebResource.handle(WebResource.java:682) ~[jersey-client-1.19.1.jar:1.19.1] at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74) ~[jersey-client-1.19.1.jar:1.19.1] at com.sun.jersey.api.client.WebResource$Builder.put(WebResource.java:529) ~[jersey-client-1.19.1.jar:1.19.1] at com.netflix.discovery.shared.transport.jersey.AbstractJerseyEurekaHttpClient.sendHeartBeat(AbstractJerseyEurekaHttpClient.java:102) ~[eureka-client-1.9.8.jar:1.9.8] at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$3.execute(EurekaHttpClientDecorator.java:92) [eureka-client-1.9.8.jar:1.9.8] at com.netflix.discovery.shared.transport.decorator.MetricsCollectingEurekaHttpClient.execute(MetricsCollectingEurekaHttpClient.java:73) ~[eureka-client-1.9.8.jar:1.9.8] at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.sendHeartBeat(EurekaHttpClientDecorator.java:89) [eureka-client-1.9.8.jar:1.9.8] at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$3.execute(EurekaHttpClientDecorator.java:92) [eureka-client-1.9.8.jar:1.9.8] at com.netflix.discovery.shared.transport.decorator.RedirectingEurekaHttpClient.executeOnNewServer(RedirectingEurekaHttpClient.java:118) ~[eureka-client-1.9.8.jar:1.9.8] at com.netflix.discovery.shared.transport.decorator.RedirectingEurekaHttpClient.execute(RedirectingEurekaHttpClient.java:79) ~[eureka-client-1.9.8.jar:1.9.8] at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.sendHeartBeat(EurekaHttpClientDecorator.java:89) [eureka-client-1.9.8.jar:1.9.8] at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$3.execute(EurekaHttpClientDecorator.java:92) [eureka-client-1.9.8.jar:1.9.8] at com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.execute(RetryableEurekaHttpClient.java:120) [eureka-client-1.9.8.jar:1.9.8] at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.sendHeartBeat(EurekaHttpClientDecorator.java:89) [eureka-client-1.9.8.jar:1.9.8] at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$3.execute(EurekaHttpClientDecorator.java:92) [eureka-client-1.9.8.jar:1.9.8] at com.netflix.discovery.shared.transport.decorator.SessionedEurekaHttpClient.execute(SessionedEurekaHttpClient.java:77) [eureka-client-1.9.8.jar:1.9.8] at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.sendHeartBeat(EurekaHttpClientDecorator.java:89) [eureka-client-1.9.8.jar:1.9.8] at com.netflix.discovery.DiscoveryClient.renew(DiscoveryClient.java:846) [eureka-client-1.9.8.jar:1.9.8] at com.netflix.discovery.DiscoveryClient$HeartbeatThread.run(DiscoveryClient.java:1405) [eureka-client-1.9.8.jar:1.9.8] at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_152] at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_152] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_152] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_152] at java.lang.Thread.run(Thread.java:748) [na:1.8.0_152] Caused by: java.net.ConnectException: Connection refused: connect at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_152] at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) ~[na:1.8.0_152] at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_152] at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_152] at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_152] at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[na:1.8.0_152] at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_152] at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_152] at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:121) ~[httpclient-4.5.6.jar:4.5.6] at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:180) ~[httpclient-4.5.6.jar:4.5.6] at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:144) ~[httpclient-4.5.6.jar:4.5.6] at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:134) ~[httpclient-4.5.6.jar:4.5.6] at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:610) ~[httpclient-4.5.6.jar:4.5.6] at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:445) ~[httpclient-4.5.6.jar:4.5.6] at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:835) ~[httpclient-4.5.6.jar:4.5.6] at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:118) ~[httpclient-4.5.6.jar:4.5.6] at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56) ~[httpclient-4.5.6.jar:4.5.6] at com.sun.jersey.client.apache4.ApacheHttpClient4Handler.handle(ApacheHttpClient4Handler.java:173) ~[jersey-apache-client4-1.19.1.jar:1.19.1] ... 27 common frames omitted

2018-11-29 13:19:33.115 WARN 14524 --- [tbeatExecutor-0] c.n.d.s.t.d.RetryableEurekaHttpClient : Request execution failed with message: java.net.ConnectException: Connection refused: connect 2018-11-29 13:19:33.115 ERROR 14524 --- [tbeatExecutor-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_HELLOCONFIGSERVER-1/IBM650-PC02E51Z.cn.ibm.com:helloConfigServer-1:7001 - was unable to send heartbeat!

com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server at com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.execute(RetryableEurekaHttpClient.java:112) ~[eureka-client-1.9.8.jar:1.9.8] at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.sendHeartBeat(EurekaHttpClientDecorator.java:89) ~[eureka-client-1.9.8.jar:1.9.8] at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$3.execute(EurekaHttpClientDecorator.java:92) ~[eureka-client-1.9.8.jar:1.9.8] at com.netflix.discovery.shared.transport.decorator.SessionedEurekaHttpClient.execute(SessionedEurekaHttpClient.java:77) ~[eureka-client-1.9.8.jar:1.9.8] at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.sendHeartBeat(EurekaHttpClientDecorator.java:89) ~[eureka-client-1.9.8.jar:1.9.8] at com.netflix.discovery.DiscoveryClient.renew(DiscoveryClient.java:846) ~[eureka-client-1.9.8.jar:1.9.8] at com.netflix.discovery.DiscoveryClient$HeartbeatThread.run(DiscoveryClient.java:1405) [eureka-client-1.9.8.jar:1.9.8] at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_152] at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_152] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_152] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_152] at java.lang.Thread.run(Thread.java:748) [na:1.8.0_152]

2018-11-29 13:19:39.366 INFO 14524 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_HELLOCONFIGSERVER-1/IBM650-PC02E51Z.cn.ibm.com:helloConfigServer-1:7001: registering service... 2018-11-29 13:19:40.366 ERROR 14524 --- [nfoReplicator-0] c.n.d.s.t.d.RedirectingEurekaHttpClient : Request execution error. endpoint=DefaultEndpoint{ serviceUrl='http://localhost:8888/eureka/}

ghost commented 6 years ago

网上说要加下面这两句, 但是加了之后config-server不就没有注册了么?怎么被config-client发现呢? eureka.client.register-with-eureka=false eureka.client.fetch-registry=false

williamlsoul commented 5 years ago

运行demo结果是:Could not resolve placeholder 'from' in string value "${from}".请问哪一个代码示例对应该章节内容?

dyc87112 commented 5 years ago

@williamlsoul 运行demo结果是:Could not resolve placeholder 'from' in string value "${from}".请问哪一个代码示例对应该章节内容?

https://github.com/dyc87112/SpringCloud-Learning/tree/master/1-Brixton%E7%89%88%E6%95%99%E7%A8%8B%E7%A4%BA%E4%BE%8B/Chapter1-1-8

williamlsoul commented 5 years ago

@dyc87112

@williamlsoul 运行demo结果是:Could not resolve placeholder 'from' in string value "${from}".请问哪一个代码示例对应该章节内容?

https://github.com/dyc87112/SpringCloud-Learning/tree/master/1-Brixton%E7%89%88%E6%95%99%E7%A8%8B%E7%A4%BA%E4%BE%8B/Chapter1-1-8

感谢博主热心解答。当前原本就是基于Chapter1-1-8demo运行的,启动eureka-server,然后启动config-server,都没问题,最后启动config-client时,报无法解析占位符字符串from。麻烦博主您在执行看是否存在该问题,或由什么原因引发该问题?实际config-client工程配置文件并未配置from属性信息

hecan1992 commented 5 years ago

@williamlsoul 运行demo结果是:Could not resolve placeholder 'from' in string value "${from}".请问哪一个代码示例对应该章节内容? 你好,请问你上面将config-server注册到服务中心是如何实现的,我也报楼上的错误c.n.d.s.t.d.RetryableEurekaHttpClient : Request execution failed with message: java.net.ConnectException: Connection refused: connect和com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server

hecan1992 commented 5 years ago

@wenchunl 你好请问你解决了吗?我也遇到一样的问题了

williamlsoul commented 5 years ago

@hecan1992 @wenchunl 你好请问你解决了吗?我也遇到一样的问题了

已经解决。首先要确保config-server能够正常访问到配置信息,因为from配置实际是客户端启动时通过访问服务端获取并注入参数值

williamlsoul commented 5 years ago

@hecan1992

@williamlsoul 运行demo结果是:Could not resolve placeholder 'from' in string value "${from}".请问哪一个代码示例对应该章节内容? 你好,请问你上面将config-server注册到服务中心是如何实现的,我也报楼上的错误c.n.d.s.t.d.RetryableEurekaHttpClient : Request execution failed with message: java.net.ConnectException: Connection refused: connect和com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server

看你这个报错来的错误是未找到注册中心,你需要确认你的注册中心是否启动,并核对配置文件是否正确配置注册中心地址

ghost commented 5 years ago

@wenchunl 你好请问你解决了吗?我也遇到一样的问题了

@hecan1992 我已经解决好了。解决方法和williamlsoul 说到的点是一致的。

hecan1992 commented 5 years ago

@williamlsoul

@hecan1992 @wenchunl 你好请问你解决了吗?我也遇到一样的问题了

已经解决。首先要确保config-server能够正常访问到配置信息,因为from配置实际是客户端启动时通过访问服务端获取并注入参数值

首先感谢你的解答。我已经可以正常的访问获取到git里面的配置信息了,但是我在config-server-git项目里面按照文档配置后,启动报错c.n.d.s.t.d.RetryableEurekaHttpClient : Request execution failed with message: java.net.ConnectException: Connection refused: connect和com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server。网上说是注册中心不能自己注注册自己,所以我加了eureka.client.register-with-eureka=false eureka.client.fetch-registry=false,这样可以正常启动了。但是访问http://localhost:1111/失败,无法访问此网站,localhost 拒绝了我们的连接请求。现在有点不明白的是作者使用的是那种情况: 1.将config-server-git项目(获取git配置中心的项目,理解为git配置服务提供者),它需要另外找到一个服务注册中心,并将config-server-git服务注入进去 2.将config-server-git项目 (获取git配置中心的项目,理解一个配置服务的注册中心,类似于eureka-server),并将自己作为一个服务注册进来供消费方使用。(这种就会报上面的错误,无法自注入)。

hecan1992 commented 5 years ago

@williamlsoul 我将config-server-git配置项目作为服务提供方,按照以前的逻辑注册到eureka-server里面去(http://localhost:1001/eureka/) spring.application.name=config-server server.port=7001 eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/

http://localhost:1001可正常访问了

hanwq520 commented 5 years ago

应该说明下1.3.7版本是可以的,之前我用1.5.9版本死活不能实现动态刷新配置,博主应该整个表格说明一下哪些版本支持这样实现,另外1.0和2.0版本的实现也是有差别的

SpringStudent commented 5 years ago

同我用的springboot1.5.8版本也是不能实现配置的刷新,找到解决方案了,在UserController上添加@RefreshScope注解

SpringStudent commented 5 years ago

@hanwq520 应该说明下1.3.7版本是可以的,之前我用1.5.9版本死活不能实现动态刷新配置,博主应该整个表格说明一下哪些版本支持这样实现,另外1.0和2.0版本的实现也是有差别的 在UserController上添加@RefreshScope注解

zhyblack commented 5 years ago

访问 config-client /refresh post 请求,返回

{
    "timestamp": 1537865395040,
    "status": 401,
    "error": "Unauthorized",
    "message": "Full authentication is required to access this resource.",
    "path": "/refresh"
}

需要在bootstrap.yml添加如下配置

management:
  security:
    enabled: false
yimapingchuan1 commented 5 years ago

@wenchunl 网上说要加下面这两句, 但是加了之后config-server不就没有注册了么?怎么被config-client发现呢? eureka.client.register-with-eureka=false eureka.client.fetch-registry=false

是服务中心eureka-server要加这两句,而不是配置中心config-server要加

yimapingchuan1 commented 5 years ago

@SpringStudent 同我用的springboot1.5.8版本也是不能实现配置的刷新,找到解决方案了,在UserController上添加@RefreshScope注解

用了你的方法,我实现刷新了,牛逼!

Joypigg commented 5 years ago

19年8月的我看着16年的教程!这些教程修正有好些地方需要修正了啊

I-DO-NOT-KNOW commented 5 years ago

在Eureka中已经成功注册config-server,并且访问http://localhost:7001/didispace/dev/master也获取到了git上的信息,如下: {"name":"didispace","profiles":["dev"],"label":"master","version":"c75295fa4d838a58ddc6dc306501baceb04f0220","propertySources":[{"name":"https://github.com/I-DO-NOT-KNOW/cloud-config-demo/didispace-dev.properties","source":{"from":"git-dev-1.0"}},{"name":"https://github.com/I-DO-NOT-KNOW/cloud-config-demo/didispace.properties","source":{"from":"git-default-1.0"}}]} 但是运行config-client的时候还是报错Could not resolve placeholder 'from' in string value "${from}",哪位大神也遇到这样的问题,怎么解决的?

I-DO-NOT-KNOW commented 5 years ago

在Eureka中已经成功注册config-server,并且访问http://localhost:7001/didispace/dev/master也获取到了git上的信息,如下: {"name":"didispace","profiles":["dev"],"label":"master","version":"c75295fa4d838a58ddc6dc306501baceb04f0220","propertySources":[{"name":"https://github.com/I-DO-NOT-KNOW/cloud-config-demo/didispace-dev.properties","source":{"from":"git-dev-1.0"}},{"name":"https://github.com/I-DO-NOT-KNOW/cloud-config-demo/didispace.properties","source":{"from":"git-default-1.0"}}]} 但是运行config-client的时候还是报错Could not resolve placeholder 'from' in string value "${from}",哪位大神也遇到这样的问题,怎么解决的?

解决了,clien的pom配置有问题

I-DO-NOT-KNOW commented 5 years ago

不好意思,我是刚开始跟着你写的博客来学习cloud的,中间配置的时候不小心把pom配置错了,所以client一直报错。博客写的没有错的。 ----- 原始邮件 ----- 发件人:Jia-99 notifications@github.com 收件人:dyc87112/blog-comments blog-comments@noreply.github.com 抄送人:I-DO-NOT-KNOW xiao_a_hei@sina.com, Comment comment@noreply.github.com 主题:Re:_[dyc87112/blog-comments]_SpringCloud构建微服务架构(四)分布式配置中心(续)|程序猿DD(#137) 日期:2019年08月18日 23点27分

@Joypigg

19年8月的我看着16年的教程!这些教程修正有好些地方需要修正了啊

我照着做了一遍,只有refresh的接口需要改,请问还有哪边需要更新吗

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.