Giter Site home page Giter Site logo

lamp's Introduction

#lamp

基于netty&spring容器封装的json通讯协议组件

##modules Alt text ##protocol

自定义协议

Request

	protected int messageID;//消息序列号id,用于标识消息

    private String uri;// 消息uri,用于路由消息到指定controller method

    private Map<String, String> paramMap;// 请求参数map,支持基本类型参数

Response

 private int messageID;// 对应请求消息的id

 private String data;// 消息返回数据,通常为json串

##example

###服务端的打开方式

####基于spring注解的打开方式

@Configuration
@ComponentScan
public class LampServer {

    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(LampServer.class);
        BaseServer baseServer = new BaseServer(DefConfigFactory.createDEVConfig(), context);
        baseServer.start();
    }
}

####基于spring xml的打开方式

 ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"application.xml"});
        BaseServer baseServer = new BaseServer(DefConfigFactory.createDEVConfig(), context);
        baseServer.start();

application.xml配置如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>
    <!-- 主动搜索以下目录 -->
    <context:component-scan base-package="com.dempe.lamp"/>

</beans>

###业务逻辑的编写方式

@Controller("sample")
public class SampleController {

    //依赖spring的注入
    @Resource
    SampleService lampService;

    /**
     * 默认匹配path getUri=/sample/hello
     * 默认注入request name属性的参数值
     * @return
     */
    @Path
    public JSONObject hello(@Param String name,@Param int age) {
//        System.out.println(age);
        return lampService.hello(name);
    }
}

###客户端的使用方式

/**
 * 简单client示例
 * User: Dempe
 * Date: 2016/1/28
 * Time: 15:43
 * To change this template use File | Settings | File Templates.
 */
public class SampleClient {

    public static void main(String[] args) throws Exception {
        futureClientExample();
    }

    /**
     * FutureClient example
     * @throws Exception
     */
    public static void futureClientExample() throws Exception {
        FutureClient client = new FutureClient("localhost", 8888);
        // 构造json请求协议
        Request request = buildRequest();
        Future<Response> future = client.send(request);
        System.out.println(future.await());
    }

    /**
     * BlockingClient example
     * @throws Exception
     */
    public static void blockingClientExample() throws Exception {
        BlockingClient client = new BlockingClient("localhost", 8888);
        Request request = buildRequest();
        Response response = client.send(request);
        System.out.println(response);
    }

    /**
     * CallbackClient example
     * @throws Exception
     */
    public static void callbackClientExample() throws Exception {
        CallbackClient client = new CallbackClient("localhost", 8888);
        Request request = buildRequest();
        client.send(request, new Callback() {
            @Override
            public void onReceive(Object message) {
                System.out.println(message);
            }
        });
    }

    public static Request buildRequest() {
        Map<String, String> data = new HashMap<String, String>();
        data.put("name", "dempe");
        data.put("age", "1");
        Request request = new Request();
        request.setUri("/sample/hello");
        request.setParamMap(data);
        return request;
    }


    /**
     * 压测方法
     * @throws Exception
     */
    public static void stressTesting() throws Exception {
        MetricThread thread = new MetricThread("client");
        List<FutureClient> clientList = new ArrayList<FutureClient>();
        int size = 8;
        for (int i = 0; i < size; i++) {
            clientList.add(new FutureClient("localhost", 8888));
        }
        int i = 0;
        while (true) {
            i++;
            thread.increment();
            FutureClient client = clientList.get(i % size);
            // 初始化client
            Request request = buildRequest();
            //发送请求并返回响应
            Future<Response> future = client.send(request);
            if (i % 100000 == 0) {
                TimeUnit.SECONDS.sleep(1);
            }
        }
    }


}

###性能测试

开发机 4核 8G win7 7w+

lamp's People

Contributors

dempezheng avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.