溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

輕量級分布式RPC框架motan怎么使用

發(fā)布時間:2022-03-07 09:08:04 來源:億速云 閱讀:147 作者:iii 欄目:開發(fā)技術(shù)

本文小編為大家詳細(xì)介紹“輕量級分布式RPC框架motan怎么使用”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“輕量級分布式RPC框架motan怎么使用”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識吧。

概述

Motan是一套高性能、易于使用的分布式遠(yuǎn)程服務(wù)調(diào)用(RPC)框架。

功能

  • 支持通過spring配置方式集成,無需額外編寫代碼即可為服務(wù)提供分布式調(diào)用能力。

  • 支持集成consul、zookeeper等配置服務(wù)組件,提供集群環(huán)境的服務(wù)發(fā)現(xiàn)及治理能力。

  • 支持動態(tài)自定義負(fù)載均衡、跨機(jī)房流量調(diào)整等高級服務(wù)調(diào)度能力。

  • 基于高并發(fā)、高負(fù)載場景進(jìn)行優(yōu)化,保障生產(chǎn)環(huán)境下RPC服務(wù)高可用。

簡單調(diào)用示例

在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>

為調(diào)用方和服務(wù)方創(chuàng)建公共接口

src/main/java/quickstart/FooService.java

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

編寫業(yè)務(wù)接口邏輯、創(chuàng)建并啟動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...");
    }
}

執(zhí)行Server類中的main函數(shù)將會啟動Motan服務(wù),并監(jiān)聽8002端口.

創(chuàng)建并執(zhí)行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"));
    }
}

執(zhí)行Client類中的main函數(shù)將執(zhí)行一次遠(yuǎn)程調(diào)用,并輸出結(jié)果。

集群調(diào)用示例

在集群環(huán)境下使用Motan需要依賴外部服務(wù)發(fā)現(xiàn)組件,目前支持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

啟動(官方文檔)

測試環(huán)境啟動:
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服務(wù)發(fā)現(xiàn)。

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程序啟動后,需要顯式調(diào)用心跳開關(guān),注冊到consul。

MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true)

進(jìn)入ui后臺查看服務(wù)是否正常提供調(diào)用

啟動client,調(diào)用服務(wù)

使用ZOOKEEPER作為注冊中心

ZooKeeper安裝與啟動

單機(jī)版安裝與啟動

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為單節(jié)點

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

zookeeper多節(jié)點集群

<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服務(wù)發(fā)現(xiàn)。

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程序啟動后,需要顯式調(diào)用心跳開關(guān),注冊到zookeeper。

MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true)

啟動client,調(diào)用服務(wù)

讀到這里,這篇“輕量級分布式RPC框架motan怎么使用”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領(lǐng)會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

rpc
AI