新浪开源轻量级分布式RPC框架motan简单示例解析

 更新时间:2022年03月05日 17:08:29   作者:kl  
这篇文章主要为大家介绍了新浪开源轻量级分布式RPC框架motan的简单示例解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步

前言

好消息,支撑微博千亿调用的轻量级 RPC 框架 Motan 在2016年5月份正式开源了,业界现在除了Dubbo 和 DubboX典型的分布式RPC服务治理型框架外,又多了一个优秀的分布式RPC了。心动了吗?使用过dubbo的话,so easy的上手,官方实例如下,动起来吧

我的demo地址,参考官方实例的简单demo,包含zookeeper注册中心,以及服务监控平台:https://coding.net/u/kailingchen/p/motan_Test/git

概述

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

github项目地址:https://github.com/weibocom/motan

功能

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

简单调用示例

在pom中添加依赖

<dependency>
    <groupId>com.weibogroupId>
    <artifactId>motan-coreartifactId>
    <version>0.1.1version>
dependency>
<dependency>
    <groupId>com.weibogroupId>
    <artifactId>motan-transport-nettyartifactId>
    <version>0.1.1version>
dependency>  <dependency>
    <groupId>com.weibogroupId>
    <artifactId>motan-springsupportartifactId>
    <version>0.1.1version>
dependency>
<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-contextartifactId>
    <version>4.2.4.RELEASEversion>
<dependency>

为调用方和服务方创建公共接口

src/main/java/quickstart/FooService.java

package quickstart; public interface FooService { public String hello(String name);
}

编写业务接口逻辑、创建并启动RPC Server

src/main/java/quickstart/FooServiceImpl.java

package quickstart; public class FooServiceImpl implements FooService { public String hello(String name) { System.out.println(name + " invoked rpc service"); return "hello " + name;
    }
}

src/main/resources/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="serviceImpl" class="quickstart.FooServiceImpl" />  <motan:service interface="quickstart.FooService" ref="serviceImpl" export="8002" />

src/main/java/quickstart/Server.java

package quickstart; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Server { public static void main(String[] args) throws InterruptedException { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:motan_server.xml"); System.out.println("server start...");
    }
}

执行Server类中的main函数将会启动Motan服务,并监听8002端口.

创建并执行RPC Client

src/main/resources/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">  <motan:referer id="remoteService" interface="quickstart.FooService" directUrl="localhost:8002"/>

src/main/java/quickstart/Client.java

package quickstart; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Client { public static void main(String[] args) throws InterruptedException { ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:motan_client.xml"); FooService service = (FooService) ctx.getBean("remoteService"); System.out.println(service.hello("motan"));
    }
}

执行Client类中的main函数将执行一次远程调用,并输出结果。

集群调用示例

在集群环境下使用Motan需要依赖外部服务发现组件,目前支持consul或zookeeper。

使用CONSUL作为注册中心

Consul安装与启动

安装(官方文档

# 这里以linux为例
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

ui后台 http://localhost:8500/ui

Motan-Consul配置

在server和client中添加motan-registry-consul依赖

<dependency>
    <groupId>com.weibogroupId>
    <artifactId>motan-registry-consulartifactId>
    <version>0.1.1version>
<dependency>

在server和client的配置文件中分别增加consul registry定义。

<motan:registry regProtocol="consul" name="my_consul" address="127.0.0.1:8500"/>

在Motan client及server配置改为通过registry服务发现。

client

<motan:referer id="remoteService" interface="quickstart.FooService" registry="my_consul"/>

server

<motan:service interface="quickstart.FooService" ref="serviceImpl" registry="my_consul" export="8002" />

server程序启动后,需要显式调用心跳开关,注册到consul。

MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true)

进入ui后台查看服务是否正常提供调用

启动client,调用服务

使用ZOOKEEPER作为注册中心

ZooKeeper安装与启动

单机版安装与启动

