Venus 是一个简单的、高性能、高并发能力的java 开源Remoting框架
wiki地址:http://wiki.hexnova.com/display/Venus/HOME
性能测试:http://wiki.hexnova.com/pages/viewpage.action?pageId=1507358
产品发布日志: http://wiki.hexnova.com/pages/viewrecentblogposts.action?key=Venus
1. 如何服务化
采用接口与实现分离,服务接口是一种契约,他与我们开发web
Service类似。
java开发语言:采用对程序员友好的接口申明形式,开发人员不需要关心客户端与服务端之间的传输协议。
其他语言:可以通过该框架提供自定义协议进行交互
2.
服务接口定制
定义服务接口
接口参数命名
定义参数校验规则
Java语言服务接口尽量不要依赖其他项目.
接口层面只需要接口相关的参数对象类与服务类
异常定义
3.
接口参数校验
4.
提供3种交互方式
请求应答模式:普通的request、response,一般用于接口有返回值
异步请求模式:通常用于接口无返回值,客户端并不关心服务器的处理结果,也不用关心服务器处理多少时间
异步回调模式:接口无返回值,处理通常消化大量时间,需要服务端通知处理结果的业务接口
下面是一个例子:
1、简单的接口例子:HelloService.java
- package com.meidusa.venus.hello.api;
-
- import com.meidusa.venus.annotations.Endpoint;
- import com.meidusa.venus.annotations.Param;
- import com.meidusa.venus.annotations.Service;
- import com.meidusa.venus.notify.InvocationListener;
-
-
-
-
-
-
-
-
-
-
-
- @Service(name="HelloService",version=1)
- public interface HelloService {
-
-
-
-
-
-
- @Endpoint(name="sayHelloWithCallbak")
- public abstract void sayHello(@Param(name="name") String name,
- @Param(name="callback") InvocationListener<Hello> invocationListener);
-
-
-
-
-
- @Endpoint(name="sayHello",async=false)
- public abstract void sayHello(@Param(name="name") String name) throws HelloNotFoundException;
-
-
-
-
-
- @Endpoint(name="sayAsyncHello",async=true)
- public abstract void sayAsyncHello(@Param(name="name") String name);
-
-
-
-
-
-
-
- @Endpoint(name="getHello")
- public abstract Hello getHello(@Param(name="name") String name);
- }
2、客户端TestCase编写
- package com.meidusa.venus.hello.client;
-
- import java.util.concurrent.CountDownLatch;
-
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.test.context.ContextConfiguration;
- import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
-
- import com.meidusa.venus.exception.CodedException;
- import com.meidusa.venus.hello.api.Hello;
- import com.meidusa.venus.hello.api.HelloNotFoundException;
- import com.meidusa.venus.hello.api.HelloService;
- import com.meidusa.venus.notify.InvocationListener;
-
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations="classpath:/applicationContext-helloworld-client.xml")
- public class TestHelloService {
-
- @Autowired
- private HelloService helloService;
-
- @Test
- public void saySync(){
- System.out.println(helloService.getHello("jack"));
- }
-
- @Test
- public void testSyncWithException(){
- try {
- helloService.sayHello("jack");
- } catch (HelloNotFoundException e) {
- System.out.println("throw an user defined HelloNotFoundException");
- }
- }
-
- @Test
- public void testAsync(){
- helloService.sayAsyncHello("jack");
- }
-
- @Test
- public void testCallback() throws Exception{
-
- final CountDownLatch latch = new CountDownLatch(1);
-
-
- InvocationListener<Hello> listener = new InvocationListener<Hello>() {
- public void callback(Hello myobject) {
- System.out.println(" async call back result="+myobject);
- latch.countDown();
- }
-
- @Override
- public void onException(Exception e) {
- if(e instanceof CodedException){
- CodedException exception = (CodedException) e;
- System.out.println(" async call back error:"+exception.getErrorCode()+",message="+exception.getMessage());
- }else{
- System.out.println(" async call back message="+e.getMessage());
- }
- latch.countDown();
-
- }
- };
-
- helloService.sayHello("jack",listener);
- latch.await();
- }
-
- }
3、服务端的实现
- package com.meidusa.venus.hello.impl;
-
- import java.math.BigDecimal;
- import java.util.HashMap;
- import java.util.Map;
-
- import com.meidusa.venus.hello.api.Hello;
- import com.meidusa.venus.hello.api.HelloNotFoundException;
- import com.meidusa.venus.hello.api.HelloService;
- import com.meidusa.venus.notify.InvocationListener;
-
- public class DefaultHelloService implements HelloService {
- private String greeting;
- public String getGreeting() {
- return greeting;
- }
-
- public void setGreeting(String greeting) {
- this.greeting = greeting;
- }
- public Hello getHello(String name) {
- Hello hello = new Hello();
- hello.setName(name);
- hello.setGreeting(greeting);
- Map<String,Object> map = new HashMap<String,Object>();
- hello.setMap(map);
- map.put("1", 1);
- map.put("2", new Long(2));
- map.put("3", new Integer(3));
- hello.setBigDecimal(new BigDecimal("1.341241233412"));
- return hello;
- }
-
- public void sayHello(String name) throws HelloNotFoundException {
- throw new HelloNotFoundException(name +" not found");
- }
-
- @Override
- public void sayAsyncHello(String name) {
- System.out.println("method sayAsyncHello invoked");
- }
-
- public void sayHello(String name,
- InvocationListener<Hello> invocationListener) {
- Hello hello = new Hello();
- hello.setName(name);
- hello.setGreeting(greeting);
- Map<String,Object> map = new HashMap<String,Object>();
- hello.setMap(map);
- map.put("1", 1);
- map.put("2", new Long(2));
- map.put("3", new Integer(3));
-
- if(invocationListener != null){
- invocationListener.callback(hello);
- }
-
- }
- }
Venus 是一个简单的、高性能、高并发能力的java 开源Remoting框架,布布扣,bubuko.com
Venus 是一个简单的、高性能、高并发能力的java 开源Remoting框架
原文:http://www.cnblogs.com/scwanglijun/p/3781394.html