Spring云配置客户端位于服务器:属性未加载

amrnrhlw  于 2023-01-02  发布在  Spring
关注(0)|答案(1)|浏览(125)

我确信这是某种启动/云版本依赖性问题,但我无法找出依赖性和属性值的组合。
配置服务器启动,属性可在浏览器本地主机:8888/master/ucdp-ingest本地主机:8888/master/ucdp-ingest-dev中获得

$ java --version
openjdk 11.0.14.1 2022-02-08 LTS
OpenJDK Runtime Environment Corretto-11.0.14.10.1 (build 11.0.14.1+10-LTS)
OpenJDK 64-Bit Server VM Corretto-11.0.14.10.1 (build 11.0.14.1+10-LTS, mixed mode)

因此,根据2021-0-3 Availability和我们使用的java版本,使用

  • Spring Boot:2.7.6
  • Spring Cloud:2021.0.3

客户端pom

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.6</version>
        <relativePath/>
    </parent>
    <properties>
        <aws-sdk.version>1.12.368</aws-sdk.version>
        <commons-lang3.version>3.12.0</commons-lang3.version>
        <jacoco.version>0.8.8</jacoco.version>
        <jasypt-spring-boot.version>3.0.5</jasypt-spring-boot.version>
        <java.version>11</java.version>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <spring-cloud.version>2021.0.3</spring-cloud.version>
        <spring-cloud-stream-kinesis-binder.version>2.2.0</spring-cloud-stream-kinesis-binder.version>
        <springdoc-openapi-ui.version>1.6.14</springdoc-openapi-ui.version>
        <swagger-maven-plugin.version>2.1.6</swagger-maven-plugin.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- Spring cloud -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

请注意,每个文档中没有"spring-cloud-starter-bootstrap"。有趣的是,"spring.profiles. active"显示已弃用。然而,根据Spring 2.4 docs,它应该是spring.config.activate.on-profile。这不会解析为系统属性。

-Dspring.config.activate.on-profile=dev
2022-12-23 15:28:51.358  INFO 36316 --- [           main] c.b.u.ucdp.ingest.UcdpIngestApplication  : No active profile set, falling back to 1 default profile: "default"

客户端:应用程序. yml

spring:
  application:
    name: ucdp-ingest
  cloud:
    config:
      name: ${spring.application.name}
      uri: http://localhost:8888
      username: ----------
      password: ---------
      request-connect-timeout: 3000
      request-read-timeout: 3000
      retry:
        initial-interval: 1000
        max-attempts: 6
      label: master
      fail-fast: true
  config:
    import: "configserver:"

server:
  port: 8300
  servlet:
    context-path: /${spring.application.name}

  # Actuator
  management:
    #opt-in use enabled-by-default: false then enable what you want. default = true
    enabled-by-default: true
    server:
      port: ${server.port}
    endpoints:
      web:
        base-path: ${server.servlet.context-path}
        exposure:
          include: "*"
    endpoint:
      env:
        enabled: true
      health:
        show-details: always
        enabled: true
      info:
        enabled: true
#      shutdown:
#        enabled: true

