当前位置: 首页 > news >正文

[RPC] Motan快速开始

文章目录

  • 一、概述
  • 二、功能
  • 三、XML配置使用
    • 1、同步调用
      • 1.1、pom中添加依赖
      • 1.2、为调用方和服务方创建公共接口。
      • 1.3、编写业务接口逻辑、创建并启动RPC Server。
      • 1.4、创建并执行RPC Client。
    • 2、异步调用
      • 2.1、在接口类上加@MotanAsync注解
      • 2.2、在client端配置motan_client.xml时,在同步调用配置的基础上,只需要修改referer的interface为Motan自动生成的接口类即可。
    • 3、Zookeeper注册中心配置
      • 3.1 在server和client中 添加maven依赖
      • 3.2 在server和client 的配置文件中分别增加zookeeper registry定义
      • 3.3 在Motan client及server配置改为通过registry服务发现。
      • 3.4 server程序启动后,需要显式调用心跳开关,注册到zookeeper。
      • 3.5 启动client,调用服务
  • 四、注解配置使用
    • server端配置
      • 1、声明Annotation用来指定需要解析的包名
      • 2、配置ProtocolConfig、RegistryConfig、BasicServiceConfig的bean对象
      • 3、service的实现类上添加@MotanService注解,注解的配置参数与xml配置方式的service标签一致。
      • 4、使用spring-boot启动服务
    • client端配置
      • 1、声明Annotation、protocolConfig、RegistryConfig的配置bean。
      • 2、配置basicRefererConfig bean
      • 3、在使用motan service 的对象上添加@MotanReferer注解,
      • 4、使用spring-boot启动client

一、概述

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

二、功能

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

三、XML配置使用

1、同步调用

1.1、pom中添加依赖

<dependency><groupId>com.weibo</groupId><artifactId>motan-core</artifactId><version>RELEASE</version></dependency><dependency><groupId>com.weibo</groupId><artifactId>motan-transport-netty</artifactId><version>RELEASE</version></dependency><!-- only needed for spring-based features --><dependency><groupId>com.weibo</groupId><artifactId>motan-springsupport</artifactId><version>RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.2.4.RELEASE</version></dependency>

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

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

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

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.xsdhttp://api.weibo.com/schema/motan http://api.weibo.com/schema/motan.xsd"><!-- service implemention bean --><bean id="serviceImpl" class="quickstart.FooServiceImpl" /><!-- exporting service by Motan --><motan:service interface="quickstart.FooService" ref="serviceImpl" export="8002" />
</beans>

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端口.

1.4、创建并执行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.xsdhttp://api.weibo.com/schema/motan http://api.weibo.com/schema/motan.xsd"><!-- reference to the remote service --><motan:referer id="remoteService" interface="quickstart.FooService" directUrl="localhost:8002"/>
</beans>

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"));}
}

2、异步调用

异步调用与同步调用基本配置完全一样,只需要在接口类中加上@MotanAsync注解,然后client端稍作修改。server端不需要做任何修改。具体步骤如下:

2.1、在接口类上加@MotanAsync注解

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

编译时,
Motan自动生成异步service类,生成路径为target/generated-sources/annotations/,生成的类名为service名加上Async。
例如 service类名为FooService.java,则自动生成的类名为FooServiceAsync.java。
另外,需要将motan自动生产类文件的路径配置为项目source path,可以使用maven plugin或手动配置。
pom.xml配置如下:

<plugin><groupId>org.codehaus.mojo</groupId><artifactId>build-helper-maven-plugin</artifactId><version>RELEASE</version><executions><execution><phase>generate-sources</phase><goals><goal>add-source</goal></goals><configuration><sources><source>${project.build.directory}/generated-sources/annotations</source></sources></configuration></execution></executions>
</plugin>

2.2、在client端配置motan_client.xml时,在同步调用配置的基础上,只需要修改referer的interface为Motan自动生成的接口类即可。

<motan:referer id="remoteService" interface="quickstart.FooServiceAsync" directUrl="localhost:8002"/>

异步使用方式如下:

