(五)SpringCloud代码实战之zuul网关

短命女 2022-10-05 08:59 223阅读 0赞

本文任务:
1、搭建zuul网关
2、nginx+zuul,可以实现网关集群

本文是在前文搭建的项目基础上构建的:
(一)springcloud实战代码之eureka注册中心
(二)springcloud实战之config配置中心
(三)SpringCloud实战之openfeign服务调用
(四)SpringCloud代码实战之hystrix熔断器

本文新建项目结构:
模块zuulServer-8060,启动类ZuulApplication,配置文件application.yml,过滤器类MyZuulFilter(继承ZuulFilter类)
在这里插入图片描述

一、搭建zuul网关

1.1、引入依赖

zuul网关依赖spring-cloud-starter-netflix-zuul,

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
  4. </dependency>

因为在父工程中声明了spring-cloud-dependencies(Hoxton.SR11)所以这里zuul的依赖不用写版本
在这里插入图片描述
其他依赖:
在这里插入图片描述

1.2、启动类

启动类上面加注解@EnableZuulProxy
在这里插入图片描述

1.3、zuul过滤器

新建过滤器类:MyZuulFilter,继承ZuulFilter,重写ZuulFilter的四个方法.。
参考:Zuul入门实战(完整版)

在这里插入图片描述
MyZuulFilter.class:

  1. package com.tzq.zuul.filter;
  2. import com.netflix.zuul.ZuulFilter;
  3. import com.netflix.zuul.context.RequestContext;
  4. import com.netflix.zuul.exception.ZuulException;
  5. import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
  6. import org.springframework.stereotype.Component;
  7. import javax.servlet.http.HttpServletRequest;
  8. /** * @Author tangzhiqian * @CreateTime 2021/6/7 14:48 */
  9. @Component
  10. public class MyZuulFilter extends ZuulFilter {
  11. @Override
  12. public String filterType() {
  13. return FilterConstants.PRE_TYPE;
  14. }
  15. @Override
  16. public int filterOrder() {
  17. return 0;
  18. }
  19. @Override
  20. public boolean shouldFilter() {
  21. return true;
  22. }
  23. @Override
  24. public Object run() throws ZuulException {
  25. RequestContext context = RequestContext.getCurrentContext();
  26. HttpServletRequest request = context.getRequest();
  27. System.out.println("执行了我的pre类型的zuul过滤器!!!");
  28. System.out.println("request.getMethod() = " + request.getMethod());
  29. System.out.println("request.getRequestURL() = " + request.getRequestURL());
  30. System.out.println("request.getRequestURI() = " + request.getRequestURI());
  31. System.out.println("request.getContextPath() = " + request.getContextPath());
  32. System.out.println("request.getCookies() = " + request.getCookies());
  33. return null;
  34. }
  35. }

1.4、配置文件

在这里插入图片描述
到这里,zuul网关已经搭建完成!
启动:注册中心,配置中心,stundent-service,zuulServer
打开浏览器:访问http://localhost:8060/mypath/student/selectAll
(zuulServer在8060端口跑)
转发成功:
在这里插入图片描述
与此同时,控制台输出:
在这里插入图片描述
说明过滤器也生效了!

二、nginx+zuul

在这里插入图片描述

2.1、安装nginx(此处是window版)

下载地址:http://nginx.org/en/download.html
在这里插入图片描述
解压后,一定要用cmd启动nginx!!!!!
在这里插入图片描述

启动是没有任何反应的,打开浏览器:http://localhost:80
在这里插入图片描述
说明安装成功。

2.2、配置nginx

在刚刚解压的nginx文件夹中:conf文件夹—>nginx.conf文件
其中weight表示权重,谁的权重大访问到的几率就大(多个服务权重可相同)
在这里插入图片描述
nginx.conf文件:

  1. #user nobody;
  2. worker_processes 1;
  3. #error_log logs/error.log;
  4. #error_log logs/error.log notice;
  5. #error_log logs/error.log info;
  6. #pid logs/nginx.pid;
  7. events {
  8. worker_connections 1024;
  9. }
  10. http {
  11. include mime.types;
  12. default_type application/octet-stream;
  13. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  14. # '$status $body_bytes_sent "$http_referer" '
  15. # '"$http_user_agent" "$http_x_forwarded_for"';
  16. #access_log logs/access.log main;
  17. sendfile on;
  18. #tcp_nopush on;
  19. #keepalive_timeout 0;
  20. keepalive_timeout 65;
  21. #gzip on;
  22. server {
  23. listen 80;
  24. server_name localhost;
  25. #charset koi8-r;
  26. #access_log logs/host.access.log main;
  27. location / {
  28. root html;
  29. index index.html index.htm;
  30. }
  31. #error_page 404 /404.html;
  32. # redirect server error pages to the static page /50x.html
  33. #
  34. error_page 500 502 503 504 /50x.html;
  35. location = /50x.html {
  36. root html;
  37. }
  38. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  39. #
  40. #location ~ \.php$ {
  41. # proxy_pass http://127.0.0.1;
  42. #}
  43. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  44. #
  45. #location ~ \.php$ {
  46. # root html;
  47. # fastcgi_pass 127.0.0.1:9000;
  48. # fastcgi_index index.php;
  49. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  50. # include fastcgi_params;
  51. #}
  52. # deny access to .htaccess files, if Apache's document root
  53. # concurs with nginx's one
  54. #
  55. #location ~ /\.ht {
  56. # deny all;
  57. #}
  58. }
  59. # another virtual host using mix of IP-, name-, and port-based configuration
  60. #
  61. #server {
  62. # listen 8000;
  63. # listen somename:8080;
  64. # server_name somename alias another.alias;
  65. # location / {
  66. # root html;
  67. # index index.html index.htm;
  68. # }
  69. #}
  70. # HTTPS server
  71. #
  72. #server {
  73. # listen 443 ssl;
  74. # server_name localhost;
  75. # ssl_certificate cert.pem;
  76. # ssl_certificate_key cert.key;
  77. # ssl_session_cache shared:SSL:1m;
  78. # ssl_session_timeout 5m;
  79. # ssl_ciphers HIGH:!aNULL:!MD5;
  80. # ssl_prefer_server_ciphers on;
  81. # location / {
  82. # root html;
  83. # index index.html index.htm;
  84. # }
  85. #}
  86. #配置上游服务器网关端口集群
  87. upstream zuulServer{
  88. server 127.0.0.1:8060 weight=1;
  89. }
  90. server {
  91. listen 80;
  92. server_name www.tzq.com;
  93. location / {
  94. ### 指定上游服务器负载均衡服务器
  95. proxy_pass http://zuulServer/;
  96. index index.html index.htm;
  97. }
  98. error_page 500 502 503 504 /50x.html;
  99. location = /50x.html {
  100. root html;
  101. }
  102. }
  103. }

配置完,重新加载一下nginx:
nginx -s reload
在这里插入图片描述

2.3、运行

http://www.tzq.com/mypath/student/selectAll
在这里插入图片描述
成功!!!

发表评论

表情:
评论列表 (有 0 条评论,223人围观)

还没有评论,来说两句吧...

相关阅读

    相关 springcloud学习zuul

    uul网关有路由和过滤器等作用 简单使用路由部分功能: 路由功能:使用户不直接访问服务,而是访问网关,网关在去eureka上拉去服务然后比对用户请求是要请求哪个服务,...

    相关 SpringCloud-zuul

    网关的作用:就相当于整个微服务体系的入口,所有的外部请求都应该通过zuul进行处理,zuul会通过请求url分析应该将请求分发给哪个微服务处理,微服务处理完毕之后,会将结果返回

    相关 springcloud zuul

    网关可以对请求进行过滤拦截,转发等,这种跟过滤器不一样,它可以拦截入口,而不是单独一个服务。 业务场景,会员服务和订单服务,用户必须登录才能调用相关接口(即含有userTok

    相关 SpringCloud路由zuul

    在微服务架构中,需要几个基础的服务治理组件,包括服务注册与发现、服务消费、负载均衡、断路器、智能路由、配置管理等,由这几个基础组件相互协作,共同组建了一个简单的微服务系统。一个