0

我正在开发基于 Spring cloud consul 的 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 应用程序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)
4

1 回答 1

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>

于 2015-11-22T21:40:47.033 回答