SpringCloud zuul 网关的基本使用

目录


  • 待补

Zuul简介


  • 在微服务架构中,后端服务往往不直接开放给调用端,而是通过一个API网关(API Gateway )根据请求的url,路由到相应的服务。当添加API网关后,在第三方调用端和服务提供方之间就创建了一面墙,这面墙直接与调用方通信进行权限控制,后将请求均衡分发给后台服务端。
  • zuul 是netflix开源的一个API Gateway 服务器, 本质上是一个web servlet应用。
  • Zuul 在云平台上提供动态路由,监控,弹性,安全等边缘服务的框架。Zuul 相当于是设备和 Netflix 流应用的 Web 网站后端所有请求的前门。

Zuul 简单使用


1.添加依赖

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

2.入口程序添加注解@EnableZuulProxy

  • EnableZuulProxy 是组合注解 已经包含 euraka客户端。
@SpringBootApplication
@EnableZuulProxy
public class MicroserviceGetwayZuulApplication {
    public static void main(String[] args) {
        SpringApplication.run(MicroserviceGetwayZuulApplication.class, args);
    }
}

3.设置配置文件
3.1 配置eureka客户端和日志配置

spring:
  application:
    name: microservice-getway-zuul
server:
  port: 8070
eureka:
  client:
    healthcheck:
      enabled: true
    serviceUrl:
      defaultZone: http://user:1234@localhost:8761/eureka
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
logging:
  level:
    com.netflix: debug # 配置该级别可以查看前后端url的转换结果日志

3.2 zuul的配置说明

  • zuul.routes 下可以给指定的微服务配置别名
  • zuul 默认代理euraka上的所有微服务
zuul:
  routes:
    microservice-consumer-hystrix: /consumer/**
 #表示只要HTTP请求是 /consumer开始的,就会forward到服务id为microservice-consumer-hystrix的服务上面
 #没有配置在routes的微服务,默认为添加appname为前缀,类似默认添加配置zuul.routes.microservice-consumer-hystrix:/microservice-consumer-hystrix/**


* zuul.ignoredServices 设置不代理的服务,多个serviceId以逗号隔开
* 填写‘*’ 表示不代理其它所有的服务,只代理配置的微服务

zuul:
  ignoredServices: '*'
  routes:
    microservice-consumer-hystrix: /consumer/**


* 为微服务取别名

zuul:
  routes:
    abc:
      path: /consumer/**
      serviceId: microservice-consumer-hystrix #填写微服务的serviceId
      #url:http://localhost:9000 与serviceId相同意思,填写微服务地址端口链接

  • 使用正则表达式指定Zuul的路由匹配规则
  • 具体为使用 PatternServiceRoute Mapper实现从微服务到映射路由的正则配置
@SpringBootApplication
@EnableZuulProxy
public class MicroserviceGetwayZuulApplication {
    // servicePattern: 指的是微服务的pattern
    // routePattern: 指的是路由的pattern
    @Bean
    public PatternServiceRouteMapper serviceRouteMapper() {
        return new PatternServiceRouteMapper("(?<name>^.+)-(?<version>v.+$)", "${version}/${name}");
    }

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

上方配置的效果如下:

如果微服务的appname设置为microservice-consumer-hystrix-v1
则访问的路径则为localhost:8070/v1/microservice-consumer-hystrix/movief/1


* 不使用eureka实现负载均衡

zuul:
  routes:
    abc:
      path: /consumer/**
      service-id: microservice-consumer-hystrix
ribbon:
  eureka:
    enabled: false
microservice-provider-user:     # 这边是ribbon要请求的微服务的serviceId
  ribbon:
    listOfServers: http://localhost:8031,http://localhost:8032


* 添加前缀

zuul:
  ignoredServices: '*'
  routes:
    microservice-consumer-hystrix: /consumer/**
  prefix: /api
  • 效果为

api/consumer/movief/1 转变为 movief/1

  • 添加strip-prefix
  • 如果所有微服务都有一样的前缀,可以这样使用
zuul:
  ignoredServices: '*'
  routes:
     abc:
      path: /consumer/**
      serviceId: microservice-consumer-hystrix #填写微服务的
      # strip-prefix: false  只用于abc
  prefix: /movief
  strip-prefix: false #全局使用
  • 效果为

movief/consumer/1 转变为 movief/1

  • 解决zuul上传文件超时
    • zuul默认使用hystrix 所以要设置hystrix超时时间
    • zuul 使用ribbon 所以设置ribbon的超时时间
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 60000
ribbon:
  ConnectTimeout: 3000
  ReadTimeout: 60000
  • 增加jvm的堆内存

VM options: -server -XX:PermSize=512M -XX:MaxPermSize=1024M

  • curl 使用curl上传测试

curl -F “[email protected]” localhost:8070/microservice-file-upload/upload

猜你喜欢

转载自blog.csdn.net/u014296316/article/details/80833110