如何使用netflix zuul转发到不同的路径? [英] How to forward to different path using netflix zuul?

查看:547
本文介绍了如何使用netflix zuul转发到不同的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using netflix zuul embedded in a spring boot 1.5.x application. Behind zuul are some microservices. These microservices expose public endpoints under /public/** .

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

Now i want to expose these public endpoints via zuul api gateway but stripping the "/public" out of the final api gateway url.

从外部(通过zuul api网关请求)的示例(预期):

Example (expected) from outside (request through zuul api gateway):

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

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

Zuul should forward this request to (with /public in the url):

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

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

How can i achieve this without using url parameter in the zuul configuration. I would like to rely on the internal client side load balancing using serviceId parameters. I couldn't figure it out.

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

If this isn't a good pattern, should i split my microservices into webservices and core-services? So that only webservices are mounted/accessible via Zuul?

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

This configuration works fine but it uses the url parameter instead of serviceId:

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

推荐答案

您需要通过Spring Cloud Netflix进行服务发现 Eureka ,检查简介

You need service discovery via Spring Cloud Netflix Eureka , check Introduction and How to declare routes with serviceId

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

After setting that up, enable zuul proxy and discovery client in zuul application

@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 application don't need to be registered with Eureka, and it should fetch the registry.

而且你可以说:

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

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

If you want to have defined multiple routes for one service, do:

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

这篇关于如何使用netflix zuul转发到不同的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