zoukankan      html  css  js  c++  java
  • springcloud第五步:使用Zuul搭建服务接口网关

    路由网关(zuul)

    什么是网关

    Zuul的主要功能是路由转发和过滤器。路由功能是微服务的一部分,比如/api/user转发到到user服务,/api/shop转发到到shop服务。zuul默认和Ribbon结合实现了负载均衡的功能, 类似于nginx转发。

    搭建SpringCloud网关

    创建工程service-zuul

    Maven依赖

    创建工程service-zuul

    <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>

                <dependency>

                      <groupId>org.springframework.cloud</groupId>

                      <artifactId>spring-cloud-starter-eureka</artifactId>

                </dependency>

                <dependency>

                      <groupId>org.springframework.cloud</groupId>

                      <artifactId>spring-cloud-starter-zuul</artifactId>

                </dependency>

                <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>

          </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>

    application.yml配置

    eureka:

      client:

        serviceUrl:

          defaultZone: http://localhost:8888/eureka/

    server:

      port: 8769

    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

    发送请求http://127.0.0.1:8769/api-member/getMemberAll

    转发到http://127.0.0.1:8762/getMemberAll

    开启网关 @EnableZuulProxy

    服务过滤

    @Component

    public class MyFilter extends ZuulFilter {

     

          private static Logger log = LoggerFactory.getLogger(MyFilter.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("%s >>> %s", request.getMethod(), request.getRequestURL().toString()));

                Object accessToken = request.getParameter("token");

                if (accessToken != null) {

                      return null;

                }

                log.warn("token is empty");

                ctx.setSendZuulResponse(false);

                ctx.setResponseStatusCode(401);

                try {

                      ctx.getResponse().getWriter().write("token is empty");

                } catch (Exception e) {

                }

                return null;

     

          }

    }

    如果请求参数中没有传入token参数 直接返回报错信息

     

    传入token参数可以正常访问请求

  • 相关阅读:
    可以多个广告浮动的页面
    获取内容并截取长度用...替换
    用UIManager使Swing界面转换风格。
    Java FileInputStream
    win7 部署tomcat总结
    观察者模式
    Java RandomAccessFile
    Java 客户端界面功能:停止当前操作
    多线程的开启与管理
    log4j内容记录
  • 原文地址:https://www.cnblogs.com/XJJD/p/10450196.html
Copyright © 2011-2022 走看看