Venus 是一個簡單的、高性能、高併發能力的java 開源Remoting框架
wiki地址:http://wiki.hexnova.com/display/Venus/HOMEjava
性能測試:http://wiki.hexnova.com/pages/viewpage.action?pageId=1507358程序員
產品發佈日誌: http://wiki.hexnova.com/pages/viewrecentblogposts.action?key=Venusweb
1. 如何服務化
採用接口與實現分離,服務接口是一種契約,他與咱們開發web Service相似。
java開發語言:採用對程序員友好的接口申明形式,開發人員不須要關心客戶端與服務端之間的傳輸協議。
其餘語言:能夠經過該框架提供自定義協議進行交互
2. 服務接口定製
定義服務接口
接口參數命名
定義參數校驗規則
Java語言服務接口儘可能不要依賴其餘項目. 接口層面只須要接口相關的參數對象類與服務類
異常定義
3. 接口參數校驗
4. 提供3種交互方式
請求應答模式:普通的request、response,通常用於接口有返回值
異步請求模式:一般用於接口無返回值,客戶端並不關心服務器的處理結果,也不用關心服務器處理多少時間
異步回調模式:接口無返回值,處理一般消化大量時間,須要服務端通知處理結果的業務接口
下面是一個例子:
一、簡單的接口例子:HelloService.javaspring
- 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);
- }
二、客戶端TestCase編寫api
- 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();
- }
-
- }
三、服務端的實現服務器
- 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);
- }
-
- }
- }