Spring Cloud配置客户端在启动后立即关闭

huangapple go评论81阅读模式
英文:

Spring cloud config client shutdown just after startup

问题

我已经实现了一个简单的应用程序,以便了解如何为客户端使用 Spring Cloud Config。

它仅包含两个类:

主类:

@SpringBootApplication
public class ClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ClientApplication.class, args);
    }
}

控制器

@RefreshScope
@RestController
public class ExampleController {

    @Value("${example.message:none}")
    private String message;

    @GetMapping("/message")
    public String getMessage() {
        return this.message;
    }
}

以及 application.properties 文件:

server.port = 8080
# Customize the Pub/Sub Topic to be used as the message bus.
spring.cloud.gcp.project-id=spring-samples-269912

spring.cloud.bus.destination=cloud-bus-demo-topic

spring.cloud.config.uri=http://35.202.199.184
spring.cloud.gcp.credentials.location=file:secret.json

build.gradle

// ... 省略了前面的内容

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.springframework.cloud:spring-cloud-gcp-starter-bus-pubsub'
    implementation 'org.springframework.cloud:spring-cloud-config-client'
}

// ... 省略了后面的内容

但当我运行时,我看到以下内容:

// ... 省略了输出的大部分内容

Application shutdowns just after startup. Config server works well because [http://35.202.199.184/example.message/default][1] (this URL is public available) responds with correct response.

How to fix it?

如何修复此问题?

英文:

I've implemented simple application to understand how spring cloud config for client.

It is just contains 2 classes:

Main:

@SpringBootApplication
public class ClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ClientApplication.class, args);
    }
}

and controller:

@RefreshScope
@RestController
public class ExampleController {

    @Value("${example.message:none}")
    private String message;

    @GetMapping("/message")
    public String getMessage() {
        return this.message;
    }

}

and application.properies file:

server.port = 8080
# Customize the Pub/Sub Topic to be used as the message bus.
spring.cloud.gcp.project-id=spring-samples-269912

spring.cloud.bus.destination=cloud-bus-demo-topic

spring.cloud.config.uri=http://35.202.199.184
spring.cloud.gcp.credentials.location=file:secret.json

build.gradle:

buildscript {      // Configuration for building
    repositories {
        jcenter()      // Bintray's repository - a fast Maven Central mirror & more
        mavenCentral()
    }
    dependencies {
        classpath 'com.google.cloud.tools:appengine-gradle-plugin:2.2.0' // Latest 1.x.x release
    }
}

plugins {
    id 'org.springframework.boot' version '2.2.5.RELEASE'
    id 'java'
    id 'com.google.cloud.tools.jib' version '2.1.0'
}

apply plugin: 'java'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'com.google.cloud.tools.appengine'

group = 'my.config.server'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
    mavenCentral()
    maven { url "https://repo.spring.io/milestone/" }

}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.springframework.cloud:spring-cloud-gcp-starter-bus-pubsub'
    implementation 'org.springframework.cloud:spring-cloud-config-client'
}

test {
    useJUnitPlatform()
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:Hoxton.SR1"
        mavenBom "org.springframework.cloud:spring-cloud-gcp-dependencies:1.2.1.RELEASE"
    }
}
appengine {
    deploy {   // deploy configuration
        stopPreviousVersion = true  // default - stop the current version
        promote = true              // default - & make this the current version
        projectId = 'spring-samples-269912' // delegate to project in gcloud config
        version = 'GCLOUD_CONFIG'   // delegate to gcloud to generate a version
    }

}

jib {
    to {
        image = "us.gcr.io/spring-samples-269912/config-client-image"
        auth {
            username = "_json_key"
            password = file("secret.json").text
        }
    }
}

But when I run I see following:

  .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/  ___)| |_)| | | | | || (_| |  ) ) ) )