public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] {"classpath:motan_client.xml"});FooServiceAsync service = (FooServiceAsync) ctx.getBean("remoteService");// sync callSystem.out.println(service.hello("motan"));// async callResponseFuture future = service.helloAsync("motan async ");System.out.println(future.getValue());// multi callResponseFuture future1 = service.helloAsync("motan async multi-1");ResponseFuture future2 = service.helloAsync("motan async multi-2");System.out.println(future1.getValue() + ", " + future2.getValue());// async with listenerFutureListener listener = new FutureListener() {@Overridepublic void operationComplete(Future future) throws Exception {System.out.println("async call "+ (future.isSuccess() ? "sucess! value:" + future.getValue() : "fail! exception:"+ future.getException().getMessage()));}};ResponseFuture future3 = service.helloAsync("motan async multi-1");ResponseFuture future4 = service.helloAsync("motan async multi-2");future3.addListener(listener);future4.addListener(listener);
}

3、Zookeeper注册中心配置

3.1 在server和client中 添加maven依赖

<dependency><groupId>com.weibo</groupId><artifactId>motan-registry-zookeeper</artifactId><version>RELEASE</version>
</dependency>

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

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

3.3 在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" />

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

MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true)

3.5 启动client,调用服务


四、注解配置使用

server端配置

1、声明Annotation用来指定需要解析的包名

 @Beanpublic AnnotationBean motanAnnotationBean() {AnnotationBean motanAnnotationBean = new AnnotationBean();motanAnnotationBean.setPackage("com.weibo.motan.demo.server");return motanAnnotationBean;}

2、配置ProtocolConfig、RegistryConfig、BasicServiceConfig的bean对象

功能与xml配置中的protocol、registry、basicService标签一致。

 @Bean(name = "demoMotan")public ProtocolConfigBean protocolConfig1() {ProtocolConfigBean config = new ProtocolConfigBean();config.setDefault(true);config.setName("motan");config.setMaxContentLength(1048576);return config;}@Bean(name = "registryConfig1")public RegistryConfigBean registryConfig() {RegistryConfigBean config = new RegistryConfigBean();config.setRegProtocol("local");return config;}@Beanpublic BasicServiceConfigBean baseServiceConfig() {BasicServiceConfigBean config = new BasicServiceConfigBean();config.setExport("demoMotan:8002");config.setGroup("testgroup");config.setAccessLog(false);config.setShareChannel(true);config.setModule("motan-demo-rpc");config.setApplication("myMotanDemo");config.setRegistry("registryConfig1");return config;}

3、service的实现类上添加@MotanService注解,注解的配置参数与xml配置方式的service标签一致。

 @MotanService(export = "demoMotan:8002")public class MotanDemoServiceImpl implements MotanDemoService {public String hello(String name) {System.out.println(name);return "Hello " + name + "!";}}

4、使用spring-boot启动服务

 @EnableAutoConfiguration@SpringBootApplicationpublic class SpringBootRpcServerDemo {public static void main(String[] args) {System.setProperty("server.port", "8081");ConfigurableApplicationContext context =  SpringApplication.run(SpringBootRpcServerDemo.class, args);MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true);System.out.println("server start...");}}

client端配置

1、声明Annotation、protocolConfig、RegistryConfig的配置bean。

方式与server端配置类似。

2、配置basicRefererConfig bean

 @Bean(name = "motantestClientBasicConfig")public BasicRefererConfigBean baseRefererConfig() {BasicRefererConfigBean config = new BasicRefererConfigBean();config.setProtocol("demoMotan");config.setGroup("motan-demo-rpc");config.setModule("motan-demo-rpc");config.setApplication("myMotanDemo");config.setRegistry("registry");config.setCheck(false);config.setAccessLog(true);config.setRetries(2);config.setThrowException(true);return config;}

3、在使用motan service 的对象上添加@MotanReferer注解,

注册配置与xml方式的referer标签一致

 @RestControllerpublic class HelloController {@MotanReferer(basicReferer = "motantestClientBasicConfig", group = "testgroup", directUrl = "127.0.0.1:8002")MotanDemoService service;@RequestMapping("/")@ResponseBodypublic String home() {String result = service.hello("test");return result;}}

4、使用spring-boot启动client

 @EnableAutoConfiguration@SpringBootApplicationpublic class SpringBootRpcClientDemo {public static void main(String[] args) {SpringApplication.run(SpringBootRpcClientDemo.class, args);}}

官网文档

相关文章:

[RPC] Motan快速开始

文章目录 一、概述二、功能三、XML配置使用1、同步调用1.1、pom中添加依赖1.2、为调用方和服务方创建公共接口。1.3、编写业务接口逻辑、创建并启动RPC Server。1.4、创建并执行RPC Client。 2、异步调用2.1、在接口类上加MotanAsync注解2.2、在client端配置motan_client.xml时…...

仿美团外卖微信小程序源码/美团外卖优惠券领劵小程序-自带流量主模式

源码简介&#xff1a; 仿美团外卖微信小程序源码&#xff0c;它是美团外卖优惠券领劵小程序&#xff0c;还自带流量主模式。可以领取外卖优惠券的小程序。实用方便。 美团优惠券小程序带举牌小人带菜谱流量主模式&#xff0c;挺多外卖小程序的&#xff0c;但是都没有搭建教程…...

【Python】Windows跟随程序启动和关闭系统代理

前言 在日常使用计算机时&#xff0c;偶尔可能需要配置代理来访问特定的网络资源或进行网络调试。 当在使用mitmproxy 时候&#xff0c; 程序开始前&#xff0c;需要手动打开系统代理&#xff1b;程序解释后&#xff0c;需要手动关闭系统代理。 这些重复性且没有技术含量工作…...

信钰证券:华为汽车概念股持续活跃 圣龙股份斩获12连板

近期&#xff0c;华为轿车概念股在A股商场遭到热捧&#xff0c;多只股票迭创前史新高。10月23日&#xff0c;华为轿车概念股再度走强&#xff0c;到收盘&#xff0c;板块内圣龙股份、银宝山新涨停&#xff0c;轿车ETF在重仓股提振下盘中一度上涨近2%。业界人士认为&#xff0c;…...

LSM Tree 深度解析

我们将深入探讨日志结构合并树&#xff0c;也称为LSM Tree&#xff1a;这是许多高度可扩展的NoSQL分布式键值型数据库的基础数据结构&#xff0c;例如Amazon的DynamoDB、Cassandra和ScyllaDB。这些数据库的设计被认为支持比传统关系数据库更高的写入速率。我们将看到LSM Tree如…...

BurpSuite安装

下载 BurpSuite 下载 Java17 下载后确定版本 java -version获取启动器 密钥生成器 破解 将下载的 BurpSuite、启动器、密钥生成器&#xff0c;放入同一个目录 打开 CMD 进入该目录 启动密钥生成器 java -jar burp-keygen-scz.jar开启新的CMD&#xff0c;进入该目录 启动…...

VB.NET 三层登录系统实战:从设计到部署全流程详解

目录 前言&#xff1a; 什么是三层 为什么要用到三层: 饭店→软件 理解: 过程: 1.三层包图: 2.数据库 3.三层项目 4.用户界面 5.添加引用 代码实现: Entity层 BLL层 DAL层 UI层 总结: 前言&#xff1a; 什么是三层 三层就是把各个功能模块划分为表示层&#…...

【前端性能】性能优化手段-高频面试题

持续更新.............................最近更新2023/10/24 1. 讲一下png8、png16、png32的区别&#xff0c;并简单讲讲 png 的压缩原理 PNG8、PNG16、PNG32 是 PNG 图像格式的不同变种&#xff0c;它们主要区别在于颜色深度和透明度支持的不同。 区别 PNG8&#xff1a; PN…...

cleanmymacX4.14免费版mac清除浏览器缓存软件

当我们使用浏览器访问网站时&#xff0c;浏览器会自动缓存一些数据&#xff0c;比如网页缓存、DNS缓存、插件缓存、SSL证书缓存和Cookie缓存等。虽然有些缓存可以提高浏览器的使用体验&#xff0c;但是缓存过多也会导致一些问题&#xff0c;比如网页更新后浏览器仍然显示旧的内…...

分享个包含各省、市、区的编码数据的在线静态资源脚本

在翻《SpringBootVue3》——十三尼克陈作者的大型前后端分离项目实战里面&#xff0c;在看到地址管理的部分时&#xff0c;发现了该作者记录有一个静态的地址资源脚本 这里做个记录&#xff0c;打点 一、引入js <script src"https://s.yezgea02.com/1641120061385/td…...

