RPC调用之微博 motan

这边文章将偏实战, 至于motan,可以去了解如何实现、比普通的http请求的优越之处等


这里贴出应用motan的代码


[b]Client端[/b]


ClassPathXmlApplicationContext ctx= new ClassPathXmlApplicationContext("classpath:motan_client.xml");


NaturalRankingService naturalRankingService = ctx.getBean(NaturalRankingService.class);
NaturalResponse naturalResponse = naturalRankingService.recommend(recommendRequest);



client端配置motan_client.xml

<?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/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://api.weibo.com/schema/motan http://api.weibo.com/schema/motan.xsd">

<!-- 配置注册中心,客户端与服务端使用相同的配置中心

-->
<motan:registry regProtocol="zookeeper" name="client_zookeeper" address="172.0.0.1:2181,172.0.0.2:2181,172.0.0.3:2181"/>



<!--codec,serialization一定要跟服务端配置的保持一致,否则无法正常调用 -->
<motan:protocol id="client_protocol" default="false" name="motan" codec="motan2" serialization="fastjson"
requestTimeout="1000" maxContentLength="2048576"
maxClientConnection="300" minClientConnection="100" loadbalance="roundrobin"/>

<!-- mock测试: filter="statistic,mock" check="true" directUrl="127.0.0.1:8002" mock="true" -->
<motan:basicReferer registry="client_zookeeper"
group="ads-ranking-natural" accessLog="false" shareChannel="false"
id="clientBasicConfig"
filter="statistic"
/>

<!-- reference to the remote service directUrl="localhost:8002" -->
<motan:referer id="NaturalRankingService" interface="com.zz.common.interfaces.NaturalRankingService"
basicReferer="clientBasicConfig"
/>
</beans>



[b]jar包中的接口service[/b]


public interface NaturalRankingService {
public NaturalResponse recommend(RecommendRequest request) ;
}



jar包依赖


<dependencies>
<!-- dependencies blow were only needed for motan-based features -->
<dependency>
<groupId>com.weibo</groupId>
<artifactId>motan-core</artifactId>
<version>${motanVersion}</version>
</dependency>
<dependency>
<groupId>com.weibo</groupId>
<artifactId>motan-transport-netty</artifactId>
<version>${motanVersion}</version>
</dependency>
<dependency>
<groupId>com.weibo</groupId>
<artifactId>motan-springsupport</artifactId>
<version>${motanVersion}</version>
</dependency>
<dependency>
<groupId>com.weibo</groupId>
<artifactId>motan-registry-zookeeper</artifactId>
<version>RELEASE</version>
</dependency>

<!-- dependencies blow were only needed for spring-based features -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4jVersion}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4jVersion}</version>
</dependency>
</dependencies>




[b]server端实现[/b] server端也加入上面的jar


public class NaturalRankinServicegImpl implements NaturalRankingService {
@Override
public NaturalResponse recommend(RecommendRequest request) {
//实现代码
return null;
}
}



[b]server端的配置motan_server.xml[/b]


<?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/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://api.weibo.com/schema/motan http://api.weibo.com/schema/motan.xsd">

<!-- 配置注册中心,客户端与服务端使用相同的配置中心

-->


<motan:registry regProtocol="zookeeper" name="server_zookeeper" address="172.0.0.1:2181,172.0.0.2:2181,172.0.0.3:2181"/>


<!-- 协议配置。为防止多个业务配置冲突,推荐使用id表示具体协议。serialization="kryo" codec="compressMotan"-->
<motan:protocol id="server_protocol" default="false" name="motan" codec="motan2" serialization="fastjson"
requestTimeout="1000" maxServerConnection="8000" maxContentLength="2048576"
maxWorkerThread="300" minWorkerThread="100" />

<!-- service implemention bean -->
<bean id="NaturalRankinServicegImpl" class="io.batcloud.service.impl.NaturalRankinServicegImpl" />


<!-- exporting service by motan -->
<motan:basicService requestTimeout="1000" export="server_protocol:8102" registry="server_zookeeper"
group="ads-ranking-natural" accessLog="false" shareChannel="false"
id="serviceBasicConfig" filter="statistic"/>
<motan:service interface="com.zz.common.interfaces.NaturalRankingService" ref= "NaturalRankinServicegImpl" basicService="serviceBasicConfig" />
</beans>



这样便完成了motan RPC的调用应用, 当然中间还有 服务注册中心zookeeper的应用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值