spring cloud config client 无法获取到 server 中的值
目标:做一个configserver,一个configclient,client 从 server 中获取配置文件中的值
问题:configserver 可以正常work,client 通过 @Value 无法从server中获取需要的值
configserver代码:
application.properties
management.security.enabled=false
server.port=8888
spring.cloud.config.server.git.uri=https://github.com/pangjianhui/my-config-2
ConfigServerApplication.java
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
build.gradle
buildscript {
ext {
springBootVersion = '1.5.2.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
ext {
springCloudVersion = 'Dalston.SR2'
}
dependencies {
compile('org.springframework.cloud:spring-cloud-config-server')
compile('org.tmatesoft.svnkit:svnkit')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:Camden.SR5"
}
}
server 可以正常工作,http://localhost:8888/my-config/test 可以看到返回的内容
{"name":"my-config","profiles":["test"],"label":"master","version":"3d39d7611569de7c6935ae9372965efda6ae9161","state":null,"propertySources":[{"name":"https://github.com/pangjianhui/my-config-2/my-config-test.yml","source":{"myapp.name":"pangjianhui"}}]}
client端
bootstrap.properties
spring.application.name=my-config-test
spring.cloud.config.uri=http://localhost:8888
management.security.enabled=false
build.gradle
buildscript {
ext { springBootVersion = '1.5.2.RELEASE' }
repositories { mavenCentral() }
dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") }
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
jar {
baseName = 'configuration-client'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories { mavenCentral() }
dependencies {
compile('org.springframework.cloud:spring-cloud-starter-config')
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:Camden.SR5"
}
}
ConfigClientApplication.java
@SpringBootApplication
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
@RefreshScope
@RestController
class MessageRestController {
@Value("${myapp.name}")
private String message;
@RequestMapping("/message")
String getMessage() {
return this.message;
}
}
client 启动时一直报错:java.lang.IllegalArgumentException: Could not resolve placeholder 'myapp.name' in value "${myapp.name}"
还请各位大虾帮忙看看到底是咋回事,多谢。