spring cloud 入门实践系列 - zuul

Zuul is the front door for all requests from devices and web sites to the backend of the Netflix streaming application. As an edge service application, Zuul is built to enable dynamic routing, monitoring, resiliency and security. It also has the ability to route requests to multiple Amazon Auto Scaling Groups as appropriate.

Zuul是从设备和网站到Netflix流应用的后端的所有请求的前门。 作为边缘服务应用程序,Zuul旨在实现动态路由,监控,弹性和安全性。 它还可以根据需要将请求路由到多个Amazon Auto Scaling Groups。

启动eureka注册中心

可参见之前文章

创建两个eureka服务

service-first 暴露在8961端口
@RestController
@RequestMapping("firstapi")
public class FirstApiController {

    @RequestMapping("speak")
    public String doSpeak(@RequestParam String words){
        return "port:8961,woooooo,I am speaking of ["+words+"]";
    }

}
service-second 暴露在8962端口
@RestController
@RequestMapping("secondApi")
public class SecondApiController {
    @RequestMapping("sing")
    public String sing(@RequestParam String song){
        return "port:8962,hoooooooo,I am singing of ["+song+"]";
    }
}

创建zuul服务

项目结构

这里写图片描述

zuul启动类
@EnableZuulProxy
@EnableEurekaClient
@SpringBootApplication
public class ZuulwareApplication {

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

@EnableZuulServer - 普通网关,只支持基本的route与filter.
@EnableZuulProxy - 配合上服务发现与熔断开关的上面这位增强版,具有反向代理功能.

zuul配置文件application.yml,zuul服务暴露在8963端口
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8963
spring:
  application:
    name: service-zuul
zuul:
  routes:
   firstapi:
     path: /firstapi/**
     serviceId: service-first
   secondapi:
          path: /secondapi/**
          serviceId: service-second

上面配置的含义是请求路径uri为/firstapi开头的转发至service-first服务
请求路径uri为/firstapi开头的转发至service-second服务
特别注意:1. routes节点下的元素要与path的前缀一致
2. path 后面的/**是指服务对应的全路径,比如我在service-first中定义的FirstApiController,访问的路径是http://172.16.153.1:8961/firstapi/speak?words=zxl,如果我们想通过zuul服务转发出去,则访问路径应该是http://172.16.153.1:8961/firstapi/firstapi/speak?words=zxl 第一个firstapi代表zuul配置文件中配置的route地址,第二个firstapi代表FirstApiController类的访问路径配置,这点一定要注意。千万不要使用http://172.16.153.1:8961/firstapi/speak?words=zxl 这种方式去访问,因为zuul将该路径请求转发给service-first后,是使用/speak去匹配,这样是找不到对应处理类的。

启动2个eureka服务和zuul服务

这里写图片描述

http://172.16.153.1:8963/firstapi/firstapi/speak?words=zxlhgggggggg
这里写图片描述

http://172.16.153.1:8963/secondapi/secondApi/sing?song=zxl
这里写图片描述

可见我们通过zuul的8693端口访问出去的请求,最终被zuul转发到8691和8692对应的服务上去了。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值