WEB网关系列02-基于http协议实现流量的代理

基于http协议实现流量的代理 模式之间差异 直连模式 代理模式 直连模式 直连模式,也就是服务与服务之间直接请求调用,比如说我们正常在浏览器上面输入baidu,然后浏览器把百度相应的响应反馈回来的过程。 这个过程种,用户充当了service-A的角色,百度的服务器充当了service-B的角色。 代码还原以上过程,利用java的Java.net.HttpURLConnection类实现网络访问 除了HttpURLConnection还有很多java封装的包也能实现网络访问 通过common封装好HttpClient; 通过 Apache 封装好CloseableHttpClient; 通过SpringBoot-RestTemplate; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets; public class DirectMode { public static void main(String[] args) { String api = "https://www.baidu.com"; HttpURLConnection connection = null; InputStream in = null; BufferedReader reader = null; try { //构造一个URL对象 URL url = new URL(api); //获取URLConnection对象 connection = (HttpURLConnection) url.openConnection(); //getOutputStream会隐含的进行connect(即:如同调用上面的connect()方法,所以在开发中不调用connect()也可以) in = connection.

事件日志埋点

通过自定义注解和AOP实现自定义事件日志埋点 注解和aop类 注解 @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface EventLog { Action action() default Action.DEFAULT; boolean recordUserId() default true; boolean recordUserName() default true; boolean recordUserPhone() default true; boolean recordTime() default true; } aop @Slf4j @Aspect @Order(1) public class EventLogAspect { public EventLogAspect() { } @Pointcut("@annotation(com.xxxx.EventLog)") private void eventLogPoint() { } @Around("eventLogPoint() && @annotation(eventLog)") public Object around(ProceedingJoinPoint pjp, EventLog eventLog) throws Throwable { Object[] args = pjp.getArgs(); if (args[0] instanceof Action.Param){ Action.Param param = (Action.

WEB网关系列01-什么是WEB网关

WEB网关系列-什么是WEB网关 gateway,我们一般都称为网关。翻译意思有网关;(通往其他地区的)门户;方法;途径;出入口;手段;门道;大门口。 现实生活中的网关有比如,高速公路的收费口、城门入口、景区或者游乐园的入口等。它们都充当了入口的功能,还有检测功能等等。 而在web中的网关,它充当的是流量的入口,一般来说当有流量进入到web程序,它的第一层便是经过网关,然后经网关代理到准确的服务地址上, 同时,对应的服务产生响应,然后网关把响应反馈回去。那么网关就充当了,流量转发的角色, 个人觉得它是一个网关最核心的功能。 在流量转发这个场景下,可以衍生出无数种组合出来。因为,我们的web应用程序存在的方式是多种多样的,比如在线购物网站、wps、视频网站等等。当然市面上常用的就是基于http协议的web服务, 还有tcp、udp、websocket等。 除了流量转发,网关还需要限制恶意的请求过来,仅正确的用户、正确的请求才可以通过,也就是网关需要提供鉴权的功能。 网关还有就是限制请求流量的作用,举个例子来说,当一台计算机器它的瞬时处理能力只有1千个请求,那瞬时有2千个请求过来,网关处理不过来的话, 限流就在这种场景下面产生了。

Reactor Pattern

Intent 意图 The Reactor design pattern handles service requests that are delivered concurrently to an application by one or more clients. 反应器设计模式处理由一个或多个客户端并发传递到应用程序的服务请求, The application can register specific handlers for processing which are called by reactor on specific events. 应用程序可以注册用于处理的特定处理程序,这些处理程序由反应器在特定事件上调用 Dispatching of event handlers is performed by an initiation dispatcher, which manages the registered event handlers. 事件处理程序的分派由启动分派程序执行,该程序管理已注册的事件处理程序 Demultiplexing of service requests is performed by a synchronous event demultiplexer. 服务请求的解复用由同步事件解复用器执行 Class diagram 类图 Applicability 应用 Use Reactor pattern when 以下场景可以使用Reactor 模式

Scalable in java 翻译

目录 Scalable network services 可扩展的网络服务 Event-driven processing 事件驱动处理 Reactor pattern Reactor 模式 Basic version 基本版本 Multithreaded versions 多线程版本 Other variants 其他变体 Walkthrough of java.nio nonblocking IO APIs 预编排java.nio nonblocking IO APIs Network Services 网络服务 Web services, Distributed Objects, etc 网络服务,分布式对象,等等。 Most have same basic structure: 大多数都有以下的基本结构 Read request 读请求 Decode request 解码请求 Process service 处理服务 Encode reply 编码响应 Send reply 发送响应 But differ in nature and cost of each step 当然在实际应用中每一步的运行效率都是不同的,例如其中可能涉及到xml解析、文件传输、web页面的加载、计算服务等不同功能 XML parsing, File transfer, Web page generation, computational services, … Classic Service Designs 传统的服务设计 Each handler may be started in its own thread 在一般的网络服务当中都会为每一个连接的处理开启一个新的线程,我们可以看下大致的示意图: public class Test { private static Integer PORT = 8080; public static void main(String[] args) { Server server = new Server(); server.

什么是metadata

什么是metadata,带例子 metadata is simply data about data,It means it is a description and context of the data. It helps to organize, find and understand data. Here are a few real world examples of metadata. 元数据是数据的数据,这意味着他是数据的描述和上下文。它帮助我们了解和认识数据。下面有一些真是的关于元数据的例子。 Typical metadata Those are some typical metadata elements: Title and description, Tags and categories, Who created and when, Who last modified and when, Who can access or update. A photo Every time you take a photo with today’s cameras a bunch of metadata is gathered and saved with it