目录
一.创建一个maven项目:searchservice22
二.配置文件(src/main/resources)
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jinghang</groupId>
<artifactId>searchservice22</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>searchservice22</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.7</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<exclusions>
<exclusion>
<artifactId>jmxri</artifactId>
<groupId>com.sun.jmx</groupId>
</exclusion>
<exclusion>
<artifactId>jmxtools</artifactId>
<groupId>com.sun.jdmk</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.11.0.GA</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver</artifactId>
<version>3.11.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>1.7.1</version>
</dependency>
<dependency>
<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>1.8</version>
<scope>system</scope>
<systemPath>C:\Program Files\Java\jdk1.8.0_192\lib\tools.jar</systemPath>
</dependency>
<!--
<dependency>
<groupId>com.appsproject</groupId>
<artifactId>appsproject</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
-->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
application-context-consumer.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:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="demo-consumer"/>
<dubbo:registry address="zookeeper://192.168.56.121:2181" />
<dubbo:reference id="helloT" interface="com.searchservice22.test.service.HelloT" version="1.0"/>
<dubbo:reference id="errorAnalyDao" interface="com.searchservice22.youmeng.dao.ErrorAnalyDao" version="1.0"/>
<!-- <dubbo:reference id="errorAnalyService" interface="com.searchservice.youmeng.service.ErrorAnalyService" version="1.0"/>
-->
</beans>
<?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:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:protocol name="dubbo" port="20880"></dubbo:protocol>
<dubbo:application name="dubbo-service-test"/>
<dubbo:registry address="zookeeper://192.168.56.121:2181" />
<dubbo:service interface="com.searchservice22.test.service.HelloT" ref="helloImpl" version="1.0" />
<bean id="helloImpl" class="com.searchservice22.test.service.impl.HelloImpl"/>
<dubbo:service interface="com.searchservice22.youmeng.dao.ErrorAnalyDao" ref="errorAnalyDao" timeout="15000" version="1.0" />
<bean id="errorAnalyDao" class="com.searchservice22.youmeng.dao.impl.ErrorAnalyDaoImpl"/>
<!-- <dubbo:service interface="com.searchservice.youmeng.service.ErrorAnalyService" ref="errorAnalyServiceImpl" version="1.0" />
<bean id="errorAnalyServiceImpl" class="com.searchservice.youmeng.service.impl.ErrorAnalyServiceImpl"/>
-->
</beans>
application-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
<!-- 自动扫描 com.searchservice.**.** -->
<context:component-scan base-package="com.searchservice22.**.**" />
</beans>
application.properties:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
<!-- 自动扫描 com.searchservice.**.** -->
<context:component-scan base-package="com.searchservice22.**.**" />
</beans>
三.测试和读取友盟数据
com.searchservice22.test
Consumer.java
package com.searchservice22.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.searchservice22.test.service.HelloT;
//https://blog.csdn.net/young_0609/article/details/100548813
public class Consumer {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"application-context-consumer.xml"});
context.start();
// Object bean = context.getBean("helloT");
// obtain proxy object for remote invocation
HelloT demoService = (HelloT) context.getBean("helloT");
// execute remote invocation
String hello = demoService.sayhello("laozhang");
// show the result
System.out.println(hello);
}
}
Provide.java
package com.searchservice22.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Provide {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"application-context-provide.xml"});
context.start();
// press any key to exit
System.in.read();
}
}
com.searchservice22.test.service.impl
HelloImpl.java
package com.searchservice22.test.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.searchservice22.test.service.HelloT;
@Service
public class HelloImpl implements HelloT {
@Override
public String sayhello(String name) {
return "哈哈===== "+name+" 进来了";
}
}
HelloT.java
package com.searchservice22.test.service;
public interface HelloT {
public String sayhello(String name);
}
启动zookeeper和mongodb,运行。web.xml记得把之前的maven里的也放进去呦。
com.searchservice22.youmeng
BaseMongo.java
package com.searchservice22.youmeng.base;
import java.util.ArrayList;
import java.util.List;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;
import com.searchservice22.youmeng.util.PropertityUtils;
public class BaseMongo {
protected static MongoClient mongoClient ;
static{
List<ServerAddress> addresses = new ArrayList<ServerAddress>();
String[] addressList = PropertityUtils.getValue("mongodbAddr").split(",");
String[] portList = PropertityUtils.getValue("mongodbPort").split(",");
for (int i = 0; i < addressList.length; i++) {
ServerAddress address = new ServerAddress(addressList[i], Integer.parseInt(portList[i]));
addresses.add(address);
}
mongoClient = new MongoClient(addresses);
}
}
ErrorAnalyDaoImpl.java
package com.searchservice22.youmeng.dao.impl;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.bson.Document;
import com.google.gson.Gson;
import com.mongodb.client.AggregateIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Sorts;
import com.searchservice22.youmeng.base.BaseMongo;
import com.searchservice22.youmeng.dao.ErrorAnalyDao;
import com.searchservice22.youmeng.entry.AppErrorAnaly;
import com.searchservice22.youmeng.util.DateTimeTools;
public class ErrorAnalyDaoImpl extends BaseMongo implements ErrorAnalyDao {
public List<AppErrorAnaly> listErrorInfoBy(String appId, String timeFrom, String timeTo, String timeUnit,
String appVersion, String appChannel, String appPlatform, String userGroup, String errorId,
List<String> groupByConditions) {
List<AppErrorAnaly> result = new ArrayList<AppErrorAnaly>();
if (appId == null || appId.trim().isEmpty()) {
return result;
}
MongoDatabase db = mongoClient.getDatabase(appId);
MongoCollection<Document> collection = null;
if(timeUnit == null || timeUnit.trim().isEmpty()){
timeUnit = "daily";
}
if("daily".equals(timeUnit)){
collection = db.getCollection("ErrorInfoDaily");
}else{
return result;
}
Long timeNow = System.currentTimeMillis();
if(timeFrom==null || timeFrom.trim().isEmpty()) {
timeFrom = DateTimeTools.getDateStrBy(timeNow - (long)7*24*3600*1000);
}
if(timeTo==null || timeTo.trim().isEmpty()) {
timeTo = DateTimeTools.getDateStrBy(timeNow);
}
Document matchFields = new Document();
matchFields.put("timeValue", new Document().append("$gte", timeFrom).append("$lte", timeTo));
if (appVersion != null && !appVersion.trim().isEmpty()) {
matchFields.put("appVersion", new Document().append("$eq", appVersion));
}
if (appChannel != null && !appChannel.trim().isEmpty()) {
matchFields.put("appChannel", new Document().append("$eq", appChannel));
}
if (appPlatform != null && !appPlatform.trim().isEmpty()) {
matchFields.put("appPlatform", new Document().append("$eq", appPlatform));
}
if (errorId != null && !errorId.trim().isEmpty()) {
matchFields.put("errorId", new Document().append("$eq", errorId));
}
Document match = new Document("$match", matchFields);
Document sort = new Document("$sort", Sorts.ascending("timeValue"));
Document groupFields = new Document();
Document idFields = new Document();
idFields.put("errorId", "$errorId");
idFields.put("timeValue", "$timeValue");
idFields.put("appId", "$appId");
if (appVersion != null && !appVersion.trim().isEmpty()) {
idFields.put("appVersion", "$appVersion");
}
if (appChannel != null && !appChannel.trim().isEmpty()) {
idFields.put("appChannel", "$appChannel");
}
if (appPlatform != null && !appPlatform.trim().isEmpty()) {
idFields.put("appPlatform", "$appPlatform");
}
if(groupByConditions != null && groupByConditions.size() >0){
for(int i=0;i<groupByConditions.size();i++){
String groupByConditon = groupByConditions.get(i);
if ("appVersion".equals(groupByConditon)) {
idFields.put("appVersion", "$appVersion");
}
if ("appChannel".equals(groupByConditon)) {
idFields.put("appChannel", "$appChannel");
}
if ("appPlatform".equals(groupByConditon)) {
idFields.put("appPlatform", "$appPlatform");
}
}
}
idFields.put("deviceStyle", "$deviceStyle");
idFields.put("osType", "$osType");
groupFields.put("_id", idFields);
groupFields.put("errorCnt", new Document("$sum", "$errorCnt"));
Document group = new Document("$group", groupFields);
Document projectFields = new Document();
projectFields.put("_id", false);
projectFields.put("errorId", "$_id.errorId");
projectFields.put("timeValue", "$_id.timeValue");
projectFields.put("appId", "$_id.appId");
if (appVersion != null && !appVersion.trim().isEmpty()) {
projectFields.put("appVersion", "$_id.appVersion");
}
if (appChannel != null && !appChannel.trim().isEmpty()) {
projectFields.put("appChannel", "$_id.appChannel");
}
if (appPlatform != null && !appPlatform.trim().isEmpty()) {
projectFields.put("appPlatform", "$_id.appPlatform");
}
if(groupByConditions != null && groupByConditions.size() >0){
for(int i=0;i<groupByConditions.size();i++){
String groupByConditon = groupByConditions.get(i);
if ("appVersion".equals(groupByConditon)) {
projectFields.put("appVersion", "$_id.appVersion");
}
if ("appChannel".equals(groupByConditon)) {
projectFields.put("appChannel", "$_id.appChannel");
}
if ("appPlatform".equals(groupByConditon)) {
projectFields.put("appPlatform", "$_id.appPlatform");
}
}
}
projectFields.put("osType", "$_id.osType");
projectFields.put("deviceStyle", "$_id.deviceStyle");
projectFields.put("errorCnt", true);
Document project = new Document("$project", projectFields);
AggregateIterable<Document> iterater = collection.aggregate(
(List<Document>) Arrays.asList(match, sort, group, project)
);
MongoCursor<Document> cursor = iterater.iterator();
Gson gson = new Gson();
while(cursor.hasNext()){
Document document = cursor.next();
String jsonString = gson.toJson(document);
AppErrorAnaly errorInfo = gson.fromJson(jsonString, AppErrorAnaly.class);
result.add(errorInfo);
}
return result;
}
public static void main(String[] args) {
List<AppErrorAnaly> list = new ErrorAnalyDaoImpl().listErrorInfoBy("youmeng", "200914", "200916", null, null, null, null, null, null, null);
System.out.println(list);
}
}
ErrorAnalyDao.java
package com.searchservice22.youmeng.dao;
import java.util.List;
import com.alibaba.dubbo.config.annotation.Service;
import com.searchservice22.youmeng.entry.AppErrorAnaly;
@Service
public interface ErrorAnalyDao {
//抽象方法 10个参数 在mongodb 里面查询数据 结果放入集合
public List<AppErrorAnaly> listErrorInfoBy(
String appId,
String timeFrom,
String timeTo,
String timeUnit,
String appVersion,
String appChannel,
String appPlatform,
String userGroup,
String errorId,
List<String> groupByConditions
);
}
AppErrorAnaly.java
package com.searchservice22.youmeng.entry;
import java.io.Serializable;
public class AppErrorAnaly implements Serializable{
private static final long serialVersionUID = 1L;
private String timeValue;
private String appId;
private String appVersion;
private String appChannel;
private String appPlatform;
private String deviceStyle;
private String osType;
private Long errorCnt;
private String errorId;
public String getTimeValue() {
return timeValue;
}
public void setTimeValue(String timeValue) {
this.timeValue = timeValue;
}
public String getAppId() {
return appId;
}
public void setAppId(String appId) {
this.appId = appId;
}
public String getAppVersion() {
return appVersion;
}
public void setAppVersion(String appVersion) {
this.appVersion = appVersion;
}
public String getAppChannel() {
return appChannel;
}
public void setAppChannel(String appChannel) {
this.appChannel = appChannel;
}
public String getAppPlatform() {
return appPlatform;
}
public void setAppPlatform(String appPlatform) {
this.appPlatform = appPlatform;
}
public String getDeviceStyle() {
return deviceStyle;
}
public void setDeviceStyle(String deviceStyle) {
this.deviceStyle = deviceStyle;
}
public String getOsType() {
return osType;
}
public void setOsType(String osType) {
this.osType = osType;
}
public Long getErrorCnt() {
return errorCnt;
}
public void setErrorCnt(Long errorCnt) {
this.errorCnt = errorCnt;
}
public String getErrorId() {
return errorId;
}
public void setErrorId(String errorId) {
this.errorId = errorId;
}
}
DateTimeTools.java
package com.searchservice22.youmeng.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateTimeTools {
public static String getDateStrBy(Long timeMs) {
SimpleDateFormat sd = new SimpleDateFormat("yyyyMMdd");
return sd.format(new Date(timeMs));
}
public static String tranferDateStrBy(String time,String dataformatbefor,String dataformatafter) throws ParseException {
SimpleDateFormat sd = new SimpleDateFormat(dataformatbefor);
Date date = sd.parse(time);
sd = new SimpleDateFormat(dataformatafter);
String resulttime = sd.format(date);
return resulttime;
}
public static Long getThisDayStartedAtMs(Long timeMs) {
Calendar cal =Calendar.getInstance();
if (null != timeMs) {
cal.setTimeInMillis(timeMs);
}
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTimeInMillis();
}
}
PropertityUtils.java
package com.searchservice22.youmeng.util;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
public class PropertityUtils {
public final static Config config = ConfigFactory.load();
public static String getValue(String key){
String value = config.getString(key);
return value;
}
//测试
// public static void main(String[] args) {
// PropertityUtils propertityUtils = new PropertityUtils();
// String value = propertityUtils.getValue("brokerList");
// System.out.println(value);
// }
}
Consumer.java
package com.searchservice22.youmeng;
import java.util.List;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.searchservice22.youmeng.dao.ErrorAnalyDao;
import com.searchservice22.youmeng.entry.AppErrorAnaly;
public class Consumer {
public static void main(String[] args) {
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext(new String[]{"application-context-consumer.xml"});
context.start();
ErrorAnalyDao errorAnaly = (ErrorAnalyDao) context.getBean("errorAnalyDao");
// ErrorAnalyService errorAnaly = (ErrorAnalyService) context.getBean("errorAnalyService");
List<AppErrorAnaly> list = errorAnaly.listErrorInfoBy("youmeng", "200914", "200916", null, null, null, null, null, null, null);
System.out.println(list);
}
}
Provide.java
package com.searchservice22.youmeng;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Provide {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"application-context-provide.xml"});
context.start();
// press any key to exit
System.in.read();
}
}
运行: