60

Nginx+Zuul集群实现高可用网关

 5 years ago
source link: https://www.tuicool.com/articles/2IruMru
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

代码参考:https://github.com/HCJ-shadow/Zuul-Gateway-Cluster-Nginx

Zuul的路由转发功能

前期准备

  • 搭建Eureka服务注册中心

  • 服务提供者msc-provider-5001【提供一个hello请求做测试】

    创建gateway-7001

  • pom依赖
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
  • yaml
server:
  port: 7001
spring:
  application:
    name: zuul-gateway

eureka:
  client:
    service-url:
      defaultZone: http://eureka2001.com:2001/eureka/,http://eureka2002.com:2002/eureka/,http://eureka2003.com:2003/eureka/
  • 主类{注解@EnableZuulProxy}
package zkrun.top;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class App_gateway_7001 {
    public static void main(String[] args) {
        SpringApplication.run(App_gateway_7001.class,args);
    }
}

测试

依次运行Eureka,provider和gateway

访问:http://localhost:2001/

nmErEvb.png!web

访问提供者的hello路径:http://localhost:5001/hello

网关默认的映射路径:http://localhost:7001/msc-provider/hello

默认路由规则:http://zuulhost:zuulport/微服务在Eureka上的serviceId/路径

serviceId小写即可。

FjmqEjr.png!web

自定义路由规则

默认的路由规则是通过服务名称来路由的,也可以自定义路由的名称,增强服务的安全性。

zuul:
  routes:
    api-a:
      path: /api-a/**
      serviceId: msc-provider
#    api-b:
#      path: /api-b/**
#      serviceId: service-feign

用户访问:

http://localhost:7001/api-a/hello

也可达到上述效果。本质上就是隐藏了微服务名称。

NJrMRbz.png!web

Zuul的过滤器功能

zuul的过滤器主要用在身份验证上。

创建一个简单的过滤器:

package zkrun.top.filter;


import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
public class MyFilter extends ZuulFilter {

    private static Logger log = LoggerFactory.getLogger(MyFilter.class);

    /**
     * filterType:返回一个字符串代表过滤器的类型,在zuul中定义了四种不同生命周期的过滤器类型,具体如下:
     * pre:路由之前
     * routing:路由之时
     * post: 路由之后
     * error:发送错误调用
     * @return
     */
    @Override
    public String filterType() {
        return "pre";
    }

    /**
     * filterOrder:过滤的顺序
     * @return
     */
    @Override
    public int filterOrder() {
        return 0;
    }


    /**
     * shouldFilter:这里可以写逻辑判断,是否要过滤,本文true,永远过滤。
     * @return
     */
    @Override
    public boolean shouldFilter() {
        return true;
    }

    /**
     * run:过滤器的具体逻辑。可用很复杂,包括查sql,nosql去判断该请求到底有没有权限访问。
     * @return
     */
    @Override
    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        System.out.println(request);

        Object  token = request.getParameter("token");
        System.out.println(token);
        if(token == null) {
            log.info("fail");
            ctx.setSendZuulResponse(false);
            ctx.setResponseStatusCode(401);//401表示无权限
            try {
                ctx.getResponse().getWriter().write("token is empty");
            }catch (Exception e)
            {}
            return null;
        }
        log.info("pass");
        return null;

    }
}

如果不加token的话:

UZNbEjy.png!web

加了token:

http://localhost:7001/api-a/hello?token=1234

nuAraeM.png!web

在实际开发中,从数据库中取出用户信息和表单信息进行匹配,实现鉴权功能。

Nginx+Zuul集群实现高可用网关

两个作用:

  • Nginx通过轮询实现负载均衡
  • Zuul通过集群实现高可用

大致是这样一个图:

feqmmmV.png!web

实现思路:

zuul网关每个除了端口的差异之外,其他的过滤器,路由都相同。

创建7001,7002,7003端口的网关。

nginx配置:

下载:

http://nginx.org/en/download.html

NnyeYvr.png!web

修改

vu2ma2Y.png!web

mQVjMrf.png!web

#配置上游服务器网关端口集群,默认轮询机制
    upstream  backServer{
        server 127.0.0.1:7001 weight=1;
        server 127.0.0.1:7002 weight=1;
        server 127.0.0.1:7003 weight=1;

    }

    server {
        listen       80;
        server_name  nginxtest.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            ### 指定上游服务器负载均衡服务器
            proxy_pass http://backServer/;
            index  index.html index.htm;
        }

测试

启动Eureka

启动Provider

启动Zuul网关集群

启动Nginx

Nginx启动:(双击)

nqeq6rz.png!web

访问

iIRFNjb.png!web

ZnA7Rjv.png!web

轮询到7002

http://nginxtest.com/api-a/hello?token=2

轮询到7001

QNVFJjN.png!web


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK