当前位置: 首页 > 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;板块去留让…...

synchronized 学习

学习源&#xff1a; https://www.bilibili.com/video/BV1aJ411V763?spm_id_from333.788.videopod.episodes&vd_source32e1c41a9370911ab06d12fbc36c4ebc 1.应用场景 不超卖&#xff0c;也要考虑性能问题&#xff08;场景&#xff09; 2.常见面试问题&#xff1a; sync出…...

R语言AI模型部署方案:精准离线运行详解

R语言AI模型部署方案:精准离线运行详解 一、项目概述 本文将构建一个完整的R语言AI部署解决方案,实现鸢尾花分类模型的训练、保存、离线部署和预测功能。核心特点: 100%离线运行能力自包含环境依赖生产级错误处理跨平台兼容性模型版本管理# 文件结构说明 Iris_AI_Deployme…...

JavaScript 中的 ES|QL:利用 Apache Arrow 工具

作者&#xff1a;来自 Elastic Jeffrey Rengifo 学习如何将 ES|QL 与 JavaScript 的 Apache Arrow 客户端工具一起使用。 想获得 Elastic 认证吗&#xff1f;了解下一期 Elasticsearch Engineer 培训的时间吧&#xff01; Elasticsearch 拥有众多新功能&#xff0c;助你为自己…...

Admin.Net中的消息通信SignalR解释

定义集线器接口 IOnlineUserHub public interface IOnlineUserHub {/// 在线用户列表Task OnlineUserList(OnlineUserList context);/// 强制下线Task ForceOffline(object context);/// 发布站内消息Task PublicNotice(SysNotice context);/// 接收消息Task ReceiveMessage(…...

反射获取方法和属性

Java反射获取方法 在Java中&#xff0c;反射&#xff08;Reflection&#xff09;是一种强大的机制&#xff0c;允许程序在运行时访问和操作类的内部属性和方法。通过反射&#xff0c;可以动态地创建对象、调用方法、改变属性值&#xff0c;这在很多Java框架中如Spring和Hiberna…...

微服务商城-商品微服务

数据表 CREATE TABLE product (id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 商品id,cateid smallint(6) UNSIGNED NOT NULL DEFAULT 0 COMMENT 类别Id,name varchar(100) NOT NULL DEFAULT COMMENT 商品名称,subtitle varchar(200) NOT NULL DEFAULT COMMENT 商…...

【VLNs篇】07:NavRL—在动态环境中学习安全飞行

项目内容论文标题NavRL: 在动态环境中学习安全飞行 (NavRL: Learning Safe Flight in Dynamic Environments)核心问题解决无人机在包含静态和动态障碍物的复杂环境中进行安全、高效自主导航的挑战&#xff0c;克服传统方法和现有强化学习方法的局限性。核心算法基于近端策略优化…...

搭建DNS域名解析服务器(正向解析资源文件)

正向解析资源文件 1&#xff09;准备工作 服务端及客户端都关闭安全软件 [rootlocalhost ~]# systemctl stop firewalld [rootlocalhost ~]# setenforce 0 2&#xff09;服务端安装软件&#xff1a;bind 1.配置yum源 [rootlocalhost ~]# cat /etc/yum.repos.d/base.repo [Base…...

CSS | transition 和 transform的用处和区别

省流总结&#xff1a; transform用于变换/变形&#xff0c;transition是动画控制器 transform 用来对元素进行变形&#xff0c;常见的操作如下&#xff0c;它是立即生效的样式变形属性。 旋转 rotate(角度deg)、平移 translateX(像素px)、缩放 scale(倍数)、倾斜 skewX(角度…...

(一)单例模式

一、前言 单例模式属于六大创建型模式,即在软件设计过程中,主要关注创建对象的结果,并不关心创建对象的过程及细节。创建型设计模式将类对象的实例化过程进行抽象化接口设计,从而隐藏了类对象的实例是如何被创建的,封装了软件系统使用的具体对象类型。 六大创建型模式包括…...