初探Motan

一 部署Motan运行环境

1 升级spring到4.2.4 Release

2 引入相关Motan的jar包

 

<!-- Motan start -->
		<dependency>
		    <groupId>com.weibo</groupId>
		    <artifactId>motan-core</artifactId>
		    <version>${motan.version}</version>
		</dependency>
		<dependency>
			<groupId>com.weibo</groupId>
			<artifactId>motan-registry-zookeeper</artifactId>
			<version>${motan.version}</version>
		</dependency>
		<dependency>
		<groupId>com.weibo</groupId>
			<artifactId>motan-registry-consul</artifactId>
			<version>${motan.version}</version>
		</dependency>
		<dependency>
			<groupId>com.weibo</groupId>
			<artifactId>serialization-extension</artifactId>
			<version>${motan.version}</version>
		</dependency>
		<dependency>
			<groupId>com.weibo</groupId>
			<artifactId>motan-springsupport</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-protocol-yar</artifactId>
			<version>${motan.version}</version>
		</dependency>
                 -->		
                <dependency>
		  <groupId>org.jboss.netty</groupId>
		  <artifactId>netty</artifactId>
		  <version>3.2.5.Final</version>
		</dependency>
		
		<!-- zookeeper start-->
		<dependency>
		  <groupId>com.101tec</groupId>
		  <artifactId>zkclient</artifactId>
		  <version>${zkclient.version}</version>
		</dependency>	
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>${log4j.version}</version>
		</dependency>		
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.4.8</version>
		</dependency>
		<!-- zookeeper end-->
		
		<dependency>
		  <groupId>com.caucho</groupId>
		  <artifactId>hessian</artifactId>
		  <version>4.0.38</version>
		</dependency>
		<dependency>
		  <groupId>com.codahale.metrics</groupId>
		  <artifactId>metrics-core</artifactId>
		  <version>3.0.2</version>
		</dependency>
		
		<!-- consul start-->
		<dependency>
		    <groupId>com.ecwid.consul</groupId>
		    <artifactId>consul-api</artifactId>
		    <version>1.1.4</version>
		</dependency>	
		<!-- consul end-->		
<!-- Motan end -->

 

 

 

二  单机版HelloWorld

 

创建motan_server.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">

<!-- service implemention bean -->
<bean id="helloServiceImpl" class="com.csair.csmbp.service.impl.DemoServiceImpl" />


<!-- 单机exporting service by Motan -->

<motan:service interface="com.csair.csmbp.service.DemoService" ref="helloServiceImpl" export="8002" />


</beans>

 Server启动类:

package com.csair.csmbp.microServer;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.weibo.api.motan.common.MotanConstants;
import com.weibo.api.motan.util.MotanSwitcherUtil;

public class Server {

	public static void main(String[] args) throws InterruptedException {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:motan_server.xml");
        System.out.println("server start...");
        MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true);
        System.out.println("service registry...");
    }
	
}

 

创建motan_client:

写道
<?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">

<!-- 单机reference to the remote service -->

<motan:referer id="remoteService" interface="com.csair.csmbp.service.DemoService" directUrl="localhost:8002"/>

</beans>

 

创建client调用类

 

package com.csair.csmbp.service.impl;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.csair.csmbp.service.DemoService;


/**
 * Class description goes here.
 *
 * @author deanPhipray
 * @since 2015-3-2
 */
@Transactional
@Service(value="rpcDemoService")
public class RpcDemoServiceImpl {

	

	public static void main(String[] args) throws InterruptedException {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:motan_client.xml");
        DemoService service = (DemoService) ctx.getBean("remoteService");
        System.out.println(service.rpcHelloWorld("motan"));
    }

	
}

 

服务端和客户端都需要用户接口类

package com.csair.csmbp.service;

import com.csair.csmbp.dao.DemoDao;
import com.csair.csmbp.model.Demo;
import com.csair.csmbp.service.base.BaseServcie;

/**
 * 用户service
 * 
 * @author DeanPhipray
 * 
 */
public interface DemoService extends BaseServcie<Demo,DemoDao> {

	/**
	 * 
	 * @param userId
	 * @return
	 */
		
	public String rpcHelloWorld(String name);


}

 

三 集群版HelloWorld

 

motan_server.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">

    <!-- service implemention bean -->
    <bean id="helloServiceImpl" class="com.csair.csmbp.service.impl.DemoServiceImpl" />
    
   
    <!-- 集群exporting service by Motan -->
    <motan:registry regProtocol="zookeeper" name="my_zookeeper" address="10.108.68.140:2181,10.108.68.140:2182,10.108.68.140:2183"/>
    
    <motan:service interface="com.csair.csmbp.service.DemoService" ref="helloServiceImpl" registry="my_zookeeper" export="8002" />
    
</beans>

 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">
	
	<!-- 集群reference to the remote service -->
	<motan:registry regProtocol="zookeeper" name="my_zookeeper" address="10.108.68.140:2181,10.108.68.140:2182,10.108.68.140:2183"/>

	<motan:referer id="remoteService" interface="com.csair.csmbp.service.DemoService" registry="my_zookeeper" requestTimeout="5000"/>

</beans>

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Exception caught when during method invocation. request:net.risesoft.rpc.itemAdmin.DocumentManager.edit4Position(java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String) requestId=1771270236171928205 java.lang.reflect.InvocationTargetException: null at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.weibo.api.motan.rpc.DefaultProvider.invoke(DefaultProvider.java:64) at com.weibo.api.motan.rpc.AbstractProvider.call(AbstractProvider.java:52) at com.weibo.api.motan.transport.ProviderMessageRouter.call(ProviderMessageRouter.java:98) at com.weibo.api.motan.transport.ProviderProtectedMessageRouter.call(ProviderProtectedMessageRouter.java:75) at com.weibo.api.motan.transport.ProviderMessageRouter.handle(ProviderMessageRouter.java:93) at com.weibo.api.motan.transport.support.DefaultRpcHeartbeatFactory$HeartMessageHandleWrapper.handle(DefaultRpcHeartbeatFactory.java:98) at com.weibo.api.motan.transport.netty4.NettyChannelHandler.processRequest(NettyChannelHandler.java:155) at com.weibo.api.motan.transport.netty4.NettyChannelHandler.processMessage(NettyChannelHandler.java:133) at com.weibo.api.motan.transport.netty4.NettyChannelHandler.access$000(NettyChannelHandler.java:32) at com.weibo.api.motan.transport.netty4.NettyChannelHandler$1.run(NettyChannelHandler.java:73) at java.util.concurrent.ThreadPoolExecutor.runWorker(Threa是哪里的问题
07-14

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值