前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Motan源码阅读--调用示例

Motan源码阅读--调用示例

作者头像
春哥大魔王
发布2018-09-21 11:17:01
5880
发布2018-09-21 11:17:01
举报

调用示例

同步调用

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>

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

src/main/java/quickstart/FooService.javapackage quickstart;public interface FooService {    public String hello(String name);
}

编写业务接口逻辑,创建并启动RPC Server

src/main/java/quickstart/FooServiceImpl.javapackage 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">

    <!-- 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.javapackage 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端口。

创建并执行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">

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

执行Client类中main函数将执行一次远程调用,并输出结果。

异步调用

异步调用和同步调用基本配置一样,只需要在接口类中加@MotanAsync注解,然后Client端稍作修改,server端不需要做任何修改。

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

编译时,Motan会自动生成异步service类,生成的类名为service名加上Async。 在client配置motan-client.xml时,在同步调用配置的基础上,只需要修改refer的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 call
    System.out.println(service.hello("motan"));    // async call
    ResponseFuture future = service.helloAsync("motan async ");
    System.out.println(future.getValue());    // multi call
    ResponseFuture future1 = service.helloAsync("motan async multi-1");
    ResponseFuture future2 = service.helloAsync("motan async multi-2");
    System.out.println(future1.getValue() + ", " + future2.getValue());    // async with listener
    FutureListener listener = new FutureListener() {
        @Override        public 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);
}

集群调用示例

在分布式环境下,motan的使用需要依赖于外部服务发现组件,目前支持consul或zk。

使用Consul作为注册中心

server和client中添加moan-registry-consul依赖

<dependency>
    <groupId>com.weibo</groupId>
    <artifactId>motan-registry-consul</artifactId>
    <version>RELEASE</version></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)

zookeeper的实现和Consul类似。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-08-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 春哥talk 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 调用示例
    • 同步调用
      • 异步调用
      • 集群调用示例
        • 使用Consul作为注册中心
        相关产品与服务
        微服务引擎 TSE
        微服务引擎(Tencent Cloud Service Engine)提供开箱即用的云上全场景微服务解决方案。支持开源增强的云原生注册配置中心(Zookeeper、Nacos 和 Apollo),北极星网格(腾讯自研并开源的 PolarisMesh)、云原生 API 网关(Kong)以及微服务应用托管的弹性微服务平台。微服务引擎完全兼容开源版本的使用方式,在功能、可用性和可运维性等多个方面进行增强。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档