1. Deploy the Motan runtime environment
1 Upgrade spring to 4.2.4 Release
2 Import related Motan jar packages
<!-- 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 -->
Two stand-alone version of HelloWorld
Create 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>
<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 startup class:
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..."); } }
Create motan_client:
wrote
<?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>
<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>
Create client calling class
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")); } }
Both server and client need user interface classes
package com.csair.csmbp.service; import com.csair.csmbp.dao.DemoDao; import com.csair.csmbp.model.Demo; import com.csair.csmbp.service.base.BaseServcie; /** * User service * * @author DeanPhipray * */ public interface DemoService extends BaseServcie<Demo,DemoDao> { /** * * @param userId * @return */ public String rpcHelloWorld(String name); }
Three-cluster version 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" /> <!-- Cluster 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>