好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

SpringCloud Zuul实现动态路由

前言

zuul 是在spring cloud netflix平台上提供 动态路由 ,监控,弹性,安全等边缘服务的框架,是netflix基于jvm的路由器和服务器端负载均衡器,相当于是设备和 netflix 流应用的 web 网站后端所有请求的前门。本文基于上篇( springcloud系列——ribbon 负载均衡 )实现zuul动态路由

github地址: https://github测试数据/netflix/zuul

官方文档: https://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/2.1.0.rc2/single/spring-cloud-netflix.html

代码编写

首先我们在springcloud下面新建一个springboot项目:zuul-server,pom继承parent,并且在eureka上面注册(还不会服务注册与发现的,请戳: springcloud系列——eureka 服务注册与发现 )

maven引入zuul

?

1

2

3

4

5

<!-- zuul -->

    <dependency>

      <groupid>org.springframework.cloud</groupid>

      <artifactid>spring-cloud-starter-netflix-zuul</artifactid>

    </dependency>

配置文件

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

server.port= 10010

spring.application.name=zuul-server

eureka.client.serviceurl.defaultzone=http: //localhost:1111/eureka/

#健康检查(需要spring-boot-starter-actuator依赖)

eureka.client.healthcheck.enabled= true

# 续约更新时间间隔(默认 30 秒)

eureka.instance.lease-renewal-interval-in-seconds= 10

# 续约到期时间(默认 90 秒)

eureka.instance.lease-expiration-duration-in-seconds= 10

 

#zuul代理配置 zuul.routes.服务名.path,服务名要与注册的一致

#应用名映射

zuul.routes.myspringboot.path=/myspringboot/**

zuul.routes.myspringboot.service-id=myspringboot

 

#url映射

#zuul.routes.myspringboot.path=/myspringboot/**

#zuul.routes.myspringboot-url.url=http: //localhost:10087/

自定义zuul过滤器

更多的检查规则后续慢慢健全

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

/**

  * zuul过滤器,实现了路由检查

  */

public class accessfilter extends zuulfilter {

 

   /**

    * 通过int值来定义过滤器的执行顺序

    */

   @override

   public int filterorder() {

     // predecoration之前运行

     return pre_decoration_filter_order - 1 ;

   }

 

   /**

    * 过滤器的类型,在zuul中定义了四种不同生命周期的过滤器类型:

    *   public static final string error_type = "error";

    *   public static final string post_type = "post";

    *   public static final string pre_type = "pre";

    *   public static final string route_type = "route";

    */

   @override

   public string filtertype() {

     return pre_type;

   }

 

   /**

    * 过滤器的具体逻辑

    */

   @override

   public object run() {

     requestcontext ctx = requestcontext.getcurrentcontext();

     httpservletrequest request = ctx.getrequest();

     system.out.println(string.format( "%s accessfilter request to %s" , request.getmethod(),request.getrequesturl().tostring()));

     string accesstoken = request.getparameter( "accesstoken" );

     //有权限令牌

     if (!stringutils.isempty(accesstoken)) {

       ctx.setsendzuulresponse( true );

       ctx.setresponsestatuscode( 200 );

       //可以设置一些值

       ctx.set( "issuccess" , true );

       return null ;

     } else {

       ctx.setsendzuulresponse( false );

       ctx.setresponsestatuscode( 401 );

       ctx.setresponsebody( "{\"result\":\"accesstoken is not correct!\"}" );

       //可以设置一些值

       ctx.set( "issuccess" , false );

       return null ;

     }

   }

 

   /**

    * 返回一个boolean类型来判断该过滤器是否要执行

    */

   @override

   public boolean shouldfilter() {

     return true ;

   }

}

启动类

添加@enablezuulproxy注解并使用自定义过滤器

?

1

2

3

4

5

6

7

8

9

10

11

12

13

@enablezuulproxy

@springbootapplication

public class zuulserverapplication {

 

   public static void main(string[] args) {

     springapplication.run(zuulserverapplication. class , args);

   }

 

   @bean

   public accessfilter accessfilter() {

     return new accessfilter();

   }

}

效果演示

启动所有项目,我们在eureka上注册了四个服务,相比上篇( springcloud系列——ribbon 负载均衡 )多了一个zuul

浏览器访问 http://localhost:10010/myspringboot/feign/ribbon、http://localhost:10010/myspringboot/feign/ribbon?accesstoken=123456

http://localhost:10010/ 这个端口对外暴露,相对于总入口,后面接不同的路径由,zuul路由到对应的服务上

1、没有accesstoken是,无法通过检查

2、携带accesstoken时,可正常路由,并且feign调用、ribbon负载均衡

后记

我们为什么要使用zuul呢?

1、请求校验、路由转发,接口校验与业务逻辑分离

2、隐藏诸多服务路径,只暴露统一入口,安全

更多zuul配置,请看官方文档

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://HdhCmsTestcnblogs测试数据/huanzi-qch/p/10142395.html

查看更多关于SpringCloud Zuul实现动态路由的详细内容...

  阅读:19次