How to forward to different path using netflix zuul?

只谈情不闲聊 提交于 2019-11-28 11:58:09

问题


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/** .

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

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

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

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

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

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.

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?

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:

回答1:


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

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);
   }
}

than:

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

Zuul application don't need to be registered with Eureka, and it should fetch the registry.

And than you can say:

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


来源:https://stackoverflow.com/questions/55590832/how-to-forward-to-different-path-using-netflix-zuul

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!