通过ServiceId进行zuul路由后如何添加路径?

时间:2019-06-18 14:28:32

标签: java microservices netflix-zuul consul service-discovery

我只想在zuul路由后添加一条路径(通过serviceId和consul)。这样,zuul http://localhost:9099/customer/getName的传入URL将被映射到特定的微服务入口点http://localhost:8091/service/customer/getName

通过领事进行的服务发现工作正常,基本映射也是如此。但是路由问题导致http://localhost:8091/getName。这意味着我需要在中间添加“ / service / customer /” 路径...但是如何?

当然。如果输入网址http://localhost:9099/customer/service/customer/getName可以,但是我真的想避免这种情况。 ?

注册领事服务:

{"service": {"name": "customer_service", "tags": ["service"], "port": 8091}}

application.yml和zuul映射:

server.port: 9099
spring.application.name: zuul

zuul:
  routes:
    heroname_service:
      path: /customer/**
      serviceId: customer_service

spring:
  cloud:
    config:
      server:
        bootstrap: true
      uri: http://localhost:${server.port}

SpringBoot应用程序文件:

@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
public class Zuul2Application {

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

}

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>de.it</groupId>
    <artifactId>zuul2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>zuul2</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>



    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

0 个答案:

没有答案