本文小编为大家详细介绍“轻量级分布式RPC框架motan怎么使用”,内容详细,步骤清晰,细节处理妥当,希望这篇“轻量级分布式RPC框架motan怎么使用”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
Motan是一套高性能、易于使用的分布式远程服务调用(RPC)框架。
支持通过spring配置方式集成,无需额外编写代码即可为服务提供分布式调用能力。
支持集成consul、zookeeper等配置服务组件,提供集群环境的服务发现及治理能力。
支持动态自定义负载均衡、跨机房流量调整等高级服务调度能力。
基于高并发、高负载场景进行优化,保障生产环境下RPC服务高可用。
<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);
}
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端口.
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安装与启动
安装(官方文档)
# 这里以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
在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安装与启动
单机版安装与启动
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
在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怎么使用”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。
亿速云「轻量应用服务器」,开箱即用、面向轻量应用场景,一站式融合常用基础云服务与热门开源软件,一键构建应用,上手更加简单便捷,价格只需23元/月!点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。