zuul:(一)SpringCloud的网关介绍和zuul网关的使用

1)什么是网关?
        API Gateway,是系统的唯一对外的入口,介于客户端和服务器端之间的中间层,处理非业务功能 提供路由请求、鉴权、监控、缓存、限流等功能

统一接入
     智能路由
     AB测试、灰度测试
     负载均衡、容灾处理
     日志埋点(类似Nignx日志)
流量监控
     限流处理
     服务降级
安全防护
     鉴权处理
     监控
     机器网络隔离

2)主流的网关

        zuul:是Netflix开源的微服务网关,和Eureka,Ribbon,Hystrix等组件配合使用,Zuul 2.0比1.0的性能提高很多
        kong: 由Mashape公司开源的,基于Nginx的API gateway
        nginx+lua:是一个高性能的HTTP和反向代理服务器,lua是脚本语言,让Nginx执行Lua脚本,并且高并发、非阻塞的处理各种请求

3)zuul网关的使用:

  1.idea创建工程,网关要单独一个工程:需要的依赖:

------------           ---------------------         ---------------------     -----------------       ----------------- 

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

  2.启动类加入注解 @EnableZuulProxy
        默认集成断路器  @EnableCircuitBreaker无需添加

package net.xdclass.apigataway;

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

@SpringBootApplication
@EnableZuulProxy
public class ApiGatawayApplication {

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

  

  3.yml配置文件设置:

server:
  port: 9000


#服务的名称
spring:
  application:
    name: api-gateway



#指定注册中心地址
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

    4.访问规则如:

1.原本调用订单中心order-server的api:save
        http://localhost:8781/api/v1/order/save?user_id=1&product_id=1
 网关访问:
        http://localhost:9000/order-server/api/v1/order/save?user_id=1&product_id=1

2.原本产品中心product-server的api:list
        http://localhost:8771/api/v1/product/list
   网关访问:
        http://localhost:9000/product-server/api/v1/product/list    

规则:http://gateway:port/service-id/**
    service-id:注册中心的服务名称

  

配置方式:

  5.自定义路由转发配置,如:

server:
  port: 9000


#服务的名称
spring:
  application:
    name: api-gateway



#指定注册中心地址
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

#自定义路由映射
zuul:
  routes:
    order-service: /apigateway/**
网关访问:
        http://localhost:9000/apigateway/api/v1/order/save?user_id=1&product_id=1
     也可以order-server代替
apigateway访问

  6.环境隔离配置:如

server:
  port: 9000


#服务的名称
spring:
  application:
    name: api-gateway



#指定注册中心地址
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

#自定义路由映射
zuul:
  routes:
    order-service: /apigateway/**
    product-service: /apigateway/**
  #统一入口为上面的配置,其他入口忽略
  ignored-patterns: /*-service/**
网关访问:
        http://localhost:9000/apigateway/api/v1/order/save?user_id=1&product_id=1
     ignored:不可以order-server代替apigateway访问

  7.忽略整个服务配置,不对外提供接口: ignored-services: product-service

server:
  port: 9000


#服务的名称
spring:
  application:
    name: api-gateway



#指定注册中心地址
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

#/order-service/api/v1/order/save?user_id=2&product_id=1
#自定义路由映射
zuul:
  routes:
    order-service: /apigateway/**
    product-service: /apigateway/**
  #统一入口为上面的配置,其他入口忽略
  ignored-patterns: /*-service/**
  #忽略整个服务,对外提供接口
  ignored-services: product-service
网关访问无效

.

  

  :

猜你喜欢

转载自www.cnblogs.com/big-cut-cat/p/9910095.html