gpt4 book ai didi

spring-boot - 微服务 : Zuul & consul in Spring cloud application

转载 作者:行者123 更新时间:2023-12-01 05:00:01 37 4
gpt4 key购买 nike

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

我的项目中有 2 个组件:

  • 使用 Zuul 的 api-gateway 微服务
  • Hello world 微服务(一个简单的 hello world Rest Webservice)

  • 这是 api-gateway 的代码:
     @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

    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:
    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]

    最佳答案

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

    关于spring-boot - 微服务 : Zuul & consul in Spring cloud application,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33877944/

    37 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com