【问题标题】:spring config server not loading native propertiesspring 配置服务器未加载本机属性
【发布时间】:2016-01-17 23:20:47
【问题描述】:

我正在尝试让一个示例应用程序为自己工作,因为我刚刚开始使用 Spring Boot。我已经创建了我的 restful 服务,下一步是让配置服务器运行,以便我可以动态刷新属性。但是我的配置服务器似乎没有加载属性。所以我有以下设置:

配置服务器 /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.cloud</groupId>
        <artifactId>spring-cloud-starter-parent</artifactId>
        <version>1.0.2.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <artifactId>config-service</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

    </dependencies>
</project>

配置服务器/应用程序类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableAutoConfiguration
@EnableConfigServer
public class ConfigurationServerApplication {

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

配置服务器/application.yml

server:
  port: ${PORT:8889}
logging:
  levels:
    org.springframework.boot.env.PropertySourcesLoader: TRACE
    org.springframework.web: DEBUG
spring:
  cloud:
    config:
      server:
        native:
          searchLocations:  classpath:/config
  profiles:
    active: native 
#conf: /Users/jlong/work/bootiful/bootiful-microservices/code/spring-doge-microservice-configuration



#my:
 # property:******************myvalue***************************

在 src/main/resources 我有一个配置目录,其中有一个 hello-service.properties 文件,其中包含以下内容:

very-so-much=testing

配置服务器启动正常,并且正在监听端口 8889,如日志所示

2015-10-19 15:10:32.490  INFO 7712 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Located managed bean 'configurationPropertiesRebinder': registering with JMX server as MBean [org.springframework.cloud.context.properties:name=configurationPropertiesRebinder,type=ConfigurationPropertiesRebinder]
2015-10-19 15:10:32.499  INFO 7712 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Located managed bean 'refreshEndpoint': registering with JMX server as MBean [org.springframework.cloud.bootstrap.config:name=refreshEndpoint,type=RefreshEndpoint]
2015-10-19 15:10:32.504  INFO 7712 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 0
2015-10-19 15:10:32.620  INFO 7712 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8889 (http)
2015-10-19 15:10:32.622  INFO 7712 --- [           main] doge.ConfigurationServerApplication      : Started ConfigurationServerApplication in 4.352 seconds (JVM running for 5.069)

我还有一个 Spring Boot RESTful 服务,我想加载配置服务器维护的属性。 RESTful应用如下:

hello-service/pom.xml

<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>
    <groupId>org.sample.springboot</groupId>
    <artifactId>spring-restful-tutorial</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-restful-tutorial</name>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.6.RELEASE</version>
    </parent>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-parent</artifactId>
                <version>1.0.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

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

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

        <!-- cloud config server -->
        <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>



    <properties>
        <java.version>1.8</java.version>
    </properties>


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

    <repositories>
        <repository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>
</project>

我的 hello-service/app.class -- 为了清楚起见省略了一些代码

@SpringBootApplication
public class Application implements CommandLineRunner {

    @Autowired
    private CustomerRepository repository;

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

    @Bean
    @RefreshScope
    CustomerProps customerProps( @Value("${very-so-much}") String exclamations) {
        CustomerProps p = new CustomerProps();
        p.setText(exclamations);
        return p;
    }

hello-service /restful 服务,为了清楚起见,我尝试加载属性省略代码

@RestController
@RequestMapping("/customer")
public class GreetingController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    //TODO switch to autowired annotation
    private CustomerRepository repository;
    private final GridFsTemplate fs;
    private CustomerProps props;

    @Autowired
    public GreetingController(CustomerRepository repository, GridFsTemplate gridFileSystem, CustomerProps props) {
        this.repository = repository;
        this.fs = gridFileSystem;
        this.props = props;
    }

    @RequestMapping("/greeting")
    public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
        name = props.getText();
        return new Greeting(counter.incrementAndGet(),
                            String.format(template, name));
    }

还有我的 application.properties

server.port=${PORT:9001}    
spring.profiles.active=native

bootstrap.properties

spring.application.name=hello-service
spring.cloud.config.uri=http://localhost:8889

所以当我点击 restful 端点问候时,它会抛出如下异常

2015-10-19 15:21:41.292  INFO 8140 --- [nio-9001-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2015-10-19 15:21:41.323  INFO 8140 --- [nio-9001-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 31 ms
2015-10-19 15:22:29.727 ERROR 8140 --- [nio-9001-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'very-so-much' in string value "${very-so-much}"] with root cause

java.lang.IllegalArgumentException: Could not resolve placeholder 'very-so-much' in string value "${very-so-much}"
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
    at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:204)
    at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:178)
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:175)
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:801)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:955)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)

任何人对我在这里缺少的东西有任何想法。我已尝试发布尽可能多的信息,但如果您需要任何进一步的信息,请询问。谢谢

【问题讨论】:

    标签: spring spring-boot spring-cloud


    【解决方案1】:

    spring.application.name=hello-service 需要进入bootstrap.properties(引导不是引导)。否则,configserver 不知道要抓取哪个配置。

    【讨论】:

    • 对 bootstrap.properties 进行了更改,但仍然出现相同的错误。如何检查我的 hello-service.properties 文件是否已被服务器加载?
    • 好吧有点奇怪现在似乎工作了。将应用程序名称添加到引导程序后,还直接将属性添加到控制器中,例如 @Value("${very-so-much}") private String prop;
    猜你喜欢
    • 2015-06-18
    • 1970-01-01
    • 2020-07-10
    • 2016-01-18
    • 2015-07-13
    • 2018-04-14
    • 2015-01-03
    • 2016-11-26
    • 2019-12-31
    相关资源
    最近更新 更多