module配置如下:
/**
* 主模块
*/
@Modules(scanPackage = true, packages = "sz.xwt.logapi")
@Ok("json:full")
@Fail("http:500")
@IocBy(type = ComboIocProvider.class, args = {"*json", "config/ioc/", "*anno", "sz.xwt.logapi",
"*tx", "*quartz", "*async","*zbus", "sz.xwt.logapi"})
@Encoding(input = "UTF-8", output = "UTF-8")
@Views({BeetlViewMaker.class, PdfViewMaker.class})
@SetupBy(value = Setup.class)
@ChainBy(args = "config/chain/nutz-mvc-chain.json")
public class Module {
}
setup配置如下:
Ioc ioc = config.getIoc();
PropertiesProxy conf = ioc.get(PropertiesProxy.class, "conf");
// 获取NutQuartzCronJobFactory从而触发计划任务的初始化与启动
ioc.get(NutQuartzCronJobFactory.class);
// 启动 生产者/消费者(即MQ服务),按需选用
ZBusFactory zbus = ioc.get(ZBusFactory.class, "zbus");
// 启动内置zbus服务器,通常不需要!!! 尤其是新版zbus支持jvm模式后, 这个选项很少使用了.
if (conf.getBoolean("zbus.server.embed.enable", false)) {
ioc.get(MqServer.class);
}
// 启动 生产者/消费者(即MQ服务), 若不需要切勿调用.
zbus.init(getClass().getPackage().getName());
zbus配置文件:
## \u5171\u7528\u914D\u7F6E(mq,rpc\u5747\u9700\u8981!!)
zbus.serverAddr=127.0.0.1:15555
zbus.mq.name=logclic
## \u5185\u5D4Czbus\u670D\u52A1\u5668\u7AEF\u7684\u914D\u7F6E
## \u751F\u4EA7\u73AF\u5883\u4E0B\u8BF7\u4F7F\u7528\u72EC\u7ACB\u7684zbus\u6CE8\u518C\u670D\u52A1!!!
zbus.server.embed.enable=false
zbus.serverPort=15555
zbus.serverHost=127.0.0.1
#zbus.trackServerList=
#zbus.thriftServer=
zbus.selectorCount=1
zbus.executorCount=64
zbus.verbose=false
zbus.storePath=mq
生产者:
@IocBean(create="init")
@At("/api/v1")
@Ok("json")
@Fail("http:500")
@Filters
@Async
public class AccessLogApiController extends Controller{
private static final Log log = Logs.get();
@Inject("java:$zbus.getProducer('accesslog')")
public ZBusProducer accesslogMq;
@At("/addDo")
@POST
public void sendMessage(@Param("alog") String alog) throws Exception {
if(StringUtils.isNotBlank(alog)){
log.info(alog);
Message msg = new Message();
msg.setBody(alog);
accesslogMq.async(msg);
}
}
}
消费者:
@IocBean
@ZBusConsumer(mq = "accesslog") // 声明自身为消费者
public class AccessLogMqConsumer implements ConsumerHandler{
private static final Log log = Logs.get();
@Inject
private AccessLogService accessLogService;
// 处理mq输入
public void handle(Message msg, Consumer consumer) throws IOException {
String alog = msg.getBodyString();
try{
AccessLog al = JSONObject.parseObject(alog, AccessLog.class);
if(al != null && StringUtils.isNotBlank(al.getAppUser())){
accessLogService.insert(al);
}
}catch(Exception e){
log.error(e);
}
}
}
日志:
2017-02-15 10:22:13,728 io.netty.util.internal.logging.Slf4JLogger.debug(Slf4JLogger.java:71) DEBUG - -Dio.netty.initialSeedUniquifier: 0xbb41f5038523824f (took 1 ms)
2017-02-15 10:22:13,807 io.netty.util.internal.logging.Slf4JLogger.debug(Slf4JLogger.java:76) DEBUG - -Dio.netty.allocator.type: unpooled
2017-02-15 10:22:13,807 io.netty.util.internal.logging.Slf4JLogger.debug(Slf4JLogger.java:76) DEBUG - -Dio.netty.threadLocalDirectBufferSize: 65536
2017-02-15 10:22:13,807 io.netty.util.internal.logging.Slf4JLogger.debug(Slf4JLogger.java:76) DEBUG - -Dio.netty.maxThreadLocalCharBufferSize: 16384
2017-02-15 10:22:13,833 io.netty.util.internal.logging.Slf4JLogger.debug(Slf4JLogger.java:81) DEBUG - -Dio.netty.buffer.bytebuf.checkAccessible: true
2017-02-15 10:22:13,837 io.netty.util.internal.logging.Slf4JLogger.debug(Slf4JLogger.java:81) DEBUG - -Dio.netty.leakDetection.level: simple
2017-02-15 10:22:13,838 io.netty.util.internal.logging.Slf4JLogger.debug(Slf4JLogger.java:81) DEBUG - -Dio.netty.leakDetection.maxRecords: 4
2017-02-15 10:22:13,911 org.zbus.net.tcp.TcpClient$1.onConnected(TcpClient.java:81) INFO - Connection(127.0.0.1:15555) OK
2017-02-15 10:22:13,915 io.netty.util.internal.logging.Slf4JLogger.debug(Slf4JLogger.java:76) DEBUG - -Dio.netty.recycler.maxCapacity.default: 262144
2017-02-15 10:22:13,930 io.netty.util.internal.logging.Slf4JLogger.debug(Slf4JLogger.java:76) DEBUG - java.nio.ByteBuffer.cleaner(): available
2017-02-15 10:22:13,943 org.zbus.net.tcp.TcpClient.onSessionMessage(TcpClient.java:398) WARN - !!!!!!!!!!!!!!!!!!!!!!!!!!Drop,HTTP/1.1 404 Not Found
connection: Keep-Alive
content-length: 23
id: f1f1b287-9158-449a-b2cd-6087c3fa5da6
mq: accesslog
MQ(accesslog) Not Found
在chrome浏览器
http://127.0.0.1:15555/
也检测不到mq
SOS