简体   繁体   中英

This is about zuul and routing

This question is about zuul and routing. Now to get zuul routing is quite easy in your application.yml you have the following

routes:
    silver:
      path: "/silver/**"
      serviceId: "SILVER-MICO-SERVICE"
      stripPrefix: true
    gold:
      path: "/gold/**"
      serviceId: "GOLD-MICO-SERVICE"
      stripPrefix: false

So if your URL has silver in it, it will direct the silver micro service, if it has gold it directs to the gold micro service.

But I have a 3rd service, that I want to call before I direct the to the silver or gold. so i want something like

routes:
    silver:
      path: "/silver/**"
      serviceId: "SILVER-MICO-SERVICE"
      stripPrefix: true
    gold:
      path: "/gold/**"
      serviceId: "GOLD-MICO-SERVICE"
      stripPrefix: false
   throttle
     path: "/throttle/**"
      serviceId: "THROTTLE-MICO-SERVICE"
      stripPrefix: false

And when say silver is called

I will have the following in my filter, I would have a throttleFilter which will call out to the throttle micro service. But I have no idea how to code a zuul call in Java.

Thanks for any help

You do not need to route to throttle micro service cause you can route to only one micro service. Instead you can write a pre filter and call throttle micro service. Here is an example of how to write a pre filter

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;

import javax.servlet.http.HttpServletRequest;

public class SimplePreFilter extends ZuulFilter {

    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 1;
    }

    @Override
    public boolean shouldFilter() {
        //You can apply custom logic if required
        return true;
    }

    @Override
    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        //TODO you can call throttle micro service here
        return null;
    }

}

Ok this is the code to do what I want to do. The functionality I needed was supplied by the EurekaClient

zuul 
    throttling:
      path: "/throttling/**"
      serviceId: "SITE-THROTTLING"
      stripPrefix: false

And in the code I have

@Autowired
private EurekaClient eurekaClient;

private boolean checkThrottling(String url) {
        
        InstanceInfo service = eurekaClient
                  .getApplication("SITE-THROTTLING")
                  .getInstances()
                  .get(0);
        StringBuilder sb = new StringBuilder(service.getHomePageUrl())
                .append("/throttling")
                .append("?paramUrl=")
                .append(url);
        return webSevice.sendPost(sb.toString());

public boolean sendPost(String url) {
        WebClient webClient = WebClient.create();
        Boolean flag = webClient.get()
                                       .uri(url)
                                       .retrieve()
                                       .bodyToMono(Boolean.class)
                                       .block();
        return flag;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM