2

Brixton.RC1对我来说,例如 for .properties 的内容协商机制在切换到 Spring Cloud或Brixton.RC2Angel.SR5和之后在 Spring Cloud Config Server 中不起作用Angel.SR6

当我使用gradlew bootRunor启动服务时会出现问题java -jar ...。它在我的集成测试中工作(见Working Integration-Test)。

使用场景:

我想访问testing应用程序配置文件中的配置my-service,所以我打电话http://localhost:8888/my-service/testing.properties

预期结果:

some.property=1234
some.other.property=hello there

实际结果:

没有Accept-Header:

<!DOCTYPE html>
<html>
    <head>
        <title>Error 406</title>
    </head>
    <body>
        <h1>Error 406: Not Acceptable</h1>
        <br/>
        Could not find acceptable representation
    </body>
</html>

使用Accept-Header application/json

{
    "timestamp": 1461140158009,
    "status": 406,
    "error": "Not Acceptable",
    "exception": "org.springframework.web.HttpMediaTypeNotAcceptableException",
    "message": "Could not find acceptable representation",
    "path": "/config/my-service/default.properties"
}

从示例中可以看出,内容协商机制似乎适用于错误处理,但对于配置访问却不是。

工作集成测试:

我写了以下Spock-Test

@WebIntegrationTest({"server.port=0", "management.port=0"})
@ActiveProfiles("testing")
@ContextConfiguration(loader = SpringApplicationContextLoader.class, classes = ConfigurationServiceApplication.class)
class ConfigurationAccessTest extends Specification {

    @Autowired
    TestserverInfo testserverInfo

    def "testing profile returns testing properties"() {
        given:
        RestTemplate rest = new TestRestTemplate(null, null)
        Properties properties = new Properties()

        when:
        String result = rest.getForObject( testserverInfo.getBasePath() + "/my-service-testing.properties", String.class );
        properties.load( new StringReader(result) )

        then:
        properties['test'] == 'Testing Profile World'
        properties['my.long.testing.property'] == 'Testing Profile Property'
    }

到目前为止我已经做过的事情:

  1. 为这个正在工作的场景写了一个上面的Spock-Test所有提到的版本中都有效Spring Cloud Config Server
  2. 查看ConfigServerMvcConfiguration任何明显的配置错误
  3. 自己提供一个 WebMvcConfigurer 并像上面引用的配置类一样初始化内容协商:

    @EnableWebMvc
    @Configuration
    public class ConfigMvcConfiguration extends WebMvcConfigurerAdapter {
        private final Logger logger =  LoggerFactory.getLogger(ConfigMvcConfiguration.class);
    
        @Override
        public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
            configurer.mediaType("properties", MediaType.valueOf("text/plain"));
            configurer.mediaType("yml", MediaType.valueOf("text/yaml"));
            configurer.mediaType("yaml", MediaType.valueOf("text/yaml"));
            logger.info("media-types added");
        }
    }
    

任何人都可以重现此问题或为我提供有关如何解决它的任何指导吗?

4

1 回答 1

3

在写完这个问题引起了一阵骚动之后,我意识到:我在猎杀鬼。在检查我自己编写的集成测试时,我发现我试图使用错误的 url-schema 访问属性。

TLDR

我试图使用 URL 访问属性

http://localhost:8888/config/my-service/testing.properties   <-- wrong
rather than
http://localhost:8888/config/my-service-testing.properties   <-- correct

细节

可以使用以下 url 模式访问 JSON 格式的属性(不使用标签时):

http://<host>:<port>/<contextPath>/<application>/<profile>

如果想要以 json 以外的格式访问属性,则必须使用另一个 url-schema(同样是没有标签的示例)

http://<host>:<port>/<contextPath>/<application>-<profile>.<format>

使用格式beeing properties/yml/yaml 作为当前支持的格式。

再一次,一个小错误会使事情复杂化很多。

于 2016-04-22T08:46:37.843 回答