浅谈motan

这几天做项目用到motan ,就学了下,分享一下。以下截图是demo架构

motan工程中的pom.xml的配置

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>motan</groupId>
  <artifactId>motan</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  
  
  <!-- 指定motan的版本 -->
  <properties>
  <motan.version>0.3.0</motan.version>
  </properties>
  
  <!-- 导入motan需要的jar包 -->
  <dependencies>
  
  <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>
  </dependencies>
  
  
  <modules>
  	<module>motan-service</module>
  	<module>motan-client</module>
  </modules>
  
  <build>
  <pluginManagement>
  <plugins>
  
  <plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.1</version>
  <configuration>
  <source>1.7</source>
  <target>1.7</target>
  <encoding>utf-8</encoding>
  </configuration>
  </plugin>
  
  </plugins>
  </pluginManagement>
  </build>
</project>

以下是客户端代码

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>motan</groupId>
    <artifactId>motan</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <groupId>motan-client</groupId>
  <artifactId>motan-client</artifactId>
  <name>motan-client</name>
  <description>motan-client</description>
  <properties>
  <spring.version>4.3.0.RELEASE</spring.version>
  </properties>
  
  <dependencies>
  <dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring.version}</version>
		</dependency>
  </dependencies>
</project>


package com.gyq.user.motan;
/**
 * 
 * @Tittle: 
 * @Description:
 * @date:2017年8月15日下午3:58:36
 * @author:高永琴
 */
public interface HelloService {
	String hello(String world);

}


客户端配置文件

<?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:referer  id="helloService" interface="com.gyq.user.motan.HelloService" directUrl="127.0.0.1:8002"/>

</beans>


客户端主函数

package com.gyq.user.motan;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 
 * @Tittle: 
 * @Description:
 * @date:2017年8月15日下午4:01:37
 * @author:高永琴
 */
public class Client {
	public static void main(String args[])
	{
		ApplicationContext applicationContext= new ClassPathXmlApplicationContext("classpath:motan-client.xml");
		HelloService helloService=(HelloService)applicationContext.getBean("helloService");
		System.out.println(helloService.hello("mmmmm"));
	}

}

服务器端代码,pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>motan</groupId>
    <artifactId>motan</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <groupId>motan-service</groupId>
  <artifactId>motan-service</artifactId>
  <name>motan-service</name>
  <description>motan-service</description>
  
  <properties>
  <spring.version>4.3.0.RELEASE</spring.version>
  </properties>
  
  <dependencies>
  <dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring.version}</version>
		</dependency>
  </dependencies>
</project>

package com.gyq.user.motan;
/**
 * 
 * @Tittle: 
 * @Description:
 * @date:2017年8月15日下午2:57:14
 * @author:高永琴
 */

public interface HelloService {
	String hello(String world);

}

package com.gyq.user.motan;
/**
 * 
 * @Tittle: 
 * @Description:
 * @date:2017年8月15日下午2:58:27
 * @author:高永琴
 */
public class HelloServiceImpl implements HelloService{

	public String hello(String world) {
		// TODO Auto-generated method stub
		return "hello"+world;
	}

}


服务端配置文件

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

<bean id="helloServiceImpl" class="com.gyq.user.motan.HelloServiceImpl"/>
<motan:service interface="com.gyq.user.motan.HelloService" ref="helloServiceImpl" export="8002"/>

</beans>


服务端主函数

package com.gyq.user.motan;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 
 * @Tittle: 
 * @Description:
 * @date:2017年8月15日下午3:02:57
 * @author:高永琴
 */
public class Server {
	public static void main(String args[]) {
		new ClassPathXmlApplicationContext("classpath:motan-service.xml");
		System.out.println("======================classpath:motan-server.xml");
		
	}
	

}

个人感觉motan和dubbo很像,如果懂dubbo,motan很容易理解的。

猜你喜欢

转载自blog.csdn.net/sinat_30777203/article/details/77367832
今日推荐