java rpc motan_RPC框架motan使用

article/2025/9/11 5:22:48

简介

motan是新浪微博开源的一套轻量级、方便使用的RPC框架

Hello World

使用的过程分为Server端和Client端,Server提供RCP的服务接口,Client端发起调用获取结果。

maven的pom文件配置

0.2.1

com.weibo

motan-core

${motan.version}

com.weibo

motan-transport-netty

${motan.version}

com.weibo

motan-springsupport

${motan.version}

Server 端

暴露的接口

package com.raventech.user.motan;

/**

* @author liuhaiming on 10/11/2016.

*/

public interface HelloService {

String hello(String world);

}

暴露接口的实现类

package com.raventech.user.motan;

/**

* @author liuhaiming on 10/11/2016.

*/

public class HelloServiceImpl implements HelloService {

@Override

public String hello(String world) {

return "hello " + world;

}

}

xml配置文件,暴露接口

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

启动服务的方法(运行main方法就可以启动服务了)

package com.raventech.user;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**

* @author liuhaiming on 10/11/2016.

*/

public class Server {

public static void main(String[] args) {

new ClassPathXmlApplicationContext("classpath:motan-server.xml");

System.out.println("Server start ...");

}

}

Client 端

要请求的接口(不论包名还是类名都要和Server端的一样)

package com.raventech.user.motan;

/**

* @author liuhaiming on 10/11/2016.

*/

public interface HelloService {

String hello(String world);

}

xml配置文件,获取接口信息

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

调用服务的方法(运行main方法就可以调用服务了)

package com.raventech.web;

import com.raventech.user.motan.HelloService;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**

* @author liuhaiming on 10/11/2016.

*/

public class Client {

public static void main(String[] args) {

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:motan-client.xml");

HelloService fooService = (HelloService) applicationContext.getBean("helloService");

System.out.println(fooService.hello("world"));

}

}

调用响应结果

fdc6a49526d3?from=singlemessage

使用Consul作为注册中心

在集群环境下使用motan需要依赖Consul等服务发现组件

maven的pom文件配置(在上面的基础上增加consul)

com.weibo

motan-registry-consul

${motan.version}

Server 端

xml配置文件添加consul的注册

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

启动服务的方法要在程序启动后调用心跳开关,将服务注册到consul,不然Client无法调用 (别的和上文Hello World一样不变,运行main方法启动服务)

package com.raventech.user;

import com.weibo.api.motan.common.MotanConstants;

import com.weibo.api.motan.util.MotanSwitcherUtil;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**

* @author liuhaiming on 10/11/2016.

*/

public class Server {

public static void main(String[] args) {

new ClassPathXmlApplicationContext("classpath:motan-server.xml");

MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true);

System.out.println("Server start ...");

}

}

Client 端

xml配置文件添加consul的服务发现

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

别的和上文Hello World一样不变,运行Client类的main方法调用服务

使用注解的方式集成到Spring Boot项目中

项目改成Spring Boot后抛弃了繁琐的xml文件配置改为用注解的方式。motan也支持注解的方式进行配置,这样更加方便了代码的集成和风格的统一。

继续在前面的代码中进行修改,没提到的保持不变

Server 端

删除motan-server.xml配置文件

用注解加载motan需要的配置

package com.raventech.user.config;

import com.weibo.api.motan.config.springsupport.AnnotationBean;

import com.weibo.api.motan.config.springsupport.BasicServiceConfigBean;

import com.weibo.api.motan.config.springsupport.ProtocolConfigBean;

import com.weibo.api.motan.config.springsupport.RegistryConfigBean;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

/**

* @author liuhaiming on 10/11/2016.

*/

@Configuration

public class MotanConfiguration {

@Bean

public AnnotationBean motanAnnotationBean() {

AnnotationBean motanAnnotationBean = new AnnotationBean();

motanAnnotationBean.setPackage("com.raventech.user.motan");

return motanAnnotationBean;

}

@Bean(name = "motan")

public ProtocolConfigBean protocolConfig1() {

ProtocolConfigBean config = new ProtocolConfigBean();

config.setDefault(true);

config.setName("motan");

config.setMaxContentLength(1048576);

return config;

}

@Bean(name = "registry")

public RegistryConfigBean registryConfig() {

RegistryConfigBean config = new RegistryConfigBean();

config.setRegProtocol("consul");

config.setAddress("127.0.0.1:8500");

return config;

}

@Bean

public BasicServiceConfigBean baseServiceConfig() {

BasicServiceConfigBean config = new BasicServiceConfigBean();

config.setExport("motan:8002");

config.setRegistry("registry");

return config;

}

}

暴露接口的实现类加上@MotanService注解,自动生成bean

package com.raventech.user.motan;

import com.weibo.api.motan.config.springsupport.annotation.MotanService;

/**

* @author liuhaiming on 10/11/2016.

*/

@MotanService

public class HelloServiceImpl implements HelloService {

@Override

public String hello(String world) {

return "hello " + world;

}

}

启动服务的方法就是启动Spring Boot项目,并在在程序启动后调用心跳开关 (运行main方法启动服务)

