6

我正在尝试使用 Zuul 和 Consul 创建一个 Spring 云微服务应用程序。

我的项目中有 2 个组件:

  1. 使用 Zuul 的 api-gateway 微服务

  2. Hello world 微服务(一个简单的 hello world Rest Webservice)

这是API网关的代码:

 @SpringBootApplication
 @EnableZuulProxy
 @EnableDiscoveryClient
 public class ZuulApplication {

 public static void main(String[] args) {
    SpringApplication.run(ZuulApplication.class, args);
  }

 }

pom.xml

<parent>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-parent</artifactId>
    <version>Brixton.M3</version>
</parent>

<properties>
    <java.version>1.8</java.version>
    <spring.cloud.consul.version>1.0.0.M4</spring.cloud.consul.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-commons</artifactId>
    </dependency>

    <dependency>
        <!-- Setup Spring Boot -->
        <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.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-zuul</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-consul-all</artifactId>
        <version>${spring.cloud.consul.version}</version>
    </dependency>

</dependencies>

应用程序.yml

 zuul:
  routes:
    hello1:
     path: /hello1/**
     serviceId: microservice-example

logging:
  level:
   org.springframework: INFO
   com.netflix: DEBUG

引导程序.yml

spring:
  application:
    name: edge-server

 cloud:
   consul:
    config:
      enabled: true
      host: localhost
      port: 8500

这是hello微服务的代码:

 @SpringBootApplication
 @EnableConfigServer
 @EnableDiscoveryClient
 @RestController
 public class Application {

 @RequestMapping(value="/hello1",method = RequestMethod.GET)
 public String hello() {
    System.out.print("hello1");
    return "Hello1";
 }
 public static void main(String[] args) {
    new SpringApplicationBuilder(Application.class).web(true).run(args);
 }
}

bootstrap.yml:spring:应用程序:名称:微服务示例配置文件:活动:本机

 cloud:
  consul:
   config:
    enabled: true
    host: localhost
    port: 8500

但是,当我启动 api-gateway 时,出现以下异常:

   Caused by: org.springframework.beans.BeanInstantiationException: Failed     to instantiate [org.springframework.cloud.netflix.zuul.filters.RouteLocator]: Factory method 'routeLocator' threw exception; nested exception is java.lang.IllegalStateException: Unable to locate service in consul agent: edge-server
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE]
... 69 common frames omitted
 Caused by: java.lang.IllegalStateException: Unable to locate service in consul agent: edge-server
at org.springframework.cloud.consul.discovery.ConsulDiscoveryClient.getLocalServiceInstance(ConsulDiscoveryClient.java:66) ~[spring-cloud-consul-discovery-1.0.0.M4.jar:1.0.0.M4]
4

1 回答 1

1

此问题已在 Brixton.M3 (1.0.0.M5) 中修复。如上所述,这是 spring-cloud-consul 的一个问题。新版本运行良好

于 2017-12-04T20:00:47.310 回答