Nginx + Zuul cluster to achieve high availability gateway

Code Reference: https: //github.com/HCJ-shadow/Zuul-Gateway-Cluster-Nginx


Zuul routing forwarding

Preparatory

  • Eureka build a service registry

  • Service provider msc-provider-5001 provides a hello request [for testing]


    Create a gateway-7001


  • pom-dependent
<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/
  • The main class {annotation @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);
    }
}

test

And then click Run Eureka, provider and gateway


Visit: http: // localhost: 2001 /

1566496950128

hello path access provider: http: // localhost: 5001 / hello

Gateway default mapping path: http: // localhost: 7001 / msc-provider / hello

Default routing rules: http: // zuulhost: zuulport / micro services on Eureka's serviceId / path

serviceId to lowercase.

1566497384654

Custom routing rules

The default routing rule are routed through the service name, or you can customize the name of the routing, enhanced security services.

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

User access:
HTTP: // localhost: 7001 / API-A / the Hello

You can also achieve these results. Essentially hidden micro service name.

1566586070236

Zuul filter function

zuul filters mainly used in authentication.


Creating a simple filter:

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;

    }
}

If not token words:

1566587036580

Plus a token:

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

1566588398996

In the actual development, user information and form information taken from the database matches, implement the authentication function.

Nginx + Zuul cluster to achieve high availability gateway

Two effects:

  • Nginx load balancing by polling
  • Zuul achieve high availability through clustering

Such a graph is roughly:

1566590072068

Realization of ideas:

zuul each gateway port except for the differences, other filters, the same route.

Create a gateway 7001,7002,7003 ports.


nginx configuration:

download:

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

1566590347942

modify

1566590372213

1566590616898

  #配置上游服务器网关端口集群,默认轮询机制
    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;
        }

test

Start Eureka

Start Provider

Start Zuul gateway cluster

Start Nginx


Nginx start :( double-click)

1566590678943

access

1566591108823

1566591154773

Polling to 7002


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

Polling to 7001

1566591226465

Guess you like

Origin www.cnblogs.com/noneplus/p/11403404.html