常用的rpc框架有阿里开源的dubbo,facebook
的thrift,以及新浪微博刚开源的motan,
接下来将以实例的方式简单介绍motan的使用方式
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 | <properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><motan.version>0.2.1</motan.version><org.springframework.version>3.2.9.RELEASE</org.springframework.version></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><!-- motan begin --><dependency><groupId>com.weibo</groupId><artifactId>motan-core</artifactId><version>${motan.version}</version></dependency><dependency><groupId>com.weibo</groupId><artifactId>motan-transport-netty</artifactId><version>${motan.version}</version></dependency><dependency><groupId>com.weibo</groupId><artifactId>motan-springsupport</artifactId><version>${motan.version}</version></dependency><!-- motan end --><!-- START spring --><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>${org.springframework.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${org.springframework.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>${org.springframework.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${org.springframework.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${org.springframework.version}</version></dependency><!-- End spring --></dependencies> |
---|
项目结构如下:
定义接口
123 | public interface UserService {User queryById(Long id);} |
---|
新建默认实现
123456789 | @Servicepublic class UserServiceImpl implements UserService {public User queryById(Long id){User u = new User();u.setId(id);u.setName("typhoon");return u;}} |
---|
在src/main/resources目录下新建
motan-provider.xml并加入如下配置:
123456789101112 | <?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:motan="http://api.weibo.com/schema/motan"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://api.weibo.com/schema/motan http://api.weibo.com/schema/motan.xsd"><!-- 扫描并注入bean --><context:component-scan base-package="com.typhoon.motan"/><!-- userService依赖 --><motan:service interface="com.typhoon.motan.service.UserService" ref="userServiceImpl" export="8002"/></beans> |
---|
配置服务启动门面类(在根目录新建)
1234567891011 | public class ProviderServer {public static void main(String[] args) throws Exception {AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring-root.xml");context.start();System.out.println("server start ......");//MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true);CountDownLatch count = new CountDownLatch(1);count.await();context.close();}} |
---|
启动服务
在src/test/resources目录下新建motan-consumer.xml并做如下配置:
1234567891011 | <?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:motan="http://api.weibo.com/schema/motan"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://api.weibo.com/schema/motan http://api.weibo.com/schema/motan.xsd"><!-- userService依赖 --><motan:referer id="userService" interface="com.typhoon.motan.service.UserService" directUrl="127.0.0.1:8002"/></beans> |
---|
新建单元测试基类
12345678910111213141516 | public class BaseTest {protected AbstractApplicationContext context = null;public BaseTest(){if (StringUtils.isNotBlank(System.getProperty("time.stamp"))) {}init();context.start();}/*** 初始化信息* @return*/public void init() {context = new ClassPathXmlApplicationContext("motan-consumer.xml");}} |
---|
新建接口单元测试类并实现测试逻辑
12345678910111213141516 | public class UserServiceTest extends BaseTest {UserService userService;public UserServiceTest() {this.userService = this.context.getBean("userService",UserService.class);}@Overridepublic void init() {context = new ClassPathXmlApplicationContext("motan-consumer.xml");}@Testpublic void testQueryById() {Long id = 1L;User u = this.userService.queryById(id);System.out.println(JSON.toJSONString(u));}} |
---|
启动单元测试发现可以像本地接口一样访问
这样我们就实现了motan的简单使用,
目前是服务启动后消费方直连的方式,
存在单点故障问题,后续会实现
motan+zk来实现服务注册和订阅,
以及实现负载均衡等
ps:以上实例在本地跑起来没有问题,
如果能够给各位看官带来帮助,
是我莫大的荣幸,感谢支持和拍砖
本文分享自 PersistentCoder 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!