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 framework的 HelloService 接口例子.</p>
- * 支持3種調用方式:</p>
- * <li> 請求應答模式:普通的request、response,一般用於接口有返回值</li>
- * <li> 異步請求模式:通常用於接口無返回值,客戶端並不關心服務器的處理結果,也不用關心服務器處理多少時間</li>
- * <li> 異步回調模式:接口無返回值,處理通常消化大量時間,需要服務端通知處理結果的業務接口</li>
- *
- * @author Struct
- *
- */
- @Service(name="HelloService",version=1)
- public interface HelloService {
- /**
- * 無返回結果的服務調用,支持回調方式,該服務在通訊層面上為異步調用
- * @param name
- * @param invocationListener 客戶端的回調接口
- */
- @Endpoint(name="sayHelloWithCallbak")
- public abstract void sayHello(@Param(name="name") String name,
- @Param(name="callback") InvocationListener<Hello> invocationListener);
- /**
- * 無返回結果的服務調用,支持同步或者異步調用,
- * 該接口申明:同步,並且接口申明異常
- * @param name
- */
- @Endpoint(name="sayHello",async=false)
- public abstract void sayHello(@Param(name="name") String name) throws HelloNotFoundException;
- /**
- * 無返回結果的服務調用,支持同步或者異步調用,無異常申明
- * @param name
- */
- @Endpoint(name="sayAsyncHello",async=true)
- public abstract void sayAsyncHello(@Param(name="name") String name);
- /**
- * 有返回結果的服務調用,該接口只能支持同步調用
- * @param name
- * @return
- */
- @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{
- //為了讓回調完成,采用countDownLatch計數器方式,避免testcase主線程運行完成而回調未結束的問題
- final CountDownLatch latch = new CountDownLatch(1);
- //在正常的使用的代碼中這個類需要單實例,避免過多的callback listener導致內存問題
- 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);
- }
- }
- }