wget http://mirrors.cnnic.cn/apache/zookeeper/zookeeper-3.4.8/zookeeper-3.4.8.tar.gz
tar zxvf zookeeper-3.4.8.tar.gz

cd zookeeper-3.4.8/conf/
cp zoo_sample.cfg zoo.cfg

cd ../
sh bin/zkServer.sh start

Motan-ZooKeeper配置

在server和client中添加motan-registry-zookeeper依赖

<dependency>
    <groupId>com.weibogroupId>
    <artifactId>motan-registry-zookeeperartifactId>
    <version>0.1.1version>
<dependency>

在server和client的配置文件中分别增加zookeeper registry定义。

zookeeper为单节点

<motan:registry regProtocol="zookeeper" name="my_zookeeper" address="127.0.0.1:2181"/>

zookeeper多节点集群

<motan:registry regProtocol="zookeeper" name="my_zookeeper" address="127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183"/>

在Motan client及server配置改为通过registry服务发现。

client

<motan:referer id="remoteService" interface="quickstart.FooService" registry="my_zookeeper"/>

server

<motan:service interface="quickstart.FooService" ref="serviceImpl" registry="my_zookeeper" export="8002" />

server程序启动后,需要显式调用心跳开关,注册到zookeeper。

MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true)

启动client,调用服务

以上就是新浪开源轻量级分布式RPC框架motan简单示例解析的详细内容,更多关于新浪开源轻量级分布式RPC框架motan的资料请关注脚本之家其它相关文章!

相关文章

  • springBoot之如何获取接口请求数据和返回数据实现日志

    springBoot之如何获取接口请求数据和返回数据实现日志

    这篇文章主要介绍了springBoot之如何获取接口请求数据和返回数据实现日志问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-04-04
  • Mybatis-Plus实现公共字段自动赋值的方法

    Mybatis-Plus实现公共字段自动赋值的方法

    这篇文章主要介绍了Mybatis-Plus实现公共字段自动赋值的方法,涉及到通用字段自动填充的最佳实践总结,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-07-07
  • Java利用POI实现导入导出Excel表格

    Java利用POI实现导入导出Excel表格

    这篇文章主要为大家详细介绍了Java利用POI实现导入导出Excel表格,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-08-08
  • mybatis那些约定的配置你真的都了解吗(经验总结)

    mybatis那些约定的配置你真的都了解吗(经验总结)

    mybatsi中Mapper和xml文件之间有很多约定俗称的规则,比如名称匹配,包扫描,别名等,这些规则是什么。如果想更加灵活,该如何配置呢?今天就给大家讲一下如何配置mybatsi的xml文件
    2021-06-06
  • JAVA把结果保留两位小数的3种方法举例

    JAVA把结果保留两位小数的3种方法举例

    在写程序的时候,有时候可能需要设置小数的位数,所以下面这篇文章主要给大家介绍了关于JAVA把结果保留两位小数的3种方法,文章通过代码介绍的非常详细,需要的朋友可以参考下
    2024-08-08
  • SpringBoot API增加version版本号方式

    SpringBoot API增加version版本号方式

    这篇文章主要介绍了SpringBoot API增加version版本号方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-10-10
  • java Comparable和Comparator的区别及作用面试精讲

    java Comparable和Comparator的区别及作用面试精讲

    这篇文章主要为大家介绍了java Comparable和Comparator的区别及作用面试精讲,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-10-10
  • Spring security自定义用户认证流程详解

    Spring security自定义用户认证流程详解

    这篇文章主要介绍了Spring security自定义用户认证流程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • java实现乘地铁方案的最优选择(票价,距离)

    java实现乘地铁方案的最优选择(票价,距离)

    这篇文章主要介绍了java实现乘地铁方案的最优选择(票价,距离),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-07-07
  • 学习Java的Date、Calendar日期操作

    学习Java的Date、Calendar日期操作

    Java开发过程中避免不了日期相关操作,这篇文章总结了一些Date、Calendar的常用方法,需要的朋友可以参考下
    2015-07-07

最新评论