【问题标题】:Micro-services: Zuul & consul in Spring cloud application微服务:Spring云应用中的Zuul & consul
【发布时间】:2016-02-25 23:12:02
【问题描述】:

我正在尝试使用 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>

application.yml

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

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

bootstrap.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: 春天: 应用: 名称:微服务示例 简介: 活跃:原生

 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]

【问题讨论】:

  • 已在此处回答:stackoverflow.com/questions/33782873/… 并已在快照中修复。
  • 你推荐哪个版本的spring-cloud-starter-consul?
  • 最新版本是 Brixton.M3 (1.0.0.M4) 的一部分,但它没有修复几天前的问题。
  • 在本例中,我使用的是最新版本 (1.0.0.M4)。请参考项目的pom。

标签: spring-boot spring-cloud consul netflix-zuul


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2017-08-08
    • 2018-08-21
    • 2019-02-13
    • 2019-07-18
    • 2018-08-02
    • 2019-01-27
    • 2019-02-12
    • 2017-12-31
    • 2019-09-11
    相关资源
    最近更新 更多