云客户端配置应用程序无法从Config Server春季启动中获取属性

时间:2019-03-06 12:34:55

标签: spring rest spring-boot build.gradle

我有一个Web应用程序,并且我想使用Spring Boot中的Server Configuration。 这两个应用程序都位于本地主机上。我从头开始制作了前两个应用程序,并且它们可以一起工作,但是当我使用其中具有许多依赖项(不仅是cloud-config和web依赖项)的客户端时,它不再起作用。我怎么知道?我在服务器的属性文件中有一个变量,我尝试在客户端中使用 @Value(“ $ {atena}”) 并且出现错误 java.lang.IllegalArgumentException:无法解析值“ $ {atena}”中的占位符'atena'

以下图像是我的服务器配置应用程序。 enter image description here

服务器的主类具有以下注释 @EnableConfigServer

在atena-config.yml中,我只有变量名:

 atena: 'Hellllloooooo'

bootstrap.yml内容

server:
  port: 9000
spring:
  profiles:
    active: native

和build.gradle依赖项:

dependencies {

    implementation 'org.springframework.cloud:spring-cloud-config-server'
    runtimeOnly 'org.springframework.boot:spring-boot-devtools'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

我确定服务器是正确的,我的客户端出了点问题。

-------------------->客户端

我有一个restcontroller:

@RestController
@RequestMapping("/songs")
    public class SongController {

    @Value("${atena}")
    String variable;

    @GetMapping(value="/check-from")
    public String viewVariable(){
        return variable;
    }
}

我试图从服务器配置中获取变量。

来自客户端的bootstrap.yml

spring:
  application:
     name: atena-config
   cloud:
     config:
       uri: http://localhost:9000

最后是来自客户端的build.gradle:

    plugins {
    id 'org.springframework.boot' version '2.1.3.RELEASE'
    id 'java'
 }

apply plugin: 'io.spring.dependency-management'

group = 'com'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {

    compile 'com.google.apis:google-api-services-youtube:v3-rev206-1.25.0'


    implementation 'org.springframework.boot:spring-boot-starter'
    implementation('org.apache.tomcat:tomcat-jdbc:9.0.10')

    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation('org.mybatis:mybatis:3.4.6')
    implementation('org.mybatis:mybatis-spring:1.3.2')

    implementation('org.springframework.boot:spring-boot-starter-jdbc')

    implementation('org.springframework.cloud:spring-cloud-starter-config')
    compile group: 'org.postgresql', name: 'postgresql', version: '42.2.5'
    runtimeOnly 'org.springframework.boot:spring-boot-devtools'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

我真的不知道会发生什么,我很确定这些依赖是问题所在,但是我还没有弄清楚哪一个,我不能排除其中的任何一个,因为我在项目中使用了它们。

1 个答案:

答案 0 :(得分:0)

没关系。我已经解决了。确实,问题出在我的依赖项上,我的gradle.build出了问题。我用具有所有依赖项的spring初始化器创建了一个新项目,并从那里复制了新的gradle.build,现在可以正常工作了。

这是客户端的新build.gradle

plugins {
    id 'org.springframework.boot' version '2.1.3.RELEASE'
    id 'java'
}

apply plugin: 'io.spring.dependency-management'

group = 'com'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
    maven { url 'https://repo.spring.io/milestone' }
}
ext {
    set('springCloudVersion', 'Greenwich.RELEASE')
}

dependencies {

    compile 'com.google.apis:google-api-services-youtube:v3-rev206-1.25.0'

    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.0.0'
    implementation 'org.springframework.cloud:spring-cloud-starter-config'
    runtimeOnly 'org.springframework.boot:spring-boot-devtools'
    runtimeOnly 'org.postgresql:postgresql'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}
相关问题