Microservices-webapi implementation, separation from iis, and tomcat-webapitomcat

Source: Internet
Author: User

Microservices-webapi implementation, separation from iis, and tomcat-webapitomcat
Preface

Microservice, as its name implies, is a tiny, single service program, a single process, a single release, development and deployment can be independent;

This is my understanding;

However, web-based services, such as webservice and webapi, must be based on iis or tomcat,

This article describes how to deploy plain text programs based on http services by bypassing iis or tomc;

The so-called plaintext program is the source code release; of course, you do not need to release the source code, this can be your personal needs;

Preface

Do not ask me why I do not directly use the ready-made web architecture for development and deployment,

First, my project has such a requirement. It was originally a service program that provided socket tcp protocol data services and socket http protocol services;

Second, sort out your own knowledge;

Third: strengthen and simulate the microservice and webapi concepts;

Start of Text

Before reading this text, we recommend that you first check previous articles, such as threads, scripts, and socket services;

This article begins with the java version. C # Will be written later, but the code mechanism is the same. This program is used to style, and java and C # versions coexist;

My code style habits ,:

The main package of the project contains the function block implementation. The function block contains the singleton manager. If you need a script interface to handle the Processing Form of interfaces contained in iseries

Create an IWebApiScript file first.

1 package net. sz. game. test. webapi. iseries; 2 3 import net. sz. game. engine. nio. nettys. http. nioHttpRequest; 4 import net. sz. game. engine. scripts. IBaseScript; 5 6/** 7*8 * <br> 9 * author lost programmers <br> 10 * blog http://www.cnblogs.com/ty408/ <br> 11 * mail 492794628@qq.com <br> 12 * phone 13882122019 <br> 13 */14 public interface IWebApiScript extends IBaseScript {15 16/** 17 * Get route settings. Automatically registered Version 18*19 * @ return20 */21 String getRoute (); 22 23 boolean action (String url, NioHttpRequest request); 24 25}
View Code

 

Create WebApiManager

1 package net. sz. game. test. webapi; 2 3 import java. util. arrayList; 4 import net. sz. game. engine. nio. nettys. nettyPool; 5 import net. sz. game. engine. nio. nettys. http. nettyHttpServer; 6 import net. sz. game. engine. nio. nettys. http. nioHttpRequest; 7 import net. sz. game. engine. nio. nettys. http. handler. IHttpHandler; 8 import net. sz. game. engine. scripts. IBaseScript; 9 import net. sz. game. engine. scripts. manager. scri PtManager; 10 import net. sz. game. engine. szlog. szLogger; 11 import net. sz. game. test. webapi. iseries. IWebApiScript; 12 13/* 14*15 * <br> 16 * author lost programmers <br> 17 * blog http://www.cnblogs.com/ty408/ <br> 18 * mail 492794628@qq.com <br> 19 * phone 13882122019 <br> 20 */21 public class WebApiManager {22 23 private static SzLogger log = SzLogger. getLogger (); 24 private static final WebApiManager IN_ME = new WebAp IManager (); 25 26 public static WebApiManager getInstance () {27 return IN_ME; 28} 29 30 public void init () {31/* GET http binding */32 NettyHttpServer addBindHttpServer = NettyPool. getInstance (). addBindHttpServer ("127.0.0.1", 9527); 33 34/* Add blacklist to prohibit access list */35 // addBindHttpServer. addBlackIP (""); 36/* the access to the White List must contain */37 // addBindHttpServer. addWhiteIP (""); 38 addBindHttpServer. addHttpBind (new IHttpHandler () {/* use an anonymous object */39 40 @ Override41 public void run (String url, NioHttpRequest request) {42/* get the latest collection every time */43 ArrayList <IBaseScript> evts = ScriptManager. getInstance (). getBaseScriptEntry (). getEvts (iwebapis.pdf. class. getName (); 44 if (evts! = Null &&! Evts. isEmpty () {45 for (int I = 0; I <evts. size (); I ++) {46 iwebapis?iwebapis=( iwebapis=) evts. get (I); 47/* Find the plaintext script webapi route to be processed */48 if (iWebApiScript. getRoute (). equalsIgnoreCase (url) {49/* The returned value also has other purposes */50 boolean action = iwebapisworkflow. action (url, request); 51/* Skip loop */52 break; 53} 54} 56/* reply to client request */57 request. respons (); 58} 59}/* Handler */, 10/* increase the number of threads for concurrent processing */, "*"/* listener status */); 60/* socket use */61 addBindHttpServer. start (4); 62} 63}

 

Currently, we have not implemented route registration,

Let's see what the results will look like;

 

No replies are generated when we enter them in the browser;

Next, we will try the route Automatic Registration effect of the plaintext script to reply to the request;

Create a project to keep the project path unchanged;

Net. sz. game. test. scripts

Create an index route listener in the webapi Script directory.

 

1 package net. sz. game. test. scripts. webapi; 2 3 import net. sz. game. engine. nio. nettys. http. nioHttpRequest; 4 import net. sz. game. engine. szlog. szLogger; 5 import net. sz. game. test. webapi. iseries. IWebApiScript; 6 7/** 8*9 * <br> 10 * author lost programmers <br> 11 * blog http://www.cnblogs.com/ty408/ <br> 12 * mail 492794628@qq.com <br> 13 * phone 13882122019 <br> 14 */15 public class IndexScript implements iwebapisments {16 17 private static SzLogger log = SzLogger. getLogger (); 18 19 @ Override20 public String getRoute () {21 return "index"; 22} 23 24 @ Override25 public boolean action (String url, NioHttpRequest request) {26/* Add the output content, html data, json data, txt data, and xml data. Define the data as needed */27 request. addContent ("I Am a plaintext webapi automatically registering a route"); 28 return true; 29} 30 31}

 

 

 

To automatically register a route, we need to add a script to start the main function to initialize the function call.

1 package net. sz. game. test. main; 2 3 import java. util. arrayList; 4 import net. sz. game. engine. scripts. manager. scriptManager; 5 import net. sz. game. engine. szlog. szLogger; 6 import net. sz. game. test. webapi. webApiManager; 7 8/** 9*10 * <br> 11 * author missing programmers <br> 12 * mail 492794628@qq.com <br> 13 * phone 13882122019 <br> 14 */15 public class test_App_Manager {16 17 private static SzLogger log = SzLogger. getLogger (); 18 19 public static void main (String [] args) {20 21/* load all script files, by default, all problems under the project path-scripts file */22 ArrayList <String> loadScripts = ScriptManager. getInstance (). reload (); 23 String join = String. join (",", loadScripts); 24 log. error (join); 25/* initialize the listener */26 WebApiManager. getInstance (). init (); 27} 28}

 

Start program test

After the program is started, we can see that the temporary script files under the program project are deleted first, and then the latest script file is reloaded. The script is successfully loaded: net. sz. game. test. scripts. webapi. indexScript, net. sz. game. test. webapi. iseries. IWebApiScript

Enter http: // 127.0.0.1/index in the browser

Output successfully;

Add a login listener;

1 package net. sz. game. test. scripts. webapi; 2 3 import net. sz. game. engine. nio. nettys. http. nioHttpRequest; 4 import net. sz. game. engine. szlog. szLogger; 5 import net. sz. game. test. webapi. iseries. IWebApiScript; 6 7/** 8*9 * <br> 10 * author lost programmers <br> 11 * blog http://www.cnblogs.com/ty408/ <br> 12 * mail 492794628@qq.com <br> 13 * phone 13882122019 <br> 14 */15 public class LoginScript implements iwebapisments {16 17 private static SzLogger log = SzLogger. getLogger (); 18 19 @ Override20 public String getRoute () {21 return "login"; 22} 23 24 @ Override25 public boolean action (String url, NioHttpRequest request) {26/* Add the output content, html data, json data, txt data, and xml data. Define the data as needed */27 request. addContent ("I Am a plaintext webapi automatically register a route login welcome to log on"); 28 return true; 29} 30 31}
View Code

 

Look at the results:

In fact, we still need to restart the service for this automatic registration; or we are not smart enough;

Dynamically load routing scripts without restarting services

1 package net. sz. game. test. scripts. webapi; 2 3 import java. util. arrayList; 4 import net. sz. game. engine. nio. nettys. http. nioHttpRequest; 5 import net. sz. game. engine. scripts. manager. scriptManager; 6 import net. sz. game. engine. szlog. szLogger; 7 import net. sz. game. test. webapi. iseries. IWebApiScript; 8 9/** 10*11 * <br> 12 * author lost programmers <br> 13 * blog http://www.cnblogs.com/ty408/ <br> 14 * mail 492794628@qq.com <br> 15 * phone 13882122019 <br> 16 */17 public class ReloadScript implements IWebApiScript {18 19 private static SzLogger log = SzLogger. getLogger (); 20 21 @ Override22 public String getRoute () {23 return "reload"; 24} 25 26 @ Override27 public boolean action (String url, NioHttpRequest request) {28/* re-read routing script */29 ArrayList <String> loadScripts = ScriptManager. getInstance (). reload (); 30 String join = "Dynamic Loading Result:" + String. join (",", loadScripts); 31 log. error (join); 32 request. addContent (join); 33 return true; 34} 35 36}

 

The reload listener processes the update operation of the script route;

The reason for not using the file update status is that it is not controllable; maybe I am only publishing now, but do not need to update; wait for the specified time to update;

Re-access login

Description

The above code completes Automatic route registration,

Pay attention to the following points:

1. We use the script-based Automatic route registration function. Therefore, the code is in plain text, which may cause damage or Trojan Infection;

2. I have not added the verification mechanism. This can be self-agreed. It is indeed the cache mechanism of iis or tomcat sesstion and cokie. The advantage is that it is highly controllable, whether it is cache or data flow, custom format; not limited by iis or Tomcat;

3. However, we can achieve dynamic release and updates without restarting the service or affecting other services. The functions are developed and deployed independently, mutual interference;

This principle is defined at the beginning of my article. It enables dynamic addition and deletion of service interfaces, independent development, independent deployment, and a single process. Of course, you can coordinate and call service interfaces;

C #. net

Netty is still called in my version library or framework;

 1 using Net.Sz.Framework.Netty; 2 using Net.Sz.Framework.Netty.Http; 3 using System; 4 using System.Collections.Generic; 5 using System.Linq; 6 using System.Text; 7 using System.Threading.Tasks; 8  9 namespace CApp_Webapi10 {11     class Program12     {13         static void Main(string[] args)14         {15 16             HttpServer httpserver = NettyPool.AddHttpBind("127.0.0.1", 9527, 2);17 18             httpserver.AddHandler("*", new ActionHttpHandler((session) =>19             {20                 session.AddContent("

 

Therefore, simple testing is the same without too much implementation and explanation of theories;

C # version, whether you are a console project, from project or service project; can be perfectly run;

Summary

Previously, when the shared data of static variables was created in iis, when the web project was not recycled in the program pool, the static resources were no longer available. You need to add the static variables to the web. load and call the cache set;

Similarly, I developed a game backend. Both the tcp protocol of socket and the http protocol of socket need to be processed. However, the program must be put together, it cannot be called again through iis;

This article describes how to implement socket http requests, the concept of Automatic route registration, the concept of microservice, and the implementation of a script-based plaintext processing interface;

As for the implementation mechanism of scripts, you can view the Framework Code or read the previous articles, but many of the Code in the previous articles are not the same as the current service framework code;

C # The following test code is basically stored in

Under the "test" folder, you can directly view the test code of the project;

Java projects are different. They are all maven projects. Each project has a test package;

The program has been tested; therefore, there are no separate projects;

Old Iron did not give a suggestion. Could you comment on it?

Changyou, juyou, Duan you, tiantiao, and Baota river demon;

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.