package com.raventech.user;

import com.weibo.api.motan.common.MotanConstants;

import com.weibo.api.motan.util.MotanSwitcherUtil;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

/**

* @author liuhaiming on 10/11/2016.

*/

@SpringBootApplication

public class Server {

public static void main(String[] args) {

SpringApplication.run(Server.class, args);

MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true);

System.out.println("Server start ...");

}

}

Client 端

删除motan-client.xml配置文件和Client.java启动文件,已经没用了

用注解加载motan需要的配置

package com.raventech.web.config;

import com.weibo.api.motan.config.springsupport.AnnotationBean;

import com.weibo.api.motan.config.springsupport.BasicRefererConfigBean;

import com.weibo.api.motan.config.springsupport.ProtocolConfigBean;

import com.weibo.api.motan.config.springsupport.RegistryConfigBean;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

/**

* @author liuhaiming on 10/11/2016.

*/

@Configuration

public class MotanConfiguration {

@Bean

public AnnotationBean motanAnnotationBean() {

AnnotationBean motanAnnotationBean = new AnnotationBean();

# 添加用到motan注解的类的包名

motanAnnotationBean.setPackage("com.raventech.web.controller");

return motanAnnotationBean;

}

@Bean(name = "motan")

public ProtocolConfigBean protocolConfig1() {

ProtocolConfigBean config = new ProtocolConfigBean();

config.setDefault(true);

config.setName("motan");

config.setMaxContentLength(1048576);

return config;

}

@Bean(name = "registry")

public RegistryConfigBean registryConfig() {

RegistryConfigBean config = new RegistryConfigBean();

config.setRegProtocol("consul");

config.setAddress("127.0.0.1:8500");

return config;

}

@Bean(name = "basicRefererConfig")

public BasicRefererConfigBean basicRefererConfigBean() {

BasicRefererConfigBean config = new BasicRefererConfigBean();

config.setProtocol("motan");

config.setRegistry("registry");

config.setThrowException(true);

return config;

}

}

调用方法(在Controller中使用)

package com.raventech.web.controller;

import com.raventech.user.motan.HelloService;

import com.raventech.web.common.BaseController;

import com.weibo.api.motan.config.springsupport.annotation.MotanReferer;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RestController;

/**

* @author liuhaiming on 10/11/2016.

*/

@RestController

@RequestMapping("/motan")

public class HelloController extends BaseController {

@MotanReferer(basicReferer = "basicRefererConfig")

private HelloService helloService;

@RequestMapping(value = "/hello", method = RequestMethod.GET)

public String hello() throws Exception {

return helloService.hello("world");

}

}


http://chatgpt.dhexx.cn/article/FNG4bVwX.shtml

相关文章

轻量级Rpc框架设计--motan源码解析一:框架介绍及框架使用初体验

一, 框架介绍 1.1 概况 motan是新浪微博开源出来的一套高性能、易于使用的分布式远程服务调用(RPC)框架。 1.2 功能 可以spring的配置方式与项目集成. 支持zookeeper服务发现组件, 实现集群环境下服务注册与发现. 保证高并发, 高负载场景下的稳定高性能, , 实现生产环境…

Motan原理、使用、JavaAPI简化、为什么使用Motan

前言,本文包括,rpc解释与为什么使用rpc、rpc性能对比、Motan依赖问题、Motan源码梳理、Motan功能、特点、使用。 主要中心:为什么使用Motan? 一、什么是RPC 官方解释:RPC(Remote Procedure Call)—远程…

jplayer自动播放

音乐网站的播放器一直都没有解决自动播放的问题,小哲说这样不行的,我也知道不可以这样,毕竟是自己提出要做的,所以要尽自己最大的能力去做好它!本周末我一直都在围绕这个问题而研究。 我曾经想过在播放器初始化的时候…

JWPlayer

原文: http://www.cnblogs.com/yukui/archive/2009/03/12/1409469.html The JW MP3 Player (built with Adobes Flash) is the easiest way to add live music or podcasts to your website. It supports playback of a single MP3 file or an RSS, XSPF or ASX pla…

今天开始写些随笔,就从Jplayer开始吧

今天才开始用Jplayer,可能有点落伍了,但是看到网上千篇一律的使用说明,开始决定把自己的使用心得分享一下,废话不多说,开始吧。 Step1: 官网上有具体的搭建顺序,URL:http://www.jp…

关于播放器JPlayer的使用及遇到的问题

jPlayer是一个用于控制和播放mp3文件的jQuery插件。它在后台使用Flash来播放mp3文件,前台播放器外观完全可以使用XHML/CSS自定义。支持: 有一点比较好的是,在支持html5的浏览器上会使用html5的标签audio或者video,而不支持的浏览…

ijkplayer支持播放rtsp、jpeg、gif

ijkplayer版本:k.0.8.8 编译环境:Ubuntu 18.04.6 LTS 使用平台:android 支持rtsp播放 默认的ijkplayer并不支持rtsp流的播放,因为在编译ffmpeg的时候并没有开启rtsp的demuxer,所以在编译ffmpeg的时候需要开启rtsp的d…

【ijkplayer】介绍

【ijkplayer】介绍 0x1 系统架构 ijkplayer是由b站开源的播放器项目,底层基于ffmpeg, 支持Android和iOS。下面我们来简单介绍一下Android上的实现。 Android上的系统架构图如下。 下面分别对各个模块进行介绍: 0x11 ijkplayer-example app的实现&a…

一款简洁的 jplayer 音乐播放器完整版

一款简洁 jplayer 音乐播放器&#xff0c;做音乐站很漂亮&#xff0c;直接套用就好了。 效果图&#xff1a; 部分源代码&#xff1a; <div id"lei_jplayer"></div> <div id"jp_container_1"><div class"jp-controls">…

Ijkplayer Android介绍

Ijkplayer Android目录结构 Ijkplayer Android可以将该工程导入android studio进行编译&#xff0c;当它下载好需要的资源文件&#xff08;例如sdk build tool&#xff0c;gradle等&#xff09;就可以进行编译了&#xff0c;该工程的目录结构如下图所示: 链接库&#xff1a…

ijkplayer播放器

播放器系列 android播放器&#xff1a;MediaPlayer ExoPlayer ijkplayer_步基的博客-CSDN博客_mediacodec流程 一 概述 IJKPlayer是一款基于ffmpeg/ffplay的开源播放器&#xff0c;可支持rtmp/rtsp/hls等多种媒体协议&#xff0c;支持Android/IOS等移动平台。项目地址&#xff…

jplayer详解

下载官网&#xff1a;http://www.jplayer.org/ 当前版本&#xff1a;2.3.0 功能&#xff1a;视频播放&#xff08;可全屏&#xff09;、音乐播放 全部原教程&#xff0c;说明并不详细&#xff0c;要结合查看其网页源代码来学习&#xff1a;http://www.jplayer.org/latest/demos…

Javplayer 整合插件版下载使用

Javplayer - 马赛克去除工具 工作中去除图片模糊像素&#xff0c;还原高清图片Javplayer - 马赛克去除工具简介版本安装使用调整示例注意事项体验视频小结咨询 工作中去除图片模糊像素&#xff0c;还原高清图片 Javplayer - 马赛克去除工具简介 JavPlayer是一种视频播放器&am…

jPlayer使用详解

原文地址&#xff1a;http://blog.csdn.net/z295304557/article/details/17298889?_t_t_t0.39512087353182124 下载官网&#xff1a;http://www.jplayer.org/ 当前版本&#xff1a;2.3.0 功能&#xff1a;视频播放&#xff08;可全屏&#xff09;、音乐播放 全部原教程&#x…

jPlayer插件的使用

jPlayer插件的使用 2017年04月21日 11:03:19 JENIFURS 阅读数&#xff1a;3952 标签&#xff1a; jPlayer视频插件兼容bug 更多 个人分类&#xff1a; jQuery 版权声明&#xff1a;本文为博主原创文章&#xff0c;未经博主允许不得转载。 https://blog.csdn.net/fuyujiaof/arti…

JPlayer播放器插件教程,JPlayer项目实战

JPlayer 播放器&#xff1a;基于HTML5/Flash的音频、视频播放器。项目预览&#xff1a;播放器插件预览jPlayer中文官方文档&#xff1a;官网功能强大&#xff0c;操作简便&#xff0c;支持换肤功能。引入JPlayer文件以及样式&#xff1a;<head><script type"text…

TensorFlow 2019

numpy pandas sklearn 算法 神经网络的Math 动手实现网络结构 应用 TensorFlow框架 图像案例 Machine Learning VS Deep Learning 1 特征提取方面 自动 2 数量集 量大/参数大 * 机器学习 &#xff1a; 朴素贝叶斯 决策树 深度学习 &#xff1a; 统称神经网络 3 应…

机器学习练习 8 -异常检测和推荐系统(协同过滤)

1 Anomaly detection&#xff08;异常检测&#xff09; 我们的第一个任务是使用高斯模型来检测数据集中未标记的示例是否应被视为异常。 我们先从简单的二维数据集开始。 之前我们构建的异常检测系统也使用了带标记的数据&#xff0c;与监督学习有些相似&#xff0c;下面的对…

从普通程序员到AI大神,月薪7W的正确打开方式...

“ 前不久&#xff0c;不少人被一段机器人后空翻的视频刷屏了。视频中&#xff0c;双足人形机器人阿特拉斯又是走“梅花桩”&#xff0c;又是秀后空翻&#xff0c;完成了一段精彩绝伦的体操表演。 超强机器人出现&#xff01;分分钟被 AI 惊呆… 要知道阿特拉斯在 2014 年的时候…

Surface Defect Detection Methods for Industrial Products : A Review

Surface Defect Detection Methods for Industrial Products : A Review &#xff08;工业产品表面缺陷检测方法综述 &#xff09; 文章来源&#xff1a;Appl. Sci.2021,11, 7657. https://doi.org/10.3390/app11167657 &#xff08;Yuanyuan Ding&#xff09; 论文亮点&#x…