我的项目没有用到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; } }
网友评论