美文网首页
spring cloud zuul的使用示例

spring cloud zuul的使用示例

作者: 天草二十六_简村人 | 来源:发表于2022-05-20 09:33 被阅读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。

  • 匹配准确uri
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
  • 使用注册中心,配置serviceId
  • 匹配前缀的路径/pay

访问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() + "/**";
}
  • 配置项中可以配置多个路由,见变量routes,对应这里的变量属性是下面的routes。这里值得注意的是,配置多个是使用Map集合。
private Map<String, ZuulRoute> routes = new LinkedHashMap<>();

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


ZuulRoute.png

相关文章

网友评论

      本文标题:spring cloud zuul的使用示例

      本文链接:https://www.haomeiwen.com/subject/gaytprtx.html