当前位置: 首页 > news >正文

java B2B2C Springboot电子商城系统-路由网关(zuul)

一、Zuul简介

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

需要JAVA Spring Cloud大型企业分布式微服务云构建的B2B2C电子商务平台源码 一零三八七七四六二六

zuul有以下功能:

Authentication

Insights

Stress Testing

Canary Testing

Dynamic Routing

Service Migration

Load Shedding

Security

Static Response handling

Active/Active traffic management

二、创建service-zuul工程

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.forezp</groupId>
    <artifactId>service-zuul</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>service-zuul</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.forezp</groupId>
        <artifactId>sc-f-chapter5</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>
    </dependencies>
</project>


复制代码

在其入口applicaton类加上注解@EnableZuulProxy,开启zuul的功能:

@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
@EnableDiscoveryClient
public class ServiceZuulApplication {

    public static void main(String[] args) {
        SpringApplication.run( ServiceZuulApplication.class, args );
    }
}
复制代码

加上配置文件application.yml加上以下的配置代码:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8769
spring:
  application:
    name: service-zuul
zuul:
  routes:
    api-a:
      path: /api-a/**
      serviceId: service-ribbon
    api-b:
      path: /api-b/**
      serviceId: service-feign
复制代码

首先指定服务注册中心的地址为http://localhost:8761/eureka/,服务的端口为8769,服务名为service-zuul;以/api-a/ 开头的请求都转发给service-ribbon服务;以/api-b/开头的请求都转发给service-feign服务;

依次运行这五个工程;打开浏览器访问:http://localhost:8769/api-a/hi?name=forezp ;浏览器显示:

hi forezp,i am from port:8762

打开浏览器访问:http://localhost:8769/api-b/hi?name=forezp ;浏览器显示:

hi forezp,i am from port:8762

这说明zuul起到了路由的作用

三、服务过滤

zuul不仅只是路由,并且还能过滤,做一些安全验证。继续改造工程;

@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;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    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) {
            log.warn("token is empty");
            ctx.setSendZuulResponse(false);
            ctx.setResponseStatusCode(401);
            try {
                ctx.getResponse().getWriter().write("token is empty");
            }catch (Exception e){}

            return null;
        }
        log.info("ok");
        return null;
    }
}
复制代码

filterType:返回一个字符串代表过滤器的类型,在zuul中定义了四种不同生命周期的过滤器类型,具体如下:

pre:路由之前

routing:路由之时

post: 路由之后

error:发送错误调用

filterOrder:过滤的顺序

shouldFilter:这里可以写逻辑判断,是否要过滤,本文true,永远过滤。

run:过滤器的具体逻辑。可用很复杂,包括查sql,nosql去判断该请求到底有没有权限访问。

这时访问:http://localhost:8769/api-a/hi?name=forezp ;网页显示:

token is empty

访问 http://localhost:8769/api-a/hi?name=forezp&token=22 ;

网页显示:

hi forezp,i am from port:8762

 java B2B2C springmvc mybatis电子商务平台源码 

转载于:https://juejin.im/post/5ca56c0cf265da307517e596

相关文章:

  • 我们用5分钟写了一个跨多端项目
  • Ubuntu MATE 推出树莓派版本
  • 【本人秃顶程序员】SpringBoot基础之banner玩法解析
  • 红米6.0系统设备最完美激活Xposed框架的流程
  • 微软宣布Azure Functions正式支持Java
  • 常用网络设备
  • Mysql5.7 - 一键安装脚本
  • 一、python小功能记录——监听键盘事件
  • note_4.10
  • jstl使用中的错误----基于idea
  • python 计算机基础
  • 数据流中的中位数(未)
  • jeecg入门操作—菜单管理
  • 解决AutoComplete数据过多时的卡顿问题
  • postgresql行列转换函数
  • [译] 理解数组在 PHP 内部的实现(给PHP开发者的PHP源码-第四部分)
  • Angular 响应式表单之下拉框
  • CSS实用技巧
  • download使用浅析
  • electron原来这么简单----打包你的react、VUE桌面应用程序
  • ESLint简单操作
  • FineReport中如何实现自动滚屏效果
  • Java Agent 学习笔记
  • Java比较器对数组,集合排序
  • java架构面试锦集:开源框架+并发+数据结构+大企必备面试题
  • rabbitmq延迟消息示例
  • SQLServer之创建显式事务
  • STAR法则
  • SwizzleMethod 黑魔法
  • vue-router的history模式发布配置
  • webpack入门学习手记(二)
  • 不上全站https的网站你们就等着被恶心死吧
  • 面试遇到的一些题
  • 配置 PM2 实现代码自动发布
  • 试着探索高并发下的系统架构面貌
  • 再谈express与koa的对比
  • 智能合约开发环境搭建及Hello World合约
  • PostgreSQL之连接数修改
  • #《AI中文版》V3 第 1 章 概述
  • $.proxy和$.extend
  • (0)Nginx 功能特性
  • (1)Map集合 (2)异常机制 (3)File类 (4)I/O流
  • (4)(4.6) Triducer
  • (Arcgis)Python编程批量将HDF5文件转换为TIFF格式并应用地理转换和投影信息
  • (floyd+补集) poj 3275
  • (附源码)spring boot校园拼车微信小程序 毕业设计 091617
  • (附源码)springboot课程在线考试系统 毕业设计 655127
  • (四)模仿学习-完成后台管理页面查询
  • (转)创业家杂志:UCWEB天使第一步
  • .【机器学习】隐马尔可夫模型(Hidden Markov Model,HMM)
  • .NET Core Web APi类库如何内嵌运行?
  • .NET Core 中的路径问题
  • .NET 动态调用WebService + WSE + UsernameToken
  • .NET 使用 ILMerge 合并多个程序集,避免引入额外的依赖
  • .NET/C# 利用 Walterlv.WeakEvents 高性能地定义和使用弱事件