简体   繁体   English

微服务:Spring云应用程序中的Zuul&consul

[英]Micro-services: Zuul & consul in Spring cloud application

I'm trying to create a Spring cloud microservice application using Zuul and Consul. 我正在尝试使用Zuul和Consul创建Spring云微服务应用程序。

I have 2 components in my project: 我的项目中有2个组件:

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

  2. Hello world microservice (a simple hello world Rest Webservice) Hello world microservice(一个简单的hello world Rest Webservice)

Here is the code of The api-gateway: 这是api-gateway的代码:

 @SpringBootApplication
 @EnableZuulProxy
 @EnableDiscoveryClient
 public class ZuulApplication {

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

 }

The pom.xml 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 application.yml

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

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

bootstrap.yml bootstrap.yml

spring:
  application:
    name: edge-server

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

Here is the code of hello microservice: 这是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: application: name: microservice-example profiles: active: native bootstrap.yml:spring:application:name:microservice-example profiles:active:native

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

But, when I start the api-gateway I got the following exception: 但是,当我启动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]

This issue is fixed in Brixton.M3 (1.0.0.M5). 此问题已在Brixton.M3(1.0.0.M5)中修复。 As mentioned above this was an issue with spring-cloud-consul. 如上所述,这是spring-cloud-consul的一个问题。 The new version is working fine 新版本工作正常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM