《springcloud学习》 二十二 nginx+zuul实现网关集群

1.准备

       (1) 互联网公司都适用集群

服务器端口
nginx8000
zuul主81
zuul从80

2.配置

               2.1  nginx

                       nginx下载


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
	##配置上游服务器
	upstream fqyd {
		server 127.0.0.1:81;
		server 127.0.0.1:82;
    }

    server {
        listen       8000;
        server_name  fqyd.com;

        location / {
            proxy_pass http://fqyd/;
            index  index.html index.htm;
        }
    }
}

                     2.2  window域名配置

           

注意,如果遇到无法修改的情况,查看另外一个博客

https://blog.csdn.net/qq_16855077/article/details/103085737 

           2.3 zuul代码变动

package com.fqyd.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * Description:
 * Author: wude
 * Date:  2019/11/8 16:18
 * Modified By:
 */
@Component
public class TokenFilter extends ZuulFilter {
   @Value("${server.port}")
    private Integer port;

    @Override
    public String filterType() {
        return "pre";// 前置过滤器
    }

    @Override
    public int filterOrder() {
        return 0; //优先级为0,数字越大,优先级越低
    }

    @Override
    public boolean shouldFilter() {
        return true;    //是否执行该过滤器,此处为true,说明需要过滤
    }

    @Override
    public Object run() throws ZuulException {
        //取到请求上下文
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        String token = request.getParameter("token");
        if(StringUtils.isEmpty(token)){
            ctx.setSendZuulResponse(false);
            ctx.setResponseBody("token is empty");
            ctx.setResponseStatusCode(401);
        }/*else{
            ctx.setSendZuulResponse(true);
            ctx.setResponseStatusCode(200);
            ctx.setResponseBody("token succ");// 输出最终结果
        }*/
        System.out.println("经过zuul转发端口"+port);
        return null;
    }
}

3.测试

启动eureka和zuul主从,会员服务

注意:zuul写一个服务就行,通过idea,修改配置,启动两次就行。idea一个服务怎么运行两篇,查看博客https://blog.csdn.net/qq_16855077/article/details/99292037

 

因80端口被占用,所以nginx使用8000端口

谷歌浏览器测试发现,而82没有任何的输出

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值