Elasticsearch聚合----aggregations的简单使用

文章目录 Getting started1、搜索 address 中包含 mill 的所有人的年龄分布以及平均年龄&#xff0c;但不显示这些人的详情2、size0不展示命中记录&#xff0c;只展示聚合结果3、按照年龄聚合&#xff0c;并且请求这些年龄段的这些人的平均薪资4、查出所有年龄分布&#xff0c;…...

GOPS·2023上海站 | 提前剧透!阿里、腾讯、字节、擎创等专家齐聚上海,共话互联网运维

一、前言 2023年10月26日-27日&#xff0c;第二十一届 GOPS 全球运维大会 2023 上海站即将举行。作为年终前最后一场面向 IT 技术从业者的高端运维盛会。大会上&#xff0c;来自腾讯、阿里、字节跳动、抖音、美团、擎创科技等明星专家&#xff0c;将带来十大互联网行业精彩主…...

防关联浏览器推荐:MuLogin指纹浏览器安全登录多平台账号

在现今的数字时代&#xff0c;我们的生活离不开互联网。我们使用在线平台进行银行交易、购物、社交媒体互动和其他各种活动。为了保护个人隐私和账号安全&#xff0c;我们需要寻找一种安全且方便的方式来管理我们的在线账号。MuLogin指纹浏览器正是为了满足这些需求而设计的一款…...

部署SeaTunnel单节点Standalone 模式环境

1.前置准备: SeaTunnel支持运行在JDK8及以上环境。该步骤需要用户自行安装JDK环境。 2.下载安装包 本次部署使用的是2.3.1版本。如果你需要下载其它版本&#xff0c;可以从如下网址中查询对应的版本。 Apache SeaTunnel ​ mkdir ~/seatunnel cd ~/seatunnelwget https://dl…...

