如何使用netflix zuul转发到其他路径?

时间:2019-04-09 10:36:29

标签: reverse-proxy http-proxy netflix-zuul spring-cloud-netflix

我正在使用内嵌在spring boot 1.5.x应用程序中的netflix zuul。 zuul的背后是一些微服务。这些微服务在/ public / **下公开公共端点。

现在,我想通过zuul api网关公开这些公共端点,但是将“ / public”从最终的api网关URL中删除。

外部示例(通过zuul api网关请求):

api.yourdomain.tld/path/to/service/endpoint

Zuul应将此请求转发至(网址中带有 / public ):

service:{port}/public/path/to/service/endpoint

如何在zuul配置中不使用url参数来实现此目标。我想依靠使用 serviceId 参数的内部客户端负载平衡。我不知道。

如果这不是一个好的模式,我是否应该将微服务分为Web服务和核心服务?这样只能通过Zuul挂载/访问Web服务吗?

此配置工作正常,但它使用url参数而不是serviceId:

    custom-service:
      path: /path/to/service/endpoint/**
      stripPrefix: false
      url: http://custom-service:51022/public
      sensitiveHeaders:

1 个答案:

答案 0 :(得分:0)

您需要通过Spring Cloud Netflix Eureka进行服务发现,选中IntroductionHow to declare routes with serviceId

设置完成后,在zuul应用程序中启用zuul代理和发现客户端

@EnableZuulProxy
@EnableDiscoveryClient
@SpringBootApplication
public class ZuulProxyApplication {

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

大于:

eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=true

Zuul应用程序不需要在Eureka上注册,它应该获取注册表。

而且你可以说:

zuul:
  routes:
    custom-service:
      path: /path/to/service/endpoint/**
      stripPrefix: false
      serviceId: customServiceId

如果要为一项服务定义多个路由,请执行以下操作:

zuul:
  routes:
    custom-service:
      path: /path/to/service/endpoint/**
      stripPrefix: false
      serviceId: customServiceId
    custom-service:
      path: /second_path/to/service/endpoint/**
      stripPrefix: false
      serviceId: customServiceId