【SpringCloud基础】zuul网关

前言

Github:https://github.com/yihonglei/thinking-in-springcloud

Eureka注册中心:eureka-server

服务提供者(订单服务):eureka-provider-order

zuul网关工程:eureka-gateway-zuul

一 eureka-gateway-zuul

Zuul可以通过加载动态过滤机制,从而实现以下各项功能:

  • 验证与安全保障: 识别面向各类资源的验证要求并拒绝那些与要求不符的请求。
  • 审查与监控: 在边缘位置追踪有意义数据及统计结果,从而为我们带来准确的生产状态结论。
  • 动态路由: 以动态方式根据需要将请求路由至不同后端集群处。
  • 压力测试: 逐渐增加指向集群的负载流量,从而计算性能水平。
  • 负载分配: 为每一种负载类型分配对应容量,并弃用超出限定值的请求。
  • 静态响应处理: 在边缘位置直接建立部分响应,从而避免其流入内部集群。
  • 多区域弹性: 跨越AWS区域进行请求路由,旨在实现ELB使用多样化并保证边缘位置与使用者尽可能接近。

1、项目结构

2、pom.xml(jar包依赖)

<!-- Eureka Client -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

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

3、application.yml(配置文件)

server:
  port: 9003
spring:
  application:
    name: eureka-gateway-zuul
eureka:
  client:
    service-url:
      defaultZone: http://localhost:9000/eureka/
  instance:
    instance-id: eureka-gateway-zuul-9003
    prefer-ip-address: true

server.port 服务端口;

spring.application.name 应用程序名称;

eureka.client.service-url.defaultZone 注册中心;

eureka.instance.instance-id 服务id; 

4、EurekaGatewayZuulApplication(启动类)

package com.jpeony.zuul;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

/**
 * 网关工程,@EnableZuulProxy开启网关支持
 *
 * @author yihonglei
 */
@SpringBootApplication
@EnableZuulProxy
public class EurekaGatewayZuulApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaGatewayZuulApplication.class, args);
    }
}

@EnableZuulProxy注解开启网关支持。

二 服务启动

1、启动注册中心

eureka-server工程EurekaServerApplication类。

2、启动服务提供者

eureka-provider-order工程EurekaOrderApplication类。

3、启动网关工程

eureka-gateway-zuul工程EurekaGatewayZuulApplication类。

三 网关测试

1、访问注册中心

http://localhost:9000/

注册中心可以看到zuul和order服务实例。

2、访问order服务

通过网关调用前,先保证order服务是通的。

http://localhost:8001/order/queryOrderInfo

3、通过网关访问order服务

http://localhost:9003/eureka-provider-order/order/queryOrderInfo

http:ip:port/服务实例/服务地址。

网关根据服务请求实例去寻找服务,并发送请求到相应服务。

四 zuul高级配置

1、默认通过服务实例访问

服务实例对应服务的spring.application.name的值。

默认通过服务实例访问服务,比如订单服务服务实例,会暴露服务名称,不安全。

http://localhost:9003/eureka-provider-order/order/queryOrderInfo

给服务访问起一个映射名称。

增加配置:

zuul:
  routes:
    # 1、指定微服务名称和路径映射
    eureka-provider-order: /order-service/**

完整配置:

server:
  port: 9003
spring:
  application:
    name: eureka-gateway-zuul
eureka:
  client:
    service-url:
      defaultZone: http://localhost:9000/eureka/
  instance:
    instance-id: eureka-gateway-zuul-9003
    prefer-ip-address: true
zuul:
  routes:
    # 1、指定微服务名称和路径映射
    eureka-provider-order: /order-service/**

重启eureka-gateway-zuul服务,访问地址由服务实例变为映射地址。

http://localhost:9003/order-service/order/queryOrderInfo

2、关闭通过服务实例名称访问

我们配置了服务实例映射后,为了安全,把服务实例访问关闭。

增加配置:

zuul:
  ignored-services: "*"

完整配置:

server:
  port: 9003
spring:
  application:
    name: eureka-gateway-zuul
eureka:
  client:
    service-url:
      defaultZone: http://localhost:9000/eureka/
  instance:
    instance-id: eureka-gateway-zuul-9003
    prefer-ip-address: true
zuul:
  routes:
    # 1、指定微服务名称和路径映射
    eureka-provider-order: /order-service/**

  # 表示关闭所有通过微服务名称映射访问
  ignored-services: "*"

重启eureka-gateway-zuul服务,再次通过服务实例访问报404,

http://localhost:9003/eureka-provider-order/order/queryOrderInfo访问报404。

3、zuul配置大全参考

zuul里面有着大量配置,根据实际需要配置即可。可以参考官网关于zuul的配置:

https://cloud.spring.io/spring-cloud-static/Finchley.SR3/single/spring-cloud.html#_router_and_filter_zuul

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值