简体   繁体   English

无法在 Spring 云配置服务器中从 GIT 读取属性源

[英]Unable to read property source from GIT in Spring cloud config server

I'm not able to read a property from in ,我无法从中的读取属性,

I have two application config-client and config-server, from config-client I want to read properties which is present in config-server repository.我有两个应用程序配置客户端和配置服务器,我想从配置客户端读取配置服务器存储库中存在的属性。

Kindly find my code below:请在下面找到我的代码:

config-client配置客户端

ConfigClientApplication.java ConfigClientApplication.java

@SpringBootApplication
public class ConfigClientApplication {

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

MessageResource.java消息资源.java

@RestController
@RequestMapping("/rest")
public class MessageResource {

    @Value("${message: Defalut Hello}")
    private String message;

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

}

bootstrap.properties bootstrap.properties

spring.application.name=config-client
server.port=8091
management.endpoints.web.exposure.include=*
spring.cloud.config.uri=http://localhost:8888

pom.xml pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.bhaskar.shopping</groupId>
    <artifactId>config-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>config-client</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.M3</spring-cloud.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>


</project>

config-server配置服务器

ConfigServerApplication.java ConfigServerApplication.java

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

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

bootstrap.properties bootstrap.properties

spring.config.name=configserver
server.port = 8888
spring.cloud.config.server.git.uri=https://github.com/BijayaBhaskar/micro-service-config-server
spring.cloud.config.server.git.clone-on-start=true

pom.xml pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.bhaskar.shopping</groupId>
    <artifactId>config-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>config-server</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.M3</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>


</project>

config-client.properties配置-client.properties

message = Hello World

I have committed this file in defined git URL., but I'm unable to get message property in my rest API.我已经在定义的 git URL 中提交了这个文件,但是我无法在我的 rest API 中获取消息属性。

While i start config-client application, it hits the config-server URL but returns defaut property,当我启动配置客户端应用程序时,它会访问配置服务器 URL 但会返回默认属性,

Here is my client and server logs.这是我的客户端和服务器日志。

config-client log配置客户端日志

 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.1.RELEASE)

2018-12-07 17:26:36.613  INFO 9336 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8888
2018-12-07 17:26:38.402  INFO 9336 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=config-cilent, profiles=[default], label=null, version=a611374438e75aa1b9808908c57833480944e1a8, state=null
2018-12-07 17:26:38.402  INFO 9336 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='configClient'}, MapPropertySource {name='https://github.com/spring-cloud-samples/config-repo/application.yml (document #0)'}]}
2018-12-07 17:26:38.402  INFO 9336 --- [           main] c.b.shopping.ConfigClientApplication     : No active profile set, falling back to default profiles: default
2018-12-07 17:26:38.992  INFO 9336 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=e4575a9f-f04b-3f9f-93b8-2da140e2fcba
2018-12-07 17:26:39.002  INFO 9336 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$11855985] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-12-07 17:26:39.223  INFO 9336 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8091 (http)
2018-12-07 17:26:39.243  INFO 9336 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-12-07 17:26:39.243  INFO 9336 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/9.0.13
2018-12-07 17:26:39.253  INFO 9336 --- [           main] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jre8\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files\TortoiseSVN\bin;C:\Program Files (x86)\MySQL\MySQL Fabric 1.5 & MySQL Utilities 1.5\;C:\Program Files (x86)\MySQL\MySQL Fabric 1.5 & MySQL Utilities 1.5\Doctrine extensions for PHP\;C:\Program Files\nodejs\;D:\Spott_Software\apache-maven-3.5.3\bin;C:\Program Files\Java\jdk1.8.0_11\bin;C:\Program Files\PuTTY\;C:\Program Files\Git\cmd;C:\Users\bijayas\AppData\Local\Programs\Python\Python37\Scripts\;C:\Users\bijayas\AppData\Local\Programs\Python\Python37\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files\Microsoft VS Code\bin;C:\Users\bijayas\AppData\Roaming\npm;.]
2018-12-07 17:26:39.493  INFO 9336 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-12-07 17:26:39.493  INFO 9336 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1081 ms
2018-12-07 17:26:39.674  INFO 9336 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2018-12-07 17:26:40.247  INFO 9336 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8091 (http) with context path ''
2018-12-07 17:26:40.247  INFO 9336 --- [           main] c.b.shopping.ConfigClientApplication     : Started ConfigClientApplication in 4.989 seconds (JVM running for 5.806)

config-server log配置服务器日志

2018-12-07 17:26:36.749  INFO 7320 --- [nio-8888-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2018-12-07 17:26:36.749  INFO 7320 --- [nio-8888-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2018-12-07 17:26:36.759  INFO 7320 --- [nio-8888-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 10 ms
2018-12-07 17:26:38.301  INFO 7320 --- [nio-8888-exec-1] o.s.c.c.s.e.NativeEnvironmentRepository  : Adding property source: file:/C:/Users/bijayas/AppData/Local/Temp/config-repo-9103792242838281221/application.yml (document #0)

Here i can see it's loading from diferent directory not from GIT repository defined in bootstrap.properties, Kindly help me on this.在这里我可以看到它是从不同的目录加载的,而不是从 bootstrap.properties 中定义的 GIT 存储库加载的,请帮助我解决这个问题。

Thanks in advance.提前致谢。

It's missing to add on your repository a config-client.properties, because you have one called catalog-service.properties.缺少在您的存储库中添加 config-client.properties,因为您有一个名为 catalog-service.properties 的文件。

If this helps you, please let me know it.如果这对您有帮助,请告诉我。

UPDATED更新

In addition you need to update a couple of things.此外,您还需要更新一些内容。

config-server配置服务器

According documentation the config name has to be the same name of project.根据文档,配置名称必须与项目名称相同。

spring.config.name=config-server

config-client配置客户端

rename application.properties to bootstrap.properties because when you put bootstrap, the properties are read before application context.将 application.properties 重命名为 bootstrap.properties 因为当你放置 bootstrap 时,属性在应用程序上下文之前被读取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用 spring-cloud-config-server 从本地 git 存储库读取配置 - Unable to read configurations from local git repository using spring-cloud-config-server Spring Cloud Config-自定义Bootstrap上下文以从另一个属性源读取配置URI - Spring Cloud Config- Customizing the Bootstrap context to read config URI from another property Source Spring 云配置服务器无法读取属性文件 - Spring cloud config server not able to read property file Spring 云配置服务器未从 git 提取最新提交 - Spring cloud Config server not pulling latest commit from git 从 spring 引导 2.4 中的配置服务器读取属性 - Read property from config server in spring boot 2.4 Spring:无法将@property 读入 config.xml - Spring:Unable to read @property into config.xml 属性“spring.cloud.config.server.git.privateKey”不是有效的私钥 - Property 'spring.cloud.config.server.git.privateKey' is not a valid private key 无法使用非对称加密解密Spring Cloud配置中的配置属性 - Unable to decrypt config property in Spring Cloud config using asymmetric encryption 从 Spring 云配置服务器从 GIT 存储库获取文件名列表 - Getting list of file names from GIT repo from Spring cloud config server Spring未知属性&#39;spring.cloud.config.server&#39; - Spring unknown property 'spring.cloud.config.server'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM