之前从未接触过android开发,但是公司要求把一些app中的服务做成微服务。大概思想如下图(app并不面向所有人,多用于银行pad):
     
   
      只能查资料找实现方法,在网上找到i-netty框架可以做android里面的web容器。下载工程和代码运行后发现,是把web工程运行在i-jetty中,和我实际情况不符合。
      转念一想,把i-jetty的jar引入android工程中,在java中实现http server就可以了。
     
     
         因为之前已经在java中实现过jetty的http server。顾在android中重新实现了一遍。代码如下:
      
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletOutputStream;

import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
 
 
/**
 *  @author yc
 */
public class HttpRunningServer {

    /** httpServer port 8080默认端口 */
    private int httpPort = 8383;
    // 主机地址
    private String host = "127.0.0.1";
    // http servier
    private Server httpServer;
    // http渠道连接器
    private SelectChannelConnector connector;
    //

    private int defaultWaitTime = 60000;

    public HttpRunningServer(String host,
                             int port) {
        this.host = host;
        this.httpPort = port;
    }

    public void start() {

        connector = new SelectChannelConnector();
        connector.setPort(this.httpPort);
        connector.setHost(this.host);
        connector.setMaxIdleTime(defaultWaitTime);
        connector.setThreadPool(new QueuedThreadPool(5));

        httpServer = new Server();
        httpServer.addConnector(connector);
        ContextHandler contextHandler = new ContextHandler();
        contextHandler.setContextPath("/");
        contextHandler.setHandler(new HttpHandler());
        ContextHandlerCollection contexts = new ContextHandlerCollection();

        contexts.setHandlers(new Handler[] { contextHandler });

        HandlerCollection handlers = new HandlerCollection();

        handlers.setHandlers(new Handler[] { contexts, new DefaultHandler() });

        httpServer.setHandler(handlers);

        try {
            httpServer.start();
        } catch (Exception e) {
            System.out.println(e);
//            e.printStackTrace();
        }

    }

    private String unpackHttpRequest(Request baseRequest, HttpServletRequest request) {
        byte[] b = new byte[1024];
        int i = 0;
        InputStream inputStream = null;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            inputStream = request.getInputStream();
            while ((i = inputStream.read(b, 0, 1024)) > 0) {
                out.write(b, 0, i);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return out.toString();
    }

    private void packHttpResponse(String result, HttpServletResponse response) {

        try {
            ServletOutputStream ouputStream = response.getOutputStream();
            ouputStream.write(result.getBytes());
        } catch (IOException e) {
            System.out.println(e);
        }

    }

    class HttpHandler extends AbstractHandler {// 默认处理入口

        public void handle(String targetUrl, Request baseRequest,
                           HttpServletRequest request, HttpServletResponse response)
                throws IOException, ServletException {
            String result = unpackHttpRequest(baseRequest,request);
            packHttpResponse(result,response);
            response.flushBuffer();
        }
    }
}

这样就启动了一个http server ,基本实现了上图中的微服务app,只需要在HttpHandler中去实现你自己的代码逻辑就可以了。

对于client 你可以在其他app上使用http服务去访问微服务的app。

    以上内容只是简单的记录!



 
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