Zuul gateway of springcloud

aims

1. Introduction and basic use of Zuul routing gateway
2. Zuul routing mapping configuration
3. Zuul request filtering configuration

Introduction and basic use of Zuul routing gateway

Introduction Please refer to the picture above for the introduction of
Zuul API routing gateway service
Insert picture description here
. The API routing gateway service here is implemented by Zuul. It is mainly used to route and filter requests when providing service interfaces to the outside, and therefore can hide the interface details of internal services. Has always been beneficial to protect the security of the system;

Routing configuration
Zuul routing configuration

We create a new module microservice-zuul-3001,
where our zuul is also registered in the eureka service, port 3001;
we modify the Hosts, specifically for zuul to create a local domain name mapping
hosts file and add:
127.0.0.1 zuul.xieminglu.com
Insert picture description here
and then pom.xml to add:

<!--zuul网关-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>

Complete pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.xieminglu</groupId>
        <artifactId>testmicroservice</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>microservice-zuul-3001</artifactId>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>

        <!-- actuator监控 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- hystrix容错 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <!--zuul网关-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zuul</artifactId>
        </dependency>

        <!--ribbon相关依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <!--引入Feign依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application.yml

server:
  port: 3001
  context-path: /
spring:
  application:
    name: microservice-zuul
eureka:
  instance:
    instance-id: microservice-zuul:3001
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://eureka2001.xieminglu.com:2001/eureka/,http://eureka2002.xieminglu.com:2002/eureka/,http://eureka2003.xieminglu.com:2003/eureka/
info:
  groupId: com.xieminglu.testmicroservice
  artifactId: microservice-zuul-3001
  version: 1.0-SNAPSHOT
  userName: http://xieminglu.com
  phone: 123456

Create a main startup class: ZuulApplication_3001
plus @EnableZuulProxy annotation

package com.xieminglu.microservicezuul3001;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
@EnableZuulProxy
public class MicroserviceZuul3001Application {

    public static void main(String[] args) {
        SpringApplication.run(MicroserviceZuul3001Application.class, args);
    }

}

Let's test:
start three eureka and then start the next 1001 service, and zuul gateway service;
Insert picture description here
there are two services;

We directly request: http://localhost:1006/student/list can get the data;
we use http://zuul.xieminglu.com:3001/microservice-student/student/list domain name + port + service name + request address Can also request data;

Explain that our basic routing configuration is OK

Zuul route mapping configuration

The above is the simple use of zuul. From the interface address, the unique identification name of the service provider microservice-student is easily exposed; there is a security risk, we need to hide it;
the function of ignored-services is to make the original service provider unique The distinguished name is disabled;
the role of Prefix is ​​to
add the following configuration to the service prefix yml file:

zuul:
  routes:
    studentServer.serviceId: microservice-student
    studentServer.path: /studentServer/**
  ignored-services: "*"
  prefix: /xieminglu

After configuration, you can test through the following link
http://zuul.xieminglu.com:3001/microservice-student/student/list
http://zuul.xieminglu.com:3001/studentServer/student/list
http://zuul .xieminglu.com:3001/xieminglu/microservice-student/student/list
http://zuul.xieminglu.com:3001/xieminglu/studentServer/student/list

Insert picture description here
The above error page will appear in the corresponding configuration, which is normal.

Zuul request filtering configuration

For example, we need authentication to log in to a certain system, username and password;

When we request services, we can also set up identity verification, which is to filter illegal requests; Zuul is implemented through ZuulFilter;
generally, if it is implemented specifically, we will verify the validity of the tokens brought by each time through the Zuul service gateway;

We first define an AccessFilter class:

package com.xieminglu.microservicezuul3001.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletRequest;

public class AccessFilter extends ZuulFilter {

    Logger logger=Logger.getLogger(AccessFilter.class);

    /**
     * 判断该过滤器是否要被执行
     */
    @Override
    public boolean shouldFilter() {
        return true;
    }

    /**
     * 过滤器的具体执行逻辑
     */
    @Override
    public Object run() throws ZuulException {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        String parameter = request.getParameter("accessToken");
        logger.info(request.getRequestURL().toString()+" 请求访问");
        if(parameter==null){
            logger.error("accessToken为空!");
            ctx.setSendZuulResponse(false);
            ctx.setResponseStatusCode(401);
            ctx.setResponseBody("{\"result\":\"accessToken is empty!\"}");
            return null;
        }
        //  token判断逻辑
        logger.info(request.getRequestURL().toString()+" 请求成功");
        return null;
    }

    /**
     * 过滤器的类型 这里用pre,代表会再请求被路由之前执行
     */
    @Override
    public String filterType() {
        return "pre";
    }

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

}

Then turn on the Filter configuration:

package com.xieminglu.microservicezuul3001.config;

import com.xieminglu.microservicezuul3001.filter.AccessFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ZuulConfig {

    @Bean
    public AccessFilter accessFilter(){
        return new AccessFilter();
    }
}

The browser enters the address to test
http://zuul.xieminglu.com:3001/xieminglu/studentServer/student/list
http://zuul.xieminglu.com:3001/xieminglu/studentServer/student/list?accessToken=1
Test result as follows
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/xieminglu/article/details/103489895