新浪开源轻量级分布式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的资料请关注脚本之家其它相关文章!

相关文章

  • Java枚举类用法实例

    Java枚举类用法实例

    这篇文章主要介绍了Java枚举类用法,实例分析了java中枚举类的实现与使用技巧,需要的朋友可以参考下
    2015-05-05
  • 简单了解redis常见客户端及Sharding机制原理

    简单了解redis常见客户端及Sharding机制原理

    这篇文章主要介绍了简单了解redis常见客户端及Sharding机制原理,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-09-09
  • SpringMVC中的HandlerAdapter解析

    SpringMVC中的HandlerAdapter解析

    这篇文章主要介绍了SpringMVC中的HandlerAdapter解析,HandlerAdapter是一个关键的组件,用于将请求与处理程序方法进行适配和调度,它充当了控制器和处理程序之间的桥梁,负责将请求的参数和处理程序方法进行匹配,并将结果返回给前端,需要的朋友可以参考下
    2023-10-10
  • java实现单机版五子棋小游戏

    java实现单机版五子棋小游戏

    这篇文章主要为大家详细介绍了java实现单机版五子棋小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-12-12
  • 用3个实例从原理到实战讲清楚Log4j史诗级漏洞

    用3个实例从原理到实战讲清楚Log4j史诗级漏洞

    最近应该很多人都在关注着一个漏洞Apache Log4j 2远程代码执行,该漏洞一旦被攻击者利用会造成严重危害,这篇文章主要给大家介绍了关于如何用3个实例从原理到实战讲清楚Log4j史诗级漏洞的相关资料,需要的朋友可以参考下
    2021-12-12
  • 从Myeclipse 导入到eclipse中无法识别为 web项目 问题的解决步骤

    从Myeclipse 导入到eclipse中无法识别为 web项目 问题的解决步骤

    这篇文章主要介绍了从Myeclipse 导入到eclipse中无法识别为 web项目 问题的解决步骤,需要的朋友可以参考下
    2018-05-05
  • Java实现选择排序算法的实例教程

    Java实现选择排序算法的实例教程

    这篇文章主要介绍了Java实现选择排序算法的实例教程,选择排序的时间复杂度为О(n&sup2;),需要的朋友可以参考下
    2016-05-05
  • mybatis TypeHandler注入spring的依赖方式

    mybatis TypeHandler注入spring的依赖方式

    这篇文章主要介绍了mybatis TypeHandler注入spring的依赖方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • 详解java重载与覆写的区别

    详解java重载与覆写的区别

    很多同学会重载和重写分不清楚,这篇文章主要介绍了详解java重载与覆写的区别,有需要的朋友可以了解一下。
    2016-11-11
  • Spring MVC获取参数和自定义参数类型转换器及编码过滤器

    Spring MVC获取参数和自定义参数类型转换器及编码过滤器

    这篇文章主要为大家详细介绍了Spring MVC获取参数和自定义参数类型转换器及编码过滤器,文中通过代码示例介绍的非常详细,具有一定的参考价值,需要的朋友可以参考下
    2023-06-06

最新评论