venus之hello world

时间:2023-03-09 00:12:40
venus之hello world

Venus Service Framework提供服务器端开发SDK以及客户端SDK。它们之间采用Venus自定义的私有协议。encoder、decoder采用多种序列化方式,客户端根据自己的语言选择一种序列化方式。

目前提供3种序列化方式:0:表示JSON, 1:表示BSON , 3:表示JAVA对象序列化(仅限于java语言)。

Venus为让开发人员只专注服务开发而努力,开发人员在设计接口的时候只需要按照java的接口设计原则进行设计,把接口、接口中使用的参数对象类、接口异常类封装成API包即可对外提供。

而Venus则承担协调沟通客户端服务器端的通讯框架。


接口定义
接口:用来约束客户端、服务端的应用层协议,他申明了具体服务接口的调用方式,参数的数据类型,具体功能的声明、异常的描述等等信息,它有服务端开发工程师,服务使用方工程师共同制定的。
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);
}
客户端开发
1、引入venus相关依赖
2、编写Spring的配置文件
3、编写venus客户端配置文件:VenusClient-simple.xml
4、编写Testcase进行junit测试

1、依赖管理

pom.xml

<dependencies>
<dependency>
<groupId>com.meidusa.service</groupId>
<artifactId>venus-client</artifactId>
<version>${venus.version}</version>
</dependency>
</dependencies>
<properties>
<venus.version>3.0.0</venus.version>
</properties>

2、客户端spring配置文件

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean id="serviceFactory" class="com.meidusa.venus.client.VenusServiceFactory">
<property name="configFiles">
<list>
<value>classpath:VenusClient-simple.xml</value>
<!--
支持file: 绝对路径与classpath:,这儿可以配置多个文件
<value>file:VenusClient-simple.xml</value>
-->
</list>
</property>
</bean>

3、客户端的 venus.xml的配置

<?xml version="1.0" encoding="utf8"?>
<venus-client> <!-- 服务接口列表 -->
<services>
<!-- 定义服务以及指定该服务提供方的远程相关配置 -->
<service type="com.meidusa.venus.hello.api.HelloService">
<property name="ipAddressList">127.0.0.1:16800</property>
</service>
</services>
</venus-client>

4、testCase using Spring autowire

import org.springframework.beans.factory.annotation.Autowired;

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");
} }
服务端开发
1、引入对venus的依赖
2、实现HelloService接口
3、Venus的服务端配置:VenusServices-simple.xml
4、Spring的相关Bean的配置(注意:需要让spring容器默认采用 byName autowire 的功能: default-autowire="byName")

1、引入Venus相关的依赖

pom.xml

<dependencies>
<dependency>
<groupId>com.meidusa.service</groupId>
<artifactId>venus-backend</artifactId>
<version>${venus.version}</version>
</dependency> </dependencies>
<properties>
<venus.version>3.0.0</venus.version>
</properties>

2、实现HelloWorld接口

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);
} }
}

3、VenusService-simple.xml的配置

<?xml version="1.0" encoding="utf-8"?>
<venus-server>
<services>
<!-- 有2种方式暴露Service,1: instance由venus负责实例化,如下代码: -->
<service type="com.meidusa.venus.hello.api.HelloService">
<instance class="com.meidusa.venus.hello.impl.DefaultHelloService">
<property name="greeting">hello venus hello service</property>
</instance>
</service> <!-- 第二种:instance由Spring负责提供,则需要采用${...} 这种方式引用Spring的bean,这种方式主要是Spring内其他bean依赖该bean,则通常采用这种 -->
<!-- 这儿的 ${helloService} 表示在你的spring xml中需要配置一个 beanName为 helloService,type为DefaultHelloService的一个bean
<service type="com.meidusa.venus.hello.api.HelloService">
<property name="instance">${helloService}</property>
</service> -->
</services>
</venus-server>

4、Spring相关的venus 配置

<!-- 引入venus Service 相关的配置 -->
<import resource="classpath:/spring/applicationContext-service-container.xml"/> <!-- 加载服务配置文件 -->
<bean id="serviceManager" class="com.meidusa.venus.backend.services.xml.XmlFileServiceManager">
<property name="configFiles">
<list>
<value>classpath:VenusServices-simple.xml</value>
</list>
</property>
</bean> <!-- 设置authenticateProvider,默认为可以使用该配置 -->
<!-- venus目前提供2种方式的认证,一种是默认的采用虚拟的方式,另外一种支持用户名/密码登录,你也可以自己实现provider -->
<bean id="authenticateProvider" class="com.meidusa.venus.backend.authenticate.SimpleAuthenticateProvider">
<property name="useDummy" value="true"/>
<property name="username" value="venus"/>
<property name="password" value="venus"/>
</bean>
服务端的启动控制台展示
1、显示了多少个Service暴露出来
2、显示了每个Service有哪些Endpoint暴露出来
3、服务端的监听端口
-- ::, INFO  xml.XmlFileServiceManager - Add Endpoint: HelloService.sayHello
-- ::, INFO xml.XmlFileServiceManager - Add Endpoint: HelloService.sayHelloWithCallbak
-- ::, INFO xml.XmlFileServiceManager - Add Endpoint: HelloService.sayAsyncHello
-- ::, INFO xml.XmlFileServiceManager - Add Endpoint: HelloService.getHello
-- ::, INFO xml.XmlFileServiceManager - Add Endpoint: ParameterizedService.arrayLong
-- ::, INFO xml.XmlFileServiceManager - Add Endpoint: ParameterizedService.arraylong
-- ::, INFO net.ServerableConnectionManager - Server listening on 0.0.0.0/0.0.0.0:.

PropertyPlaceholder:用于替换venus的配置文件中包含 ${}变量的东西

<bean class="com.meidusa.venus.spring.VenusPropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>file:${project.home:.}/application.properties</value> </list>
</property>
</bean>

helloworld源代码地址:

svn 地址:svn://svn.hexnova.com/venus/venus-helloworld/trunk

转载自:http://wiki.hexnova.com/pages/viewpage.action?pageId=2883620