今天有人问我这个,初次使用 spring cloud 配置管理后,一运行 编译报错

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'hello' in value "${hello}"

百度一下解决问题,感觉网上说的不是很清楚,新手的话一时半会可能看不懂,自己就写一篇博客吧。力求人人都能看懂。

解决方法

配置文件 用 bootstrap.properties
配置如下

# 和git里的文件名对应
spring.application.name=client
# 远程仓库的分支
spring.cloud.config.label=master
# dev 开发环境配置文件 |  test 测试环境  |  pro 正式环境
# 和git里的文件名对应
spring.cloud.config.profile=dev
# 注册在总服务里面的,配置服务 地址
spring.cloud.config.uri= http://localhost:8888/
server.port=7020

下面是选看的内容

先看一下我的工程配置,大家也可以对照一下,是否你们的错误了

spring cloud server

这个呢,就是spring cloud的总服务,所有的其他分布式工程都会注册到这里。
application.yml

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
       #总服务的打开地址
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

Note:运行这个项目后,我们打开上面我们自己定义的网址 defaultZone

当其他分布式项目没有运行时候,如图所示。

SpringBoot Skywalking agent 不生效 springboot could not resolve placeholder_spring


如果,其他分布式项目运行了,例如下面的spring cloud config-server运行,那么会注册一个到总服务里面。

SpringBoot Skywalking agent 不生效 springboot could not resolve placeholder_spring boot_02

spring cloud config-server

首先,看一下我的config-server的主要配置。config-server :顾名思义 配置服务,这个服务提供配置文件以便让其他项目进行读取。

application.yml

eureka:
  client:
    serviceUrl:
      #总服务的 地址
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8888

spring:
  cloud:
    config:
      server:
        git:
          #存放配置文件的git 仓库地址 SpringCloudConfig是仓库名
          uri: https://github.com/planitian/SpringCloudConfig
          #由于我是直接在仓库下面建立文件,
          searchPaths: /
          #git分支选为 master分支
      label: master
  application:
    name: config-server

上面git仓库地址目录截图

SpringBoot Skywalking agent 不生效 springboot could not resolve placeholder_spring_03


client-dev.properties内容,spring cloud 约定大于配置,按照以下规则配置

1. /{application}/{profile}[/{label}]
2. /{application}-{profile}.yml
3. /{application}-{profile}.properties
4. /{label}/{application}-{profile}.properties

很明显是以第三个来命名的。

SpringBoot Skywalking agent 不生效 springboot could not resolve placeholder_spring cloud_04

Note:当这个项目运行后,应该能在总服务的defaultZone网页看见当前服务名。

SpringBoot Skywalking agent 不生效 springboot could not resolve placeholder_配置文件_05


并且,在浏览器访问 http://localhost:8888/client/dev

client 是你需要远程得到配置的项目 名称,dev是profile 那里定义的,代表开发环境,当显示下面图片时候,说明config server没问题。

SpringBoot Skywalking agent 不生效 springboot could not resolve placeholder_配置文件_06

spring cloud config client

这个项目呢,就是需要远程读取配置的系统。
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Note:这个的配置文件,不能再是application.yml必须是 bootstrap.properties。
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

上面这个就是解决方法,配置文件 用 bootstrap.properties。

SpringBoot Skywalking agent 不生效 springboot could not resolve placeholder_spring_07


bootstrap.properties 配置

# 和git里的文件名对应
spring.application.name=client
# 远程仓库的分支
spring.cloud.config.label=master
# dev 开发环境配置文件 |  test 测试环境  |  pro 正式环境
# 和git里的文件名对应
spring.cloud.config.profile=dev
# 注册在总服务里面的,配置服务 地址
spring.cloud.config.uri= http://localhost:8888/
server.port=7020

ClientApplication

package com.example.client;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


@SpringBootApplication
@RestController
public class ClientApplication {

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

    @Value("${hello}")
    private String value;

    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public String tee(@PathVariable String id){
        System.out.println(value);
        return value;
    }
}

这样,运行项目后,访问http://localhost:7020/id 就可以看见远程取回的配置,

SpringBoot Skywalking agent 不生效 springboot could not resolve placeholder_java_08

原理

bootstrap.yml(bootstrap.properties)用来程序引导时执行,应用于更加早期配置信息读取,如可以使用来配置application.yml中使用到的参数

application.yml(application.properties) 应用程序特有配置信息,可以用来配置后续各个模块中需使用的公共参数等。