'  |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot ::        (v2.2.5.RELEASE)
2020-03-16 16:13:59.255  INFO 8948 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8888
2020-03-16 16:13:59.995  INFO 8948 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=application, profiles=[default], label=null, version=abd4f2a3e1193705ae692a7217db894772525277, state=null
2020-03-16 16:13:59.995  INFO 8948 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: [BootstrapPropertySource {name='bootstrapProperties-configClient'}, BootstrapPropertySource {name='bootstrapProperties-git://github.com/gredwhite/config-repo-demo.git/application.properties'}]
2020-03-16 16:13:59.999  INFO 8948 --- [           main] my.config.client.ClientApplication       : No active profile set, falling back to default profiles: default
2020-03-16 16:14:00.347  INFO 8948 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=30c969b5-0fdd-3bcc-953b-09ef7014e64c
2020-03-16 16:14:00.364  INFO 8948 --- [           main] faultConfiguringBeanFactoryPostProcessor : No bean named 'errorChannel' has been explicitly defined. Therefore, a default PublishSubscribeChannel will be created.
2020-03-16 16:14:00.367  INFO 8948 --- [           main] faultConfiguringBeanFactoryPostProcessor : No bean named 'taskScheduler' has been explicitly defined. Therefore, a default ThreadPoolTaskScheduler will be created.
2020-03-16 16:14:00.370  INFO 8948 --- [           main] faultConfiguringBeanFactoryPostProcessor : No bean named 'integrationHeaderChannelRegistry' has been explicitly defined. Therefore, a default DefaultHeaderChannelRegistry will be created.
2020-03-16 16:14:00.384  INFO 8948 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.integration.config.IntegrationManagementConfiguration' of type [org.springframework.integration.config.IntegrationManagementConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-03-16 16:14:00.385  INFO 8948 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration$IntegrationJmxConfiguration' of type [org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration$IntegrationJmxConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-03-16 16:14:00.390  INFO 8948 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration' of type [org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-03-16 16:14:00.393  INFO 8948 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'mbeanServer' of type [com.sun.jmx.mbeanserver.JmxMBeanServer] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-03-16 16:14:00.399  INFO 8948 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'integrationChannelResolver' of type [org.springframework.integration.support.channel.BeanFactoryChannelResolver] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-03-16 16:14:00.399  INFO 8948 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'integrationDisposableAutoCreatedBeans' of type [org.springframework.integration.config.annotation.Disposables] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-03-16 16:14:01.400  INFO 8948 --- [           main] o.s.c.g.core.DefaultCredentialsProvider  : Default credentials provider for service account spring-samples-service-account@spring-samples-269912.iam.gserviceaccount.com
2020-03-16 16:14:01.400  INFO 8948 --- [           main] o.s.c.g.core.DefaultCredentialsProvider  : Scopes in use by default credentials: [https://www.googleapis.com/auth/pubsub, https://www.googleapis.com/auth/spanner.admin, https://www.googleapis.com/auth/spanner.data, https://www.googleapis.com/auth/datastore, https://www.googleapis.com/auth/sqlservice.admin, https://www.googleapis.com/auth/devstorage.read_only, https://www.googleapis.com/auth/devstorage.read_write, https://www.googleapis.com/auth/cloudruntimeconfig, https://www.googleapis.com/auth/trace.append, https://www.googleapis.com/auth/cloud-platform, https://www.googleapis.com/auth/cloud-vision, https://www.googleapis.com/auth/bigquery]
2020-03-16 16:14:01.401  INFO 8948 --- [           main] o.s.c.g.a.c.GcpContextAutoConfiguration  : The default project ID is spring-samples-269912
2020-03-16 16:14:01.409  INFO 8948 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService 'pubsubPublisherThreadPool'
2020-03-16 16:14:01.411  INFO 8948 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService 'pubsubSubscriberThreadPool'
2020-03-16 16:14:01.428  INFO 8948 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'pubSubAcknowledgementExecutor'
2020-03-16 16:14:01.811  INFO 8948 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService 'taskScheduler'
2020-03-16 16:14:01.908  INFO 8948 --- [           main] o.s.i.monitor.IntegrationMBeanExporter   : Registering MessageChannel errorChannel
2020-03-16 16:14:01.948  INFO 8948 --- [           main] o.s.i.monitor.IntegrationMBeanExporter   : Registering MessageChannel springCloudBusInput
2020-03-16 16:14:01.966  INFO 8948 --- [           main] o.s.i.monitor.IntegrationMBeanExporter   : Registering MessageChannel springCloudBusOutput
2020-03-16 16:14:01.972  INFO 8948 --- [           main] o.s.i.monitor.IntegrationMBeanExporter   : Registering MessageChannel nullChannel
2020-03-16 16:14:01.981  INFO 8948 --- [           main] o.s.i.monitor.IntegrationMBeanExporter   : Registering MessageHandler errorLogger
2020-03-16 16:14:02.000  INFO 8948 --- [           main] o.s.c.s.m.DirectWithAttributesChannel    : Channel 'application-1.springCloudBusInput' has 1 subscriber(s).
2020-03-16 16:14:02.001  INFO 8948 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : Adding {logging-channel-adapter:_org.springframework.integration.errorLogger} as a subscriber to the 'errorChannel' channel
2020-03-16 16:14:02.001  INFO 8948 --- [           main] o.s.i.channel.PublishSubscribeChannel    : Channel 'application-1.errorChannel' has 1 subscriber(s).
2020-03-16 16:14:02.001  INFO 8948 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : started bean '_org.springframework.integration.errorLogger'
2020-03-16 16:14:02.380  INFO 8948 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://35.202.199.184
2020-03-16 16:14:03.015  INFO 8948 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=application, profiles=[default], label=null, version=abd4f2a3e1193705ae692a7217db894772525277, state=null
2020-03-16 16:14:03.015  INFO 8948 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: [BootstrapPropertySource {name='bootstrapProperties-configClient'}, BootstrapPropertySource {name='bootstrapProperties-git://github.com/gredwhite/config-repo-demo.git/application.properties'}]
2020-03-16 16:14:04.268  INFO 8948 --- [           main] o.s.c.s.m.DirectWithAttributesChannel    : Channel 'application-1.springCloudBusOutput' has 1 subscriber(s).
2020-03-16 16:14:10.152  INFO 8948 --- [           main] o.s.c.stream.binder.BinderErrorChannel   : Channel 'anonymous.cloud-bus-demo-topic.0c7beefc-5850-4cb5-8b1d-3779305910eb.errors' has 1 subscriber(s).
2020-03-16 16:14:10.153  INFO 8948 --- [           main] o.s.c.stream.binder.BinderErrorChannel   : Channel 'anonymous.cloud-bus-demo-topic.0c7beefc-5850-4cb5-8b1d-3779305910eb.errors' has 0 subscriber(s).
2020-03-16 16:14:10.153  INFO 8948 --- [           main] o.s.c.stream.binder.BinderErrorChannel   : Channel 'anonymous.cloud-bus-demo-topic.0c7beefc-5850-4cb5-8b1d-3779305910eb.errors' has 1 subscriber(s).
2020-03-16 16:14:10.153  INFO 8948 --- [           main] o.s.c.stream.binder.BinderErrorChannel   : Channel 'anonymous.cloud-bus-demo-topic.0c7beefc-5850-4cb5-8b1d-3779305910eb.errors' has 2 subscriber(s).
2020-03-16 16:14:10.179  INFO 8948 --- [           main] .s.c.g.p.i.i.PubSubInboundChannelAdapter : started org.springframework.cloud.gcp.pubsub.integration.inbound.PubSubInboundChannelAdapter@10b67f54
2020-03-16 16:14:10.196  INFO 8948 --- [           main] my.config.client.ClientApplication       : Started ClientApplication in 12.181 seconds (JVM running for 13.372)
2020-03-16 16:14:10.201  INFO 8948 --- [extShutdownHook] .s.c.g.p.i.i.PubSubInboundChannelAdapter : stopped org.springframework.cloud.gcp.pubsub.integration.inbound.PubSubInboundChannelAdapter@10b67f54
2020-03-16 16:14:13.941  INFO 8948 --- [extShutdownHook] o.s.i.monitor.IntegrationMBeanExporter   : Registering MessageChannel anonymous.cloud-bus-demo-topic.0c7beefc-5850-4cb5-8b1d-3779305910eb.errors
2020-03-16 16:14:13.990  INFO 8948 --- [extShutdownHook] o.s.c.stream.binder.BinderErrorChannel   : Channel 'application-1.anonymous.cloud-bus-demo-topic.0c7beefc-5850-4cb5-8b1d-3779305910eb.errors' has 1 subscriber(s).
2020-03-16 16:14:13.990  INFO 8948 --- [extShutdownHook] o.s.c.stream.binder.BinderErrorChannel   : Channel 'application-1.anonymous.cloud-bus-demo-topic.0c7beefc-5850-4cb5-8b1d-3779305910eb.errors' has 0 subscriber(s).
2020-03-16 16:14:13.991  INFO 8948 --- [extShutdownHook] o.s.i.endpoint.EventDrivenConsumer       : Removing {logging-channel-adapter:_org.springframework.integration.errorLogger} as a subscriber to the 'errorChannel' channel
2020-03-16 16:14:13.991  INFO 8948 --- [extShutdownHook] o.s.i.channel.PublishSubscribeChannel    : Channel 'application-1.errorChannel' has 0 subscriber(s).
2020-03-16 16:14:13.991  INFO 8948 --- [extShutdownHook] o.s.i.endpoint.EventDrivenConsumer       : stopped bean '_org.springframework.integration.errorLogger'
2020-03-16 16:14:13.991  INFO 8948 --- [extShutdownHook] o.s.s.c.ThreadPoolTaskScheduler          : Shutting down ExecutorService 'taskScheduler'
2020-03-16 16:14:13.994  INFO 8948 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'pubSubAcknowledgementExecutor'
2020-03-16 16:14:13.994  INFO 8948 --- [extShutdownHook] o.s.s.c.ThreadPoolTaskScheduler          : Shutting down ExecutorService 'pubsubSubscriberThreadPool'
2020-03-16 16:14:13.995  INFO 8948 --- [extShutdownHook] o.s.s.c.ThreadPoolTaskScheduler          : Shutting down ExecutorService 'pubsubPublisherThreadPool'
2020-03-16 16:14:13.996  INFO 8948 --- [extShutdownHook] o.s.i.monitor.IntegrationMBeanExporter   : Summary on shutdown: bean 'errorChannel'
2020-03-16 16:14:13.996  INFO 8948 --- [extShutdownHook] o.s.i.monitor.IntegrationMBeanExporter   : Summary on shutdown: bean 'springCloudBusInput'
2020-03-16 16:14:13.996  INFO 8948 --- [extShutdownHook] o.s.i.monitor.IntegrationMBeanExporter   : Summary on shutdown: bean 'springCloudBusOutput'
2020-03-16 16:14:13.996  INFO 8948 --- [extShutdownHook] o.s.i.monitor.IntegrationMBeanExporter   : Summary on shutdown: nullChannel
2020-03-16 16:14:13.996  INFO 8948 --- [extShutdownHook] o.s.i.monitor.IntegrationMBeanExporter   : Summary on shutdown: bean '_org.springframework.integration.errorLogger.handler' for component '_org.springframework.integration.errorLogger'

Application shutdowns just after startup. Config server works well because http://35.202.199.184/example.message/default (this URL is public available) responds with correct response.

How to fix it?

答案1

得分: 5

我遇到了相同的问题,通过按照这里发布的答案1进行操作,我的问题得以解决。原因是您没有在项目中添加Spring Starter Web依赖项,因此没有为应用程序创建Web服务器实例,这将保持应用程序保持活动状态,因此在向发现服务器注册自身后,它的工作就完成了,并且关闭了应用程序。
只需在您的build.gradle文件中添加以下内容:

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
//其他依赖项
}
英文:

I had the same issue and by following the answer posted here my issue was resolved. The reason is that you didn't add the Spring Starter Web dependency in your project so there was no web server instance created for your application, which will keep the application alive, hence after registering itself with the discovery server its job is done and it shut down the application.
Just add the following lines in your build.gradle file: -

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
//other dependencies
}

huangapple
  • 本文由 发表于 2020年3月16日 21:24:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/60706841.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定