赞
踩
这周开始就要优化自己主导的商城项目了,不得不说因为前期项目周期短,时间紧张,很多框架上的设计做的很粗糙啊。今天开始改造一下系统中的日志存储,日志这种边缘化的东西,和商城业务分开放是很好的做法,不然或多或少会影响mysql的主业务数据存储。
我这里下载的最新版,本人风格就是喜新厌旧,况且技术本来就是新的更刺激。
mongodb下载地址:http://downloads.mongodb.org/linux/mongodb-linux-x86_64-4.0.5.tgz
linux解压命令:
tar -zxvf tar -zxvf mongodb-linux-x86_64-4.0.5.tgz
在解压后的mongodb-linux-x86_64-4.0.5目录下创建两个文件夹conf、data和log:
- mkdir conf
- mkdir data
- mkdir log
在conf中创建一个启动用的配置文件mongod.yml,并且打开密码验证
- systemLog:
- destination: file
- path: "../log/mongod.log"
- logAppend: true
- security:
- authorization: enabled
- storage:
- dbPath: "../data"
- journal:
- enabled: true
- processManagement:
- fork: true
- net:
- bindIp: 172.26.175.201
- port: 27017
启动mongodb数据库
./mongod --config ../conf/mongod.yml
这里我推荐的Studio 3T,很好用的mongodb可是化工具。
下载地址:https://download.studio3t.com/studio-3t/windows/2018.6.1/studio-3t-x64.zip
连接配置如下:
创建新的数据库wmshop,创建一个表shop_wm_log,按照提示无脑下一步就可以了
设置用户,并且设置角色为root,这里要在admin库上设置
重新连接数据库,需要输入密码连接了
OK了,基本设置已经完成。
最新版本的spring-data-mongdb需要jdk1.8以及spring5.1.3以上的版本,所以如果这两个依赖低于此版本则需要更新。
添加依赖:
- <spring.framework.version>5.1.3.RELEASE</spring.framework.version>
- <!-- other dependency elements omitted -->
- <dependency>
- <groupId>org.springframework.data</groupId>
- <artifactId>spring-data-mongodb</artifactId>
- <version>2.1.3.RELEASE</version>
- </dependency>
mongodb配置文件:
- mongodb.host=${mongodb.host}
- mongodb.username=${mongodb.username}
- mongodb.password=${mongodb.password}
- mongodb.database=${mongodb.database}
- mongodb.port=${mongodb.port}
- mongodb.cph=${mongodb.cph}
- mongodb.tatbfcm=${mongodb.tatbfcm}
- mongodb.mwt=${mongodb.mwt}
- mongodb.ska=${mongodb.ska}
- mongodb.st=${mongodb.st}
- mongodb.dbname=${mongodb.dbname}
- <?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:mongo="http://www.springframework.org/schema/data/mongo"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd">
- <mongo:mongo-client id="mongoClient" host="${mongodb.host}" port="${mongodb.port}"
- credentials="${mongodb.username}:${mongodb.password}@${mongodb.database}">
- <mongo:client-options connections-per-host="${mongodb.cph}"
- threads-allowed-to-block-for-connection-multiplier="${mongodb.tatbfcm}"
- connect-timeout="${mongodb.ct}"
- max-wait-time="${mongodb.mwt}"
- socket-keep-alive="${mongodb.ska}"
- socket-timeout="${mongodb.st}"/>
- </mongo:mongo-client>
- <mongo:db-factory dbname="${mongodb.dbname}" mongo-ref="mongoClient"/>
-
- <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
- <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
- </bean>
-
- <mongo:mapping-converter id="converter" />
- </beans>
mongodb model类:
- @Document(collection = "shop_wm_log")
- @Data
- @ToString
- public class WMLog extends BaseEntity implements Serializable {
-
- /**
- * 日志编号
- */
- @Id
- private String wlId;
-
- /**
- * 接口请求地址
- */
- private String wlRequestUrl;
-
- /**
- * 接口请求参数
- */
- private Map<String,Object> wlRequestParameters;
-
- /**
- * 接口响应
- */
- private Map<String,Object> wlResponse;
-
- /**
- * 备注
- */
- private String remarks;
-
- public void setWlRequestUrl(String wlRequestUrl){
- this.wlRequestUrl = wlRequestUrl;
- go(wlRequestUrl);
- }
-
- public void setWlRequestParameters(Map<String,Object> wlRequestParameters){
- this.wlRequestParameters=wlRequestParameters;
- go(wlRequestParameters);
- }
-
- /**
- * 位remarks赋值,主要加上动作key
- * @param args
- */
- private void go(Object args){
- if(args == null){
- return ;
- }
- Map<String,Object> result = null;
- if(args instanceof Map){
- result = (Map<String, Object>) args;
- String traceId = (String)result.get("traceId");
- String[] split = traceId.split("-");
- this.remarks = split[1];
- } else if(args instanceof String){
- String parameters = (String)args;
- parameters = parameters.substring(parameters.indexOf("?")+1, parameters.length());
- if(parameters.isEmpty()){
- return ;
- }
- Map<String, String[]> stringMap = RequestUtils.parseQueryString(parameters);
- for(Map.Entry<String,String[]> entry:stringMap.entrySet()){
- String key = entry.getKey();
- String[] values = entry.getValue();
- if("traceId".equals(key)){
- //解析动作
- String value = values[0];
- String[] split = value.split("-");
- String actionKey = split[1];
- this.remarks = actionKey;
- break;
- }
- }
- }
- }
- }
mongodb dao接口:
- public interface WMLogDao {
-
- List<WMLog> findByWMLogId(String wmLogId);
-
- WMLog findById(String wmLogId);
-
- void saveWMLog(WMLog wmLog);
-
- void updateWMLog(WMLog wmLog);
-
- WMLog findByWMLog(WMLog wmLog);
- }
mongodb daoimpl接口:
- @Repository
- public class WMLogDaoImpl implements WMLogDao {
-
- // @Resource
- // private WMLogMapper wmLogMapper;
- @Autowired
- private MongoTemplate mongoTemplate;
-
- @Override
- public List<WMLog> findByWMLogId(String wmLogId) {
- return null;
- }
-
- @Override
- public WMLog findById(String wmLogId) {
- return findById(wmLogId);
- }
-
- @Override
- public void saveWMLog(WMLog wmLog) {
- mongoTemplate.save(wmLog);
- }
-
- @Override
- public void updateWMLog(WMLog wmLog) {
- Query query = new Query();
- query.addCriteria(Criteria.where("_id").is(wmLog.getWlId()));
- Update update = new Update();
- update.set("remarks",wmLog.getRemarks());
- mongoTemplate.updateFirst(query,update,WMLog.class);
- }
-
- @Override
- public WMLog findByWMLog(WMLog wmLog) {
- return null;
- }
- }
业务调用部分(这里是在消息队列监听中调用,可以忽略):
- @Component
- public class WMLogQueueReceiver implements MessageListener {
-
- @Resource
- private WMLogDao wmLogDao;
-
- @Override
- public void onMessage(Message message) {
- ObjectMessage obj = (ObjectMessage) message;
- try {
- if (obj.getObject() instanceof WMLog) {
- WMLog wmLog = ((WMLog) obj.getObject());
- wmLog.setCreateTime(new Date().getTime());
- wmLogDao.saveWMLog(wmLog);
- }
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- }
项目跑起来,然后请求几个接口,再去studio 3t工具中查看数据库,增加了日志,大功告成。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。