spring cloud zuul的使用示例

2022-05-20  本文已影响0人  天草二十六_简村人

1、依赖引入zuul的starter

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

2、在入口类处增加注解@EnableZuulProxy

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

3、application.yml的配置项

stripPrefix配置为false, 默认值是true。

zuul:
  routes:
    order-details:
      path: /api/v1/pri/orders/details
      serviceId: order-service
      url:
      stripPrefix: false
zuul:
  routes:
    order-details:
      path: /api/v1/pri/orders/details
      serviceId: 
      url: http://192.168.66.100:8080
      stripPrefix: false

访问http://localhost:8080/pay/api/v1/pri/trade/create, 跳转至 http://pay-service/api/v1/pri/trade/create, 会将/pay截取掉。

zuul:
  routes:
    pay-service:
      path: /pay/**
      serviceId: pay-service
      url: 
#    stripPrefix: true

4、配置文件org.springframework.cloud.netflix.zuul.filters.ZuulProperties

// uri作模糊处理
if (!StringUtils.hasText(value.getPath())) {
    value.path = "/" + entry.getKey() + "/**";
}
private Map<String, ZuulRoute> routes = new LinkedHashMap<>();

ZuulRoute是ZuulProperties的内部类。重要的属性就是path, serviceId, url, stripPrefix(默认是true)。


ZuulRoute.png
上一篇 下一篇

猜你喜欢

热点阅读