SpringCloud- 接口网关(zuul)

1.什么是网关

大家都知道,从一个房间走到另一个房间,必然要经过一扇门。同样,从一个网络向另一个网络发送信

息,也必须经过一道“关口”,这道关口就是网关。网关 就是一个网络连接到另一个网络的“关口”。也就是网络关卡。例如:我的一个pc工程的ip地址:192.168.1.215 然后另一个h5工程的ip地址是192.168.2.114 如果pc工程想调用h5

工程 属于不在同一个网段之中,跨域问题,需要搭建接口网关!

 

2.如何使用SpringCloud-zuul 搭建一个接口网关

 接口网关:拦截请求,nginx也有此作用,比如防止ddos攻击

 

 

 使用接口网关以后 pc工程只需要访问地址:www.zull.com/member 则直接有接口网关根据请求地址 直接找到 www.member.com 和nginx搭建企业网关类似

 

3.使用java代码实现接口网关

 创建项目:zuul-server

步骤一:pom文件导入所需要的依赖

<?xml-stylesheet type="text/xsl" href="XXX.xsl"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.jiahou</groupId>
    <artifactId>zull-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!--eureka server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</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-zuul</artifactId>
        </dependency>
        <!-- spring boot test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.RC1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>


</project>

步骤二:创建zull-server的yml配置文件:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8888/eureka/
server:
  port: 8799
spring:
  application:
    name: service-zuul
zuul:
  routes:
    api-a:
      path: /api-member/**
      service-id: service-member
    api-b:
      path: /api-order/**
      service-id: service-order 

 

ps:path:就是地址请求路径 比如请求地址:127.0.0.1:8799/api-member/method 首先 访问zull服务  zull服务根据配置对所有/api-member/进行拦截,然后请求的地址对应的是eureka中service-member的实际地址

 

步骤三:在member服务和order服务中中添加测试方法:

 

    @RequestMapping("/getMenberZull")
    public String getMemberMsg1() {
        String str = "这个是会员服务";
        return str;
    }
@RequestMapping("/getMenberZull")
    public String getMemberMsg1() {
        String str = "这个是订单服务";
        return str;
    }

 

 

步骤四:启动eureka,member,order,zull服务

 

 

 

 步骤五:分别输入请求地址:http://127.0.0.1:8799/api-member/getMenberZullhttp://127.0.0.1:8799/api-order/getMenberZull

 

 

 

 

 

 

 

 

4.使用zull接口网关过滤请求参数

 在nginx里面可以搭建企业的黑白名单,过滤请求参数,spirngcloud中zull过滤请求参数

  创建一个自己的filter继承ZuulFilter

    

package com.example.demo;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;

@Component
public class MyZuulFilter extends ZuulFilter {

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

    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    public boolean shouldFilter() {
        return true;
    }

    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        log.info(String.format("当前接受方法类型:{}和请求地址", request.getMethod(), request.getRequestURL().toString()));
        Object accessToken = request.getParameter("id");
        if (accessToken != null) {
            return null;
        }
        log.warn("当前没有任何参数");
        ctx.setSendZuulResponse(false);
        ctx.setResponseStatusCode(401);
        try {
            ctx.getResponse().getWriter().write("id is null");
        } catch (Exception e) {
        }
        return null;

    }
}

 

 

 

 

 

 

 

 ps:nginx有的功能 zuul基本都有 

  纠正:zull->zuul

 

posted on 2019-05-14 16:33  年少不知愁  阅读(1423)  评论(0编辑  收藏  举报