2、motan入门程序

准备工作

1、创建项目工程

motan-parent依赖:

<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>com.motan</groupId>
    <artifactId>motan-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <modules>
        <module>motan-api</module>
        <module>motan-client</module>
        <module>motan-server</module>
    </modules>
</project>
View Code

motan-api依赖:

<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>com.motan</groupId>
        <artifactId>motan-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>motan-api</artifactId>
    <packaging>war</packaging>
</project>
View Code

motan-client依赖:

<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>com.motan</groupId>
        <artifactId>motan-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>motan-client</artifactId>
    <packaging>war</packaging>
    
    <dependencies>
        <dependency>
            <groupId>com.motan</groupId>
            <artifactId>motan-api</artifactId>
            <version>${project.parent.version}</version>
        </dependency>
    </dependencies>
</project>
View Code

motan-server依赖:

<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>com.motan</groupId>
        <artifactId>motan-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>motan-server</artifactId>
    <packaging>war</packaging>
    
    <dependencies>
        <dependency>
            <groupId>com.motan</groupId>
            <artifactId>motan-api</artifactId>
            <version>${project.parent.version}</version>
        </dependency>
    </dependencies>
</project>
View Code

注意:motan-client、motan-server必须引用motan-api

2、添加motan依赖(在父工程pom中添加)

 1 <dependencies>
 2         <dependency>
 3             <groupId>com.weibo</groupId>
 4             <artifactId>motan-core</artifactId>
 5             <version>1.0.0</version>
 6         </dependency>
 7         <dependency>
 8             <groupId>com.weibo</groupId>
 9             <artifactId>motan-transport-netty</artifactId>
10             <version>1.0.0</version>
11         </dependency>
12 
13         <dependency>
14             <groupId>com.weibo</groupId>
15             <artifactId>motan-springsupport</artifactId>
16             <version>1.0.0</version>
17         </dependency>
18         <dependency>
19             <groupId>org.springframework</groupId>
20             <artifactId>spring-context</artifactId>
21             <version>4.2.4.RELEASE</version>
22         </dependency>
23 </dependencies>
View Code

这样每个子工程都会引入相同的jar包


入门程序

1、在motan-api这个工程中创建一个service接口

package com.motan.service;

public interface HelloWorldService {

    String hello(String name);
}

2、在motan-server中实现motan-api中的HelloWorldServoce接口

package com.motan.service;

public class HelloWorldServiceImpl implements HelloWorldService{

    @Override
    public String hello(String name) {
        System.out.println(name);
        return "Hello " + name + "!";
    }

}

3、在motan-server中添加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">

    <bean id="helloWorldServiceImpl" class="com.motan.service.HelloWorldServiceImpl" />
    <motan:service interface="com.motan.service.HelloWorldService" ref="helloWorldServiceImpl" export="8002" />
</beans>

4、编写服务端启动程序

package com.motan.server;

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) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:motan-server.xml");
        MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true);
        System.out.println("server start...");
    }
}

5、在motan-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">

    <!-- 具体referer配置。使用方通过beanid使用服务接口类 -->
    <motan:referer id="helloWorldReferer" interface="com.motan.service.HelloWorldService" directUrl="localhost:8002"/>
</beans>

6、编写客户端调用程序

package com.motan.client;

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

import com.motan.service.HelloWorldService;

public class Client {

    public static void main(String[] args) throws InterruptedException {
        ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"classpath:motan-client.xml"});

        HelloWorldService service = (HelloWorldService) ctx.getBean("helloWorldReferer");
        for (int i = 0; i < Integer.MAX_VALUE; i++) {
            System.out.println(service.hello("motan" + i));
            Thread.sleep(1000);
        }
        System.out.println("motan demo is finish.");
        System.exit(0);
    }
}

7、测试(每隔一秒客户端调用一次服务端接口)

①、首先启动服务端应用程序

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
server start...

②、接着启动客户端应用程序

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
Hello motan0!
Hello motan1!
Hello motan2!
Hello motan3!
Hello motan4!
...
每隔一秒调用1次,以下不停的输出
...

③、再来看服务端的控制台

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
server start...
motan0
motan1
motan2
motan3
motan4
...
不停的被调用
...

测试成功 ! 

原文地址:https://www.cnblogs.com/Json1208/p/8784906.html