我正在开发基于Spring云领事的2个应用程序。 第一个调用Hello1并公开一个简单的hello world Web服务。第二个叫做Hello2,尝试调用第一个应用程序公开的Web服务。这2个应用程序使用consul作为服务发现。
对于Hello2,我有以下代码:
@RestController
@Slf4j
public class HelloWorldConsController {
@Autowired
private DiscoveryClient discoveryClient;
@Autowired
protected RestTemplate restTemplate;
protected String serviceUrl;
@RequestMapping("/hello2")
public String hello() {
log.info("Hello calls ");
return restTemplate.getForObject("http://hello1-Example/hello1", String.class);
}
}
配置文件(yml):
spring:
application:
name: hello2-Example
profiles:
active: native
cloud:
consul:
config:
enabled: true
host: localhost
port: 8500
对于Hello1应用程序:
@Controller
@RestController
public class HelloWorldController {
@RequestMapping("/hello1")
public String hello() {
return "Hello1";
}
}
配置文件(yml):
spring:
application:
name: hello1-Example
profiles:
active: native
cloud:
consul:
config:
enabled: true
host: localhost
port: 8500
这是我的依赖项:
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Angel.SR3</version>
</parent>
<properties>
<lombok.version>1.16.2</lombok.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<!-- Setup Spring MVC & REST, use Embedded Tomcat -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<!-- Spring Cloud starter -->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-all</artifactId>
<version>1.0.0.M2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
</dependencies>
但是,当我调用hello 2 application http://localhost:1234/hello2时,它会给我以下错误:
org.springframework.web.client.HttpClientErrorException: 404 NOT_FOUND
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:614)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:570)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:530)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:237)
答案 0 :(得分:0)
它使用以下pom:
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Brixton.M3</version>
</parent>
<properties>
<lombok.version>1.16.2</lombok.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
<version>1.1.0.M3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>1.3.0.RELEASE</version>
</dependency>
<dependency>
<!-- Setup Spring MVC & REST, use Embedded Tomcat -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.3.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>1.1.0.M3</version>
</dependency>
<dependency>
<!-- Spring Cloud starter -->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-all</artifactId>
<version>1.0.0.M4</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>