二十三、设计模式之组合模式![

目录 二十三、设计模式之组合模式能帮我们干什么&#xff1f;主要解决什么问题&#xff1f;优缺点优点缺点&#xff1a; 使用的场景理解实现角色组合模式 总结 魔战已经完结。成功登顶。占领敌军最高峰。 二十三、设计模式之组合模式 “组合模式”也被称为“部分整体模式”该…...

hbase和aerospike基础概念及所对应的python包API使用

Hbase Hbase shell常用操作 1.创建表 create table name,column familytable name&#xff1a;表名 column family:列族名 2.查看所有表名称 list3.插入操作 put table name,row1,column family:column name,valuerow1:行键(即Row Key) column family:column name&#xf…...

监测难?误差大?北斗突破铁路监测预警难题,24小时全方位守护

受极端气象和复杂地形地质条件的影响&#xff0c;近年来铁路沿线地质灾害易发频发。为防范化解重大安全风险&#xff0c;提高自然灾害防治能力&#xff0c;国务院决策部署制定了《关于加强铁路自然灾害监测预警工作的指导意见》&#xff0c;强调了利用先进技术和手段开展各类自…...

kafka入门03——简单实战

目录 安装Java 安装Zookeeper 安装Kafka 生产与消费 主要是记录下Kafka的安装配置过程&#xff0c;前置条件需要安装jdk和zookeeper。 安装Java 1.Oracle官网下载对应jdk安装包 官网地址&#xff1a;Java Downloads | Oracle 好人分享了下载需要的oracle账号&#xff0c…...

工作两年,本地git分支达到了惊人的361个,该怎么快速清理呢?

说在前面 不知道大家平时工作的时候会不会需要经常新建git分支来开发新需求呢&#xff1f;在我这边工作的时候&#xff0c;需求都是以issue的形式来进行开发&#xff0c;每个issue新建一个关联的分支来进行开发&#xff0c;这样可以通过issue看到一个需求完整的开发记录&#x…...

行业追踪,2023-10-24

自动复盘 2023-10-24 凡所有相&#xff0c;皆是虚妄。若见诸相非相&#xff0c;即见如来。 k 线图是最好的老师&#xff0c;每天持续发布板块的rps排名&#xff0c;追踪板块&#xff0c;板块来开仓&#xff0c;板块去清仓&#xff0c;丢弃自以为是的想法&#xff0c;板块去留让…...

椭圆曲线密码学(ECC)

一、ECC算法概述 椭圆曲线密码学&#xff08;Elliptic Curve Cryptography&#xff09;是基于椭圆曲线数学理论的公钥密码系统&#xff0c;由Neal Koblitz和Victor Miller在1985年独立提出。相比RSA&#xff0c;ECC在相同安全强度下密钥更短&#xff08;256位ECC ≈ 3072位RSA…...

Linux相关概念和易错知识点(42)(TCP的连接管理、可靠性、面临复杂网络的处理)

目录 1.TCP的连接管理机制&#xff08;1&#xff09;三次握手①握手过程②对握手过程的理解 &#xff08;2&#xff09;四次挥手&#xff08;3&#xff09;握手和挥手的触发&#xff08;4&#xff09;状态切换①挥手过程中状态的切换②握手过程中状态的切换 2.TCP的可靠性&…...

页面渲染流程与性能优化

页面渲染流程与性能优化详解&#xff08;完整版&#xff09; 一、现代浏览器渲染流程&#xff08;详细说明&#xff09; 1. 构建DOM树 浏览器接收到HTML文档后&#xff0c;会逐步解析并构建DOM&#xff08;Document Object Model&#xff09;树。具体过程如下&#xff1a; (…...

JVM暂停(Stop-The-World,STW)的原因分类及对应排查方案

JVM暂停(Stop-The-World,STW)的完整原因分类及对应排查方案,结合JVM运行机制和常见故障场景整理而成: 一、GC相关暂停​​ 1. ​​安全点(Safepoint)阻塞​​ ​​现象​​:JVM暂停但无GC日志,日志显示No GCs detected。​​原因​​:JVM等待所有线程进入安全点(如…...

MySQL用户和授权

开放MySQL白名单 可以通过iptables-save命令确认对应客户端ip是否可以访问MySQL服务&#xff1a; test: # iptables-save | grep 3306 -A mp_srv_whitelist -s 172.16.14.102/32 -p tcp -m tcp --dport 3306 -j ACCEPT -A mp_srv_whitelist -s 172.16.4.16/32 -p tcp -m tcp -…...

如何理解 IP 数据报中的 TTL?

目录 前言理解 前言 面试灵魂一问&#xff1a;说说对 IP 数据报中 TTL 的理解&#xff1f;我们都知道&#xff0c;IP 数据报由首部和数据两部分组成&#xff0c;首部又分为两部分&#xff1a;固定部分和可变部分&#xff0c;共占 20 字节&#xff0c;而即将讨论的 TTL 就位于首…...

Mac下Android Studio扫描根目录卡死问题记录

环境信息 操作系统: macOS 15.5 (Apple M2芯片)Android Studio版本: Meerkat Feature Drop | 2024.3.2 Patch 1 (Build #AI-243.26053.27.2432.13536105, 2025年5月22日构建) 问题现象 在项目开发过程中&#xff0c;提示一个依赖外部头文件的cpp源文件需要同步&#xff0c;点…...

微软PowerBI考试 PL300-在 Power BI 中清理、转换和加载数据

微软PowerBI考试 PL300-在 Power BI 中清理、转换和加载数据 Power Query 具有大量专门帮助您清理和准备数据以供分析的功能。 您将了解如何简化复杂模型、更改数据类型、重命名对象和透视数据。 您还将了解如何分析列&#xff0c;以便知晓哪些列包含有价值的数据&#xff0c;…...

Leetcode33( 搜索旋转排序数组)

题目表述 整数数组 nums 按升序排列&#xff0c;数组中的值 互不相同 。 在传递给函数之前&#xff0c;nums 在预先未知的某个下标 k&#xff08;0 < k < nums.length&#xff09;上进行了 旋转&#xff0c;使数组变为 [nums[k], nums[k1], …, nums[n-1], nums[0], nu…...

QT开发技术【ffmpeg + QAudioOutput】音乐播放器

一、 介绍 使用ffmpeg 4.2.2 在数字化浪潮席卷全球的当下&#xff0c;音视频内容犹如璀璨繁星&#xff0c;点亮了人们的生活与工作。从短视频平台上令人捧腹的搞笑视频&#xff0c;到在线课堂中知识渊博的专家授课&#xff0c;再到影视平台上扣人心弦的高清大片&#xff0c;音…...