spring-cloud-part 5 : 路由网关 (Zuul)

摘自: http://blog.csdn.net/forezp/article/details/69939114  方志朋微博

一 zuul用作路由的作用
/**
* zuul 路由网关的使用
* 步骤: 1. eureka-server 8761
* 2. eureka-client 8762 hi
* 3. service-ribbon 8764 hello
* 4. service-feign 8763 how
* 5. service-zuul
* 1. 导包 spring-cloud-starter-eureka
* spring-boot-starter-web
* spring-cloud-starter-zuul
* 2. 在入口类上加上注解 @EnableZuulProxy
* 3. application.yaml 配置文件
* 端口: 8765 spring.application.name:service-zuul
* eureka.service-url...
* zuul的配置
* 4. 测试
* 访问: http://localhost:8765/api-a/hello?name=zdj
*      访问: http://localhost:8765/api-b/hello?name=zdj
* 均可以出现网页 hi,this port is : 8762--说明zuul在这里起到了路由的作用
*
*/

服务网关API gateway 有多种实现方法,比如 Nginx,Zuul ,甚至一个Node.js 的服务端;其作用是是前台提供后台服务的集合,提供一个统一的服务出口。
解决它们之间的耦合,同时负责鉴权、认证、安全和跳转的作用。

Zuul是边缘服务,用来提供动态路由、监控、鉴权、安全、调度等功能,将权限控制等一些业务逻辑抽离出来,单独放到Zuul里,使得服务组件更简单,具有更好的可复用性。

zuul 作用原理 :

  Zuul是反向代理工具,代理了后台的所有服务端,前端请求不需要知道真正的服务端是谁,只要交给Zuul就可以了,Zuul负责路由到真正的服务商

简言之,zuul的作用就是  封装内部所有的服务商,对外提供统一的调用

01 导包

02 入口类上加上@EnableZuulProxy

03  配置 .yaml 配置文件

server:
  port: 8765

spring:
  application:
    name: service-zuul

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/


#  以 /api-a/ 开头的请求都转发给 service-ribbon 服务;
#  以 /api-b/ 开头的请求都转发给 service-feign 服务;
zuul:
  routes:
    api-a:
      path: /api-a/**
      serviceId: service-ribbon
    api-b:
      path: /api-b/**
      serviceId: service-feign

 04  测试   访问  http://localhost:8765/api-a/hello?name=zdj  以及  http://localhost:8765/api-b/hello?name=zdj  均可以调用 service-hi 服务,说明zuul在这里起到了路由的作用。

 

二   zuul 还可以用作服务过滤























posted @ 2018-01-12 14:21  po~  阅读(293)  评论(0编辑  收藏  举报