两个带有两个端口的Spring Boot应用程序在使用RestTemplate发布时抛出500错误



好的,我有STS 4和构建Spring Boot应用程序。我想要一个使用Vaadin处理GUI的,它运行良好。我有另一个获取ActiveMQ主题并创建第一个主题的POST。如果我使用POSTMAN来完成这篇文章,那么它对第一个应用程序上的REST接口来说很好。但当我运行第二个应用程序时,检测到一个Topic,并且它试图发布到第一个应用程序REST接口时,我得到。。。

org.springframework.web.client.HttpServerErrorException$InternalServerError: 500 : [<!doctype html><html lang="en"><head><title>HTTP Status 500 – Internal Server Error</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-colo... (6083 bytes)]

进一步的测试表明,它在抱怨,因为它在与第一个端口相同的端口(8080(上侦听。

在我的第一个应用程序的application.properties中,我设置了

server.port=${PORT:8080}
vaadin.compatibilityMode = false
logging.level.org.atmosphere = warn
spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.user=admin
spring.activemq.password=admin
spring.jms.pub-sub-domain=true

在第二个应用程序中,我设置了

server.port=${PORT:8081}
spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.user=admin
spring.activemq.password=admin
spring.jms.pub-sub-domain=true
active-mq.topic=personQBE

试图POST的违规代码是…

public void postResponses(Person person) {
System.out.println("postResponses(Person person)");
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
headers.add("Accept", MediaType.APPLICATION_JSON.toString()); //Optional in case server sends back JSON data

MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("firstName", person.getFirstName());
map.add("lastName", person.getLastName());
map.add("alias", person.getAlias());
map.add("dataSource", "BI");

HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(map, headers);
ResponseEntity<String> response = null;
try {
response = restTemplate.exchange("http://localhost:8080/person/add",
HttpMethod.POST,
entity,
String.class);
} catch (RestClientException e) {
// TODO Log.error later
e.printStackTrace();
}
System.out.println("response:" + response);

}

我已经阅读了关于Spring的微服务的博客文章,但不确定我是否需要这样做来解决我的问题,接下来,无论第一个应用程序何时发布该主题,第二个应用程序都将成为代理从外部API获取数据的模板,因此如果第二个程序在某个地方单独运行,端口争用将不会成为问题。

添加以下值以运行带有随机端口号的应用程序

server.port=0

希望您已经配置了EurekaZuul

最新更新