springboot集成motan

我的项目没有用到maven  而是用到了一个叫gradle的管理工具

Gradle是一个基于JVM的构建工具,是一款通用灵活的构建工具,支持maven, Ivy仓库,支持传递性依赖管理,而不需要远程仓库或者是pom.xml和ivy.xml配置文件,基于Groovy,build脚本使用Groovy编写。



Motan 的架构及模块设计

  架构设计,分为服务提供方(RPC Server)、服务调用方(RPC Client)、注册中心(Registry)三个角色,Server 向 Registry 注册声明所提供的服务;Client 向 Registry 订阅指定服务,与 Registry 返回的服务列表的 Server 建立连接,进行 RPC 服务调用。Client 通过 Registry 感知 Server 的状态变更。三者的交互关系如下图:

关系图




motan-api

可以看做是RPC Server

这里面,就是对client暴露的接口 ,通俗一点,对外暴露的接口,类似于service



motan-client

这里可以简单的看做是调用端,需要配置config的内容如下,这里采用注解的方式配置的

/**

*@Author lccsetsun

*@Description client端配置信息

*/

@Configuration

public class MotanConfiguration {

/**

    * @Description: 声明Annotation用来指定需要解析的包名

    */

    @Bean

    @ConfigurationProperties(prefix ="motan.annotation")

public AnnotationBean motanAnnotationBean() {

AnnotationBean motanAnnotationBean =new AnnotationBean();

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

        return motanAnnotationBean;

}

/**

    * @Description: 协议配置

    */

    @Bean(name ="motan")

@ConfigurationProperties(prefix ="motan.protocol")

public ProtocolConfigBean protocolConfig() {

ProtocolConfigBean config =new ProtocolConfigBean();

return config;

}

/**

    * @Description: 注册中心配置

    */

    @Bean(name ="registry")

@ConfigurationProperties(prefix ="motan.registry")

public RegistryConfigBean registryConfig() {

RegistryConfigBean config =new RegistryConfigBean();

return config;

}

/**

    * @Description: 服务端配置

    */

    @Bean(name ="basicRefererConfig")

@ConfigurationProperties(prefix ="motan.server")

public BasicRefererConfigBean basicRefererConfigBean() {

BasicRefererConfigBean config =new BasicRefererConfigBean();

return config;

}

}

对应的application.properties 配置文件内容为


#\u05e2\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd

motan.registry.regProtocol=zookeeper

#motan.registry.address=

motan.registry.address=127.0.0.1:2181

motan.registry.connectTimeout=2000

#\u042d\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd

motan.protocol.name=restful

motan.protocol.endpointFactory=netty

#motan.protocol.minWorkerThread=20 #\ufffd\ufffd\u0421\ufffd\ufffd\ufffd\ufffdpool\ufffd\u07f3\ufffd\ufffd\ufffd

#motan.protocol.maxWorkerThread=50 #\ufffd\ufffd\ufffd\ufffd\ufffdpool\ufffd\u07f3\ufffd\ufffd\ufffd

motan.protocol.maxContentLength=1048576

motan.protocol.isDefault=true

#\u05b8\ufffd\ufffd\ufffd\ufffd\u04aa\ufffd\ufffd\ufffd\ufffd\ufffd\u0130\ufffd\ufffd\ufffd

motan.annotation.package=cn.thunderwind.uc.user   

#\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd

motan.server.protocol=motan

motan.server.registry=registry

motan.server.throwException=true

# mongodb

spring.data.mongodb.uri=mongodb://192.168.1.203:27017/log_db

# \ufffd\u02ff\ufffd

server.port=9087



motan-server


server则是具体操作,你要操作什么逻辑,全在这里操作,操作mapper  调用方法等等

驱动配置如下


/**

*@Author lccsetsun

*@Date 2018/2/1

*@Description  motan配置信息

*/

@Configuration

public class MotanConfiguration {

/**

    * @Description: 声明Annotation用来指定需要解析的包名

    */

    @Bean

    @ConfigurationProperties(prefix ="motan.annotation")

public AnnotationBean motanAnnotationBean() {

AnnotationBean motanAnnotationBean =new AnnotationBean();

return motanAnnotationBean;

}

/**

    * @Description: 协议配置

    */

    @Bean(name ="motan")

@ConfigurationProperties(prefix ="motan.protocol")

public ProtocolConfigBean protocolConfig1() {

ProtocolConfigBean config =new ProtocolConfigBean();

return config;

}

/**

    * @Description: 注册中心配置

    */

    @Bean(name ="registry")

@ConfigurationProperties(prefix ="motan.registry")

public RegistryConfigBean registryConfig() {

RegistryConfigBean config =new RegistryConfigBean();

return config;

}

/**

    * @Description: 服务端配置

    */

    @Bean

    @ConfigurationProperties(prefix ="motan.server")

public BasicServiceConfigBean baseServiceConfig() {

BasicServiceConfigBean config =new BasicServiceConfigBean();

return config;

}

}

对应的properties配置文件

# \ufffd\u02ff\ufffd

server.port=9996

#AOP

spring.aop.proxy-target-class=true

spring.thymeleaf.cache=false

# \ufffd\ufffd\ufffd\u077f\ufffd

spring.datasource.url=jdbc:mysql://name?characterEncoding=utf8&useSSL=true

