Motan RPC Demo

Motan RPC Demo

摘要

Motan RPC Demo ,springboot , motan集群使用示例,motan注解使用示例

什么是Motan

Motan是一套高性能、易于使用的分布式远程服务调用(RPC)框架。

功能

  1. 支持通过spring配置方式集成,无需额外编写代码即可为服务提供分布式调用能力。
  2. 支持集成consul、zookeeper等配置服务组件,提供集群环境的服务发现及治理能力。
  3. 支持动态自定义负载均衡、跨机房流量调整等高级服务调度能力。
  4. 基于高并发、高负载场景进行优化,保障生产环境下RPC服务高可用。

git地址 https://github.com/weibocom/motan

结合springboot注解示例

Consul安装与启动

wget https://releases.hashicorp.com/consul/0.6.4/consul_0.6.4_linux_amd64.zip
unzip consul_0.6.4_linux_amd64.zip
sudo mv consul /bin
consul agent -dev -bind 192.168.1.65 -client 0.0.0.0

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>net.javablog</groupId>
    <artifactId>motan</artifactId>
    <version>0.0.1</version>
    <name>test_motan3</name>

    <properties>
        <motan_version>0.2.2</motan_version>
    </properties>

    <dependencies>

        <!-- motan -->
        <dependency>
            <groupId>com.weibo</groupId>
            <artifactId>motan-core</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-registry-consul</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-protocol-yar</artifactId>
            <version>${motan_version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.3.3</version>
        </dependency>

        <!-- spring web -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>

        <!-- spring boot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>log4j-over-slf4j</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>logback-classic</artifactId>
                    <groupId>ch.qos.logback</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <artifactId>slf4j-api</artifactId>
            <groupId>org.slf4j</groupId>
            <version>1.7.11</version>
        </dependency>
        <dependency>
            <artifactId>slf4j-log4j12</artifactId>
            <groupId>org.slf4j</groupId>
            <version>1.7.11</version>
        </dependency>


    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.spring.platform</groupId>
                <artifactId>platform-bom</artifactId>
                <version>1.1.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


</project>

demo运行截图

网页127.0.0.1:8080是请求客户端,客户端会再发起rpc请求到server。
可以看到控制台上server已经打印出被请求到了。

demo的完整代码

https://coding.net/u/javacore/p/motan_demo/git

参考

https://github.com/weibocom/motan/tree/master/motan-demo
https://github.com/weibocom/motan/wiki/zh_quickstart#%E4%BD%BF%E7%94%A8%E6%B3%A8%E8%A7%A3%E6%96%B9%E5%BC%8F%E9%85%8D%E7%BD%AEmotan