前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >motan入门

motan入门

作者头像
叔牙
发布2020-11-19 14:35:51
3700
发布2020-11-19 14:35:51
举报
文章被收录于专栏:一个执拗的后端搬砖工
代码语言:javascript
复制
常用的rpc框架有阿里开源的dubbo,facebook
的thrift,以及新浪微博刚开源的motan,
接下来将以实例的方式简单介绍motan的使用方式
一、新建maven项目并引进依赖

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>

代码语言:javascript
复制
项目结构如下:
二、新建接口及实现
代码语言:javascript
复制
定义接口

123

public interface UserService {User queryById(Long id);}

代码语言:javascript
复制
新建默认实现

123456789

@Servicepublic class UserServiceImpl implements UserService {public User queryById(Long id){User u = new User();u.setId(id);u.setName("typhoon");return u;}}

三、服务提供者配置及服务启动
代码语言:javascript
复制
在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>

代码语言:javascript
复制
配置服务启动门面类(在根目录新建)

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

代码语言:javascript
复制
启动服务
四、配置服务消费端并测试rpc请求
代码语言:javascript
复制
在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>

代码语言:javascript
复制
新建单元测试基类

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

代码语言:javascript
复制
新建接口单元测试类并实现测试逻辑

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

代码语言:javascript
复制
 启动单元测试发现可以像本地接口一样访问
代码语言:javascript
复制
这样我们就实现了motan的简单使用,
目前是服务启动后消费方直连的方式,
存在单点故障问题,后续会实现
motan+zk来实现服务注册和订阅,
以及实现负载均衡等

ps:以上实例在本地跑起来没有问题,

如果能够给各位看官带来帮助,

是我莫大的荣幸,感谢支持和拍砖

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2017-07-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 PersistentCoder 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、新建maven项目并引进依赖
  • 二、新建接口及实现
  • 三、服务提供者配置及服务启动
  • 四、配置服务消费端并测试rpc请求
相关产品与服务
负载均衡
负载均衡(Cloud Load Balancer,CLB)提供安全快捷的流量分发服务,访问流量经由 CLB 可以自动分配到云中的多台后端服务器上,扩展系统的服务能力并消除单点故障。负载均衡支持亿级连接和千万级并发,可轻松应对大流量访问,满足业务需求。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档