#OpenAPI
springdoc:
  api-docs:
    path: /docs/api-docs
    version: openapi_3_1
  swagger-ui:
    path: /docs/swagger-ui
  paths-to-match: /**

远程应用程序-ucdp-摄取. yml

app:
  ucdp:
    url: https://ucdpapi.pcr.uu.se/api
    connect-timeout-millis: 2000
    read-timeout-seconds: 2000
  ged:
    job:
      enabled: true
      cron: "20 49 11 * * ?"
    #Max page size is 1000
    pageSize: 1000
    uri: /gedevents/21.1

REMOTE application-ucdp-ingest-dev.yml包含了mongo和aws连接属性。由于属性没有远程加载,Mongo连接失败。
日志:注意我将概要文件作为系统属性传递。

+ /c/sfw/java/jdk11.0.14_10/bin/java -Dspring.config.activate.on-profile=dev -Dlogging.config=/c/projects/ucdp-ingest/ucdp-ingest-app/config/logback.xml -jar /c/projects/ucdp-ingest/ucdp-ingest-app/target/ucdp-ingest-app-0.0.1-SNAPSHOT.jar

2022-12-23 15:30:50.081  INFO 20496 --- [           main] c.b.u.ucdp.ingest.UcdpIngestApplication  : Starting UcdpIngestApplication v0.0.1-SNAPSHOT using Java 11.0.14.1 on BRANDA-BSTAQ-PC with PID 20496 (C:\projects\ucdp-ingest\ucdp-ingest-app\target\ucdp-ingest-app-0.0.1-SNAPSHOT.jar started by BruceRandall in C:\projects\ucdp-ingest\ucdp-ingest-app)
2022-12-23 15:30:50.086  INFO 20496 --- [           main] c.b.u.ucdp.ingest.UcdpIngestApplication  : The following 1 profile is active: "dev"
2022-12-23 15:30:50.136  INFO 20496 --- [           main] o.s.c.c.c.ConfigServerConfigDataLoader   : Fetching config from server at : http://localhost:8888
2022-12-23 15:30:50.137  INFO 20496 --- [           main] o.s.c.c.c.ConfigServerConfigDataLoader   : Located environment: name=ucdp-ingest, profiles=[dev], label=master, version=7102644c36175df62e3a3952793021cdf730583a, state=null
2022-12-23 15:30:51.495  INFO 20496 --- [           main] ptablePropertiesBeanFactoryPostProcessor : Post-processing PropertySource instances
2022-12-23 15:30:51.496  INFO 20496 --- [           main] c.u.j.EncryptablePropertySourceConverter : Skipping PropertySource configurationProperties [class org.springframework.boot.context.properties.source.ConfigurationPropertySourcesPropertySource
2022-12-23 15:30:51.498  INFO 20496 --- [           main] c.u.j.EncryptablePropertySourceConverter : Skipping PropertySource servletConfigInitParams [class org.springframework.core.env.PropertySource$StubPropertySource
2022-12-23 15:30:51.498  INFO 20496 --- [           main] c.u.j.EncryptablePropertySourceConverter : Skipping PropertySource servletContextInitParams [class org.springframework.core.env.PropertySource$StubPropertySource
2022-12-23 15:30:51.499  INFO 20496 --- [           main] c.u.j.EncryptablePropertySourceConverter : Converting PropertySource systemProperties [org.springframework.core.env.PropertiesPropertySource] to EncryptableMapPropertySourceWrapper
2022-12-23 15:30:51.499  INFO 20496 --- [           main] c.u.j.EncryptablePropertySourceConverter : Converting PropertySource systemEnvironment [org.springframework.boot.env.SystemEnvironmentPropertySourceEnvironmentPostProcessor$OriginAwareSystemEnvironmentPropertySource] to EncryptableSystemEnvironmentPropertySourceWrapper
2022-12-23 15:30:51.500  INFO 20496 --- [           main] c.u.j.EncryptablePropertySourceConverter : Converting PropertySource random [org.springframework.boot.env.RandomValuePropertySource] to EncryptablePropertySourceWrapper
2022-12-23 15:30:51.500  INFO 20496 --- [           main] c.u.j.EncryptablePropertySourceConverter : Converting PropertySource cachedrandom [org.springframework.cloud.util.random.CachedRandomPropertySource] to EncryptablePropertySourceWrapper
2022-12-23 15:30:51.500  INFO 20496 --- [           main] c.u.j.EncryptablePropertySourceConverter : Converting PropertySource springCloudClientHostInfo [org.springframework.core.env.MapPropertySource] to EncryptableMapPropertySourceWrapper
2022-12-23 15:30:51.500  INFO 20496 --- [           main] c.u.j.EncryptablePropertySourceConverter : Converting PropertySource configClient [org.springframework.core.env.MapPropertySource] to EncryptableMapPropertySourceWrapper
2022-12-23 15:30:51.500  INFO 20496 --- [           main] c.u.j.EncryptablePropertySourceConverter : Converting PropertySource Config resource 'file [config\application.yml]' via location 'optional:file:./config/' [org.springframework.boot.env.OriginTrackedMapPropertySource] to EncryptableMapPropertySourceWrapper

2022-12-23 15:30:51.664  INFO 20496 --- [           main] c.u.j.filter.DefaultLazyPropertyFilter   : Property Filter custom Bean not found with name 'encryptablePropertyFilter'. Initializing Default Property Filter
2022-12-23 15:30:51.673  INFO 20496 --- [           main] c.u.j.r.DefaultLazyPropertyResolver      : Property Resolver custom Bean not found with name 'encryptablePropertyResolver'. Initializing Default Property Resolver
2022-12-23 15:30:51.675  INFO 20496 --- [           main] c.u.j.d.DefaultLazyPropertyDetector      : Property Detector custom Bean not found with name 'encryptablePropertyDetector'. Initializing Default Property Detector
2022-12-23 15:30:52.246  INFO 20496 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8300 (http)
2022-12-23 15:30:52.256  INFO 20496 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-12-23 15:30:52.256  INFO 20496 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.69]
2022-12-23 15:30:52.351  INFO 20496 --- [           main] o.a.c.c.C.[.[localhost].[/ucdp-ingest]   : Initializing Spring embedded WebApplicationContext
2022-12-23 15:30:52.351  INFO 20496 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2212 ms

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'gedJob' defined in URL [jar:file:/C:/projects/ucdp-ingest/ucdp-ingest-app/target/ucdp-ingest-app-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/com/bar/udl/ucdp/ingest/service/impl/GedJob.class]: Initialization of bean failed; nested exception is java.lang.IllegalStateException: Encountered invalid @Scheduled method 'executeJob': 
Could not resolve placeholder 'app.ged.job.cron' in value "${app.ged.job.cron}"
ConfigServerConfigDataLoader   : Located environment: name=ucdp-ingest, profiles=[dev], label=master, version=7102644c36175df62e3a3952793021cdf730583a, state=null
Could not resolve placeholder 'app.ged.job.cron' in value "${app.ged.job.cron}"

遗憾的是,ConfigServerConfigDataLoader不会提供完整的URL
配置服务器终结点http://localhost:8888/master/ucdp-ingest

{
  "name": "master",
  "profiles": [
    "ucdp-ingest"
  ],
  "label": null,
  "version": "155b907870c49c194dee1f1b6d94be768ce043ec",
  "state": null,
  "propertySources": [
    {
      "name": "C:\\\\projects\\\\udl-app-config/file:C:\\Users\\BRUCER~1\\AppData\\Local\\Temp\\config-repo-1334124828626900950\\application-ucdp-ingest.yml",
      "source": {
        "app.ucdp.url": "https://ucdpapi.pcr.uu.se/api",
        "app.ucdp.connect-timeout-millis": 2000,
        "app.ucdp.read-timeout-seconds": 2000,
        "app.ged.job.enabled": true,
        "app.ged.job.cron": "20 49 11 * * ?",
        "app.ged.pageSize": 1000,
        "app.ged.uri": "/gedevents/21.1",
      }
    }
  ]
xqnpmsa8

xqnpmsa81#

恢复到 Boot 2.4.3,云2020.0.3,但将恢复到启动2.7.6云2021.0.3。
主要问题是错误地解释了属性文件命名约定的规范。我注意到当查询配置服务器时,名称是分支,而概要文件是应用程序名称。因此,我只能使用localhost:8888/master/app-name-profile提取属性
一旦我将application从git存储库中的文件名中移到${spring.application.name}-profile中,我就可以正确地查询服务器,客户端也可以连接并加载属性。
我的最后一个问题是我不能将概要文件作为系统属性传入-当bean访问配置服务器时,Dspring.profiles.active=dev解析为默认值。

相关问题