spring cloud 入门实践系列

来源:互联网 发布:linux编译链接 编辑:程序博客网 时间:2024/06/05 22:30

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@SpringBootApplicationpublic 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: 8963spring:  application:    name: service-zuulzuul:  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对应的服务上去了。

原创粉丝点击