`
phipray
  • 浏览: 66028 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

初探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>

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics