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

Spring cloud config client shutdown just after startup

我已经实现了简单的应用程序来了解如何 spring 客户端的云配置。

它只包含 2 类:

主线:

@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.properies文件:

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
        }
    }
}

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

  .   ____          _            __ _ _
 /\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: 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'

应用程序在启动后立即关闭。配置服务器运行良好,因为 http://35.202.199.184/example.message/default(此 URL 可用 public)以正确的响应响应。

如何解决?

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

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