刷新服务器。弹簧云配置客户端通过 /执行器 /刷新

发布于 2025-01-18 18:24:58 字数 4234 浏览 0 评论 0原文

是否可以通过某种方式动态地使Spring Cloud Config Client Application动态刷新其配置?云配置服务器?

使用“ application.properties”,使用最简单的示例项目(仅使用Spring-Boot-Starter-web使用一个RestController),下面”,/code>/cartuator/refresh “适用于” “正常”特性。

在config(config> config-client-default.yml)中具有“ server.port:12345”。我更改了配置服务器提供的配置文件(例如“ server.port:8080”),然后调用“ /cartuator/refresh ”端点的端点(例如通过“ curl -sx post http:// localhost:12345/certuator/refresh”,我可以看到配置已从配置服务器重新阅读,但是客户端不会重新启动其Web新端口上的服务器(8080

。 ”,最好是尽可能少的代码?

### Spring Cloud Config Server application.properties :
server.port=8888
spring.cloud.config.server.git.force-pull=true
spring.cloud.config.server.git.uri=file:///D:/DEVEL/local_git
spring.cloud.config.server.git.default-label=master
spring.cloud.config.server.health.enabled=false
### Spring Cloud Config Client application.properties :
management.endpoints.web.exposure.include=*
spring.application.name=config-client
spring.config.import=configserver:http://localhost:8888
### Config client startup :
$ java.exe -jar config-client-0.0.1-SNAPSHOT.jar
:: Spring Boot ::                (v2.6.5)
INFO 17028 --- [main] i.s.c.ConfigClientApplication            : Starting ConfigClientApplication using Java 11.0.14 on PC with PID 17028 (config-client-0.0.1-SNAPSHOT.jar started by Me in D:\DEVEL\config-client)
INFO 17028 --- [main] i.s.c.ConfigClientApplication            : No active profile set, falling back to 1 default profile: "default"
INFO 17028 --- [main] o.s.b.context.config.ConfigDataLoader    : Fetching config from server at : http://localhost:8888
INFO 17028 --- [main] o.s.b.context.config.ConfigDataLoader    : Located environment: name=config-client, profiles=[default], label=null, version=2262dfe9b9ffa387cdb5b3253a0951a084d35a1c, state=null
INFO 17028 --- [main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=6cab5e7d-ad01-347c-8b2d-01847b263bb8
INFO 17028 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 12345 (http)
INFO 17028 --- [main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
INFO 17028 --- [main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.60]
INFO 17028 --- [main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
INFO 17028 --- [main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 735 ms
INFO 17028 --- [main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 15 endpoint(s) beneath base path '/actuator'
INFO 17028 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 12345 (http) with context path ''
INFO 17028 --- [main] i.s.c.ConfigClientApplication            : Started ConfigClientApplication in 1.866 seconds (JVM running for 2.153)
### Changed the client configuration served by the config server :
$ curl -sS http://localhost:8888/config-client/default | json_pp | fgrep 'server.port'
            "server.port" : 8080
### Calling the refresh endpoint :

$ curl -sX POST http://localhost:12345/actuator/refresh
["config.client.version","server.port"]

### Config client log (continuation):
INFO 17028 --- [io-12345-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
INFO 17028 --- [io-12345-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
INFO 17028 --- [io-12345-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
INFO 17028 --- [io-12345-exec-1] o.s.b.context.config.ConfigDataLoader    : Fetching config from server at : http://localhost:8888
INFO 17028 --- [io-12345-exec-1] o.s.b.context.config.ConfigDataLoader    : Located environment: name=config-client, profiles=[default], label=null, version=c92ba03724cb0b06b88a57d19fe79048d830b481, state=null

### (Config client still running on port 12345 - not 8080)
$ curl -sX POST http://localhost:12345/actuator/refresh
[]

Is it possible to make a Spring Cloud Config Client application refresh its configuration dynamically in a way, that it would restart its web server on a different "server.port" that has been read from updated configuration via Spring Cloud Config Server?

With the simplest example project (just one RestController using spring-boot-starter-web) with the "application.properties" below, "/actuator/refresh" works fine for "normal" properties.

Having "server.port: 12345" in the config (config-client-default.yml) provided by the config server, the config client properly starts on port 12345. When I change the configuration file served by the config server (e.g. to "server.port: 8080") and call the "/actuator/refresh" endpoint of the client (e.g. via "curl -sX POST http://localhost:12345/actuator/refresh", I can see that the configuration had been re-read from the config server, but the client does not restart its web server on the new port (8080).

Is it possible to dynamically refresh (reload) "server.port" using "/actuator/refresh", preferably with as little additional code as possible? Thanks in advance.

### Spring Cloud Config Server application.properties :
server.port=8888
spring.cloud.config.server.git.force-pull=true
spring.cloud.config.server.git.uri=file:///D:/DEVEL/local_git
spring.cloud.config.server.git.default-label=master
spring.cloud.config.server.health.enabled=false
### Spring Cloud Config Client application.properties :
management.endpoints.web.exposure.include=*
spring.application.name=config-client
spring.config.import=configserver:http://localhost:8888
### Config client startup :
$ java.exe -jar config-client-0.0.1-SNAPSHOT.jar
:: Spring Boot ::                (v2.6.5)
INFO 17028 --- [main] i.s.c.ConfigClientApplication            : Starting ConfigClientApplication using Java 11.0.14 on PC with PID 17028 (config-client-0.0.1-SNAPSHOT.jar started by Me in D:\DEVEL\config-client)
INFO 17028 --- [main] i.s.c.ConfigClientApplication            : No active profile set, falling back to 1 default profile: "default"
INFO 17028 --- [main] o.s.b.context.config.ConfigDataLoader    : Fetching config from server at : http://localhost:8888
INFO 17028 --- [main] o.s.b.context.config.ConfigDataLoader    : Located environment: name=config-client, profiles=[default], label=null, version=2262dfe9b9ffa387cdb5b3253a0951a084d35a1c, state=null
INFO 17028 --- [main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=6cab5e7d-ad01-347c-8b2d-01847b263bb8
INFO 17028 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 12345 (http)
INFO 17028 --- [main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
INFO 17028 --- [main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.60]
INFO 17028 --- [main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
INFO 17028 --- [main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 735 ms
INFO 17028 --- [main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 15 endpoint(s) beneath base path '/actuator'
INFO 17028 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 12345 (http) with context path ''
INFO 17028 --- [main] i.s.c.ConfigClientApplication            : Started ConfigClientApplication in 1.866 seconds (JVM running for 2.153)
### Changed the client configuration served by the config server :
$ curl -sS http://localhost:8888/config-client/default | json_pp | fgrep 'server.port'
            "server.port" : 8080
### Calling the refresh endpoint :

$ curl -sX POST http://localhost:12345/actuator/refresh
["config.client.version","server.port"]

### Config client log (continuation):
INFO 17028 --- [io-12345-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
INFO 17028 --- [io-12345-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
INFO 17028 --- [io-12345-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
INFO 17028 --- [io-12345-exec-1] o.s.b.context.config.ConfigDataLoader    : Fetching config from server at : http://localhost:8888
INFO 17028 --- [io-12345-exec-1] o.s.b.context.config.ConfigDataLoader    : Located environment: name=config-client, profiles=[default], label=null, version=c92ba03724cb0b06b88a57d19fe79048d830b481, state=null

### (Config client still running on port 12345 - not 8080)
$ curl -sX POST http://localhost:12345/actuator/refresh
[]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文