带有spring-cloud的Spring Boot:gradle构建失败

时间:2017-06-22 14:07:45

标签: spring gradle spring-boot spring-cloud spring-cloud-config

运行./gradlew build任务时,

:test失败,底部显示错误。代码只检查上下文是否正确加载。

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration
public class RegistryApplicationTests {

    @Test
    public void contextLoads() {
    }
}

下面给出了bootstrap.yml文件(非常标准),我不确定它为什么要尝试从cloud-config服务加载属性文件,我该如何解决?

spring:
 application:
   name: registry
 profiles:
   active: default
 cloud:
   config:
     uri: http://localhost:8888
     fail-fast: true

eureka:
  instance:
    prefer-ip-address: true
  client:
    registerWithEureka: false
    fetchRegistry: false
    server:
      waitTimeInMsWhenSyncEmpty: 0

堆栈跟踪

Caused by: java.lang.IllegalStateException: Could not locate PropertySource and the fail fast property is set, failing
    at org.springframework.cloud.config.client.ConfigServicePropertySourceLocator.locate(ConfigServicePropertySourceLocator.java:130)
    at org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration.initialize(PropertySourceBootstrapConfiguration.java:89)
    at 
    ....
    ....
Caused by: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8888/registry/default": Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:666)
    at 
    ....
    ....
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at 

更新 正如@dzatorsky所建议的那样,尝试添加@Profile("test")@ActiveProfiles("test") DID NOT WORK

尝试使用@TestPropertySource(locations = "file:src/test/resources/application-test.yml")手动添加测试的属性文件 DID NOT WORK

最后使用有效的@TestPropertySource(properties = {"spring.cloud.config.fail-fast=false"})进行覆盖,但它看起来像是一个非常丑陋的工作

bootstrap.yml中的推理src/main/resources会覆盖其他地方指定的属性,尝试重命名application-test.yml to bootstrap.yml in src/test/resources WORKED

这是更清洁的方式吗?

1 个答案:

答案 0 :(得分:1)

  

" http://localhost:8888/registry/default"的GET请求出错:连接被拒绝:连接;嵌套异常是java.net.ConnectException:连接被拒绝

似乎您的 Spring Cloud Config 服务器已关闭。

UPDATE :如果您想在不运行Config Server的情况下运行测试(在大多数情况下这是正确的事情),那么我建议您执行以下操作:

使用以下内容添加application-test.yml:

cloud:
    config:
        fail-fast: false

使用以下命令注释您的测试类:

@Profile("test")

在这种情况下,无论何时运行测试,它们都使用application.yml中定义的默认参数以及在application.test.yml中重写的参数。