spring.datasource.username=name

spring.datasource.password=1123

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

# REDIS (RedisProperties)

spring.redis.database=1

spring.redis.host=192.168.1.203

spring.redis.port=6379

spring.redis.password=

spring.redis.pool.max-active=8

spring.redis.pool.max-wait=-1

spring.redis.pool.max-idle=8

spring.redis.pool.min-idle=0

spring.redis.timeout=0

# \ufffd\ufffd\u05be\ufffd\ufffd\ufffd\ufffd\ufffd\u026b

spring.output.ansi.enabled=DETECT

# mongodb

spring.data.mongodb.uri=mongodb://192.168.1.203:27017/log_db

# mybatis

mybatis.config-locations=classpath:myBatis-config.xml

mybatis.mapper-locations=classpath:mapper/*.xml

## motan

motan.registry.regProtocol=zookeeper

motan.registry.address=127.0.0.1:2181

motan.registry.connectTimeout=2000

##\u042d\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd

motan.protocol.name=restful

motan.protocol.endpointFactory=netty

#motan.protocol.minWorkerThread=20 #\ufffd\ufffd\u0421\ufffd\ufffd\ufffd\ufffdpool\ufffd\u07f3\ufffd\ufffd\ufffd

#motan.protocol.maxWorkerThread=50 #\ufffd\ufffd\ufffd\ufffd\ufffdpool\ufffd\u07f3\ufffd\ufffd\ufffd

motan.protocol.maxContentLength=1048576

motan.protocol.isDefault=true

#motan.protocol.filter=statistic

##\u05b8\ufffd\ufffd\ufffd\ufffd\u04aa\ufffd\ufffd\ufffd\ufffd\ufffd\u0130\ufffd\ufffd\ufffd

motan.annotation.package=cn.thunderwind

##\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd

motan.server.export=motan:8003

motan.server.registry=registry


motan日志记录以及拦截请求

import java.util.HashMap;

import java.util.Map;

/**

* describe:

* MotanService 日志记录

* @author hetong

* @date 2018/01/30

**/

@SpiMeta(name ="motanLogFilter")

@Activation(key = {MotanConstants.NODE_TYPE_SERVICE}, sequence =30)

public class MotanLogFilterimplements Filter {

private final static GtLoggerlogger = GtLoggerFactory.getLogger(MotanLogFilter.class);

@Override

    public Response filter(Caller caller, Request request) {

Long start = System.currentTimeMillis();

Map info =new HashMap<>(7);

info.put("content","MotanService层日志");

info.put("loggerName", request.getInterfaceName());

info.put("method", request.getMethodName());

info.put("attachments", request.getAttachments());

info.put("requestId", request.getRequestId());

Map args =null;

if ( request.getArguments() !=null && request.getArguments().length >0 ) {

args =new HashMap<>(request.getArguments().length);

for (Object arg : request.getArguments()) {

if (arg !=null) {

args.put(arg.getClass().getName(), arg.toString());

}

}

}

info.put("remoteAddress", caller.getUrl().getHost());

info.put("arguments", args);

Response response = caller.call(request);

info.put("takeTime", System.currentTimeMillis() - start);

logger.operate(Operates.desc("MotanService"), JSON.toJSON(info).toString());

Exception exception = response.getException();

if (response ==null || exception !=null) {

info.put("exception", exception.getMessage());

}

logger.operate(Operates.desc("Motan Service"), JSON.toJSONString(info));

return response;

}

}


统一异常处理

@SpiMeta(name = "motanExceptionFilter") @Activation(key = {MotanConstants.NODE_TYPE_SERVICE}, sequence = 40) public class MotanExceptionFilter implements Filter { private final static GtLogger logger = GtLoggerFactory.getLogger(MotanExceptionFilter.class); @Override public Response filter(Caller caller, Request request) { Response response = null; boolean isThrowable = false; DefaultResponse e_resp = null; try { response = caller.call(request); Exception exception = response.getException(); if (response == null || exception != null) { isThrowable = true; e_resp = transExceptionRespWithResp(response, exception.getCause()); } } catch (Exception e) { isThrowable = true; e_resp = transExceptionRespWithReq(request, e.getCause()); } return isThrowable ? e_resp : response; } private DefaultResponse transExceptionRespWithResp(Response response, Throwable exception) { logger.error("MotanService 返回异常", exception); DefaultResponse dResp = new DefaultResponse(); dResp.setAttachments(response.getAttachments()); dResp.setProcessTime(response.getProcessTime()); dResp.setRpcProtocolVersion(response.getRpcProtocolVersion()); dResp.setRequestId(response.getRequestId()); dResp.setValue(new ResponseEntity.Builder().setData(exception) .setMessage("返回异常! error:" + exception.getMessage()) .setStatus(-1) .build()); return dResp; } private DefaultResponse transExceptionRespWithReq(Request response, Throwable exception) { logger.error("MotanService 返回异常", exception); DefaultResponse dResp = new DefaultResponse(); dResp.setAttachments(response.getAttachments()); dResp.setRpcProtocolVersion(response.getRpcProtocolVersion()); dResp.setRequestId(response.getRequestId()); dResp.setValue(new ResponseEntity.Builder().setData(exception) .setMessage("返回异常! error:" + exception.getMessage()) .setStatus(-1) .build()); return dResp; } }

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容