Explorar o código

Merge branch 'dev' of http://192.168.1.220:10080/Amoy/patient-co-management into dev

wujunjie %!s(int64=8) %!d(string=hai) anos
pai
achega
8f4c646cf7

+ 20 - 0
patient-co-analysis/pom.xml

@ -352,6 +352,11 @@
            <artifactId>spring-boot-starter-jdbc</artifactId>
            <version>${version.spring-boot}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
            <version>${version.spring-boot}</version>
        </dependency>
        <!--<dependency>-->
        <!--<groupId>org.springframework.boot</groupId>-->
        <!--<artifactId>spring-boot-starter-security</artifactId>-->
@ -408,6 +413,21 @@
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.yihu.wlyy.analysis.Application</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
                <version>${version.spring-boot}</version>
            </plugin>
            <!--打成war包需要的配置-->
            <!--<plugin>-->
                <!--<artifactId>maven-war-plugin</artifactId>-->

+ 5 - 0
patient-co-analysis/src/main/java/com/yihu/wlyy/analysis/config/kafka/KafkaConsumerConfig.java

@ -1,5 +1,6 @@
package com.yihu.wlyy.analysis.config.kafka;
import com.yihu.wlyy.analysis.listener.LabelDataListener;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.springframework.beans.factory.annotation.Value;
@ -59,4 +60,8 @@ public class KafkaConsumerConfig {
        return propsMap;
    }
    @Bean
    public LabelDataListener labelDataListener() {
        return new LabelDataListener();
    }
}

+ 37 - 0
patient-co-analysis/src/main/java/com/yihu/wlyy/analysis/controller/KafKaTest.java

@ -0,0 +1,37 @@
package com.yihu.wlyy.analysis.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * Created by Administrator on 2017/2/6.
 */
@RestController
public class KafKaTest {
    @Autowired
    private KafkaTemplate kafkaTemplate;
    @RequestMapping("/produce")
    public void contextLoads() throws InterruptedException {
        System.out.println("发送消息开始");
        ListenableFuture<SendResult<String, String>> future = kafkaTemplate.send("topicTest", "ABC");
        future.addCallback(new ListenableFutureCallback<SendResult<String, String>>() {
            @Override
            public void onSuccess(SendResult<String, String> result) {
                System.out.println("success");
            }
            @Override
            public void onFailure(Throwable ex) {
                System.out.println("failed");
            }
        });
        System.out.println(Thread.currentThread().getId());
    }
}

+ 26 - 3
patient-co-analysis/src/main/java/com/yihu/wlyy/analysis/listener/LabelDataListener.java

@ -1,34 +1,57 @@
package com.yihu.wlyy.analysis.listener;
import com.yihu.wlyy.analysis.model.LabelDataModel;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoDbUtils;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.SendResult;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;
import java.util.Optional;
/**
 * Created by Administrator on 2017/2/6.
 */
@Component
public class LabelDataListener {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private MongoTemplate mongoTemplate;
    private static String mongoDbTableName = "WLYY_ANALYSIS";
    //@Scheduled(cron = "0 0/1 * * * ?") //每分钟执行一次
    @KafkaListener(topics = "flumeLog1")
    public void labelData(ConsumerRecord<?, ?> record) {
        logger.debug("Kafka开始消费");
        Optional<?> kafkaMessage = Optional.ofNullable(record.value());
        if (kafkaMessage.isPresent()) {
            Object message = kafkaMessage.get();
            try {
                System.out.println(message);
                logger.debug("接受到的消息:" + String.valueOf(message));
                String[] value = String.valueOf(message).split(" - ");
                if (value.length == 5) {
                    mongoTemplate.insert(
                            new LabelDataModel(value[0], value[1], value[2], value[3], value[4]), mongoDbTableName
                    );
                    logger.debug("保存成功,message:" + message);
                } else {
                    logger.error("数据格式错误,message:" + message);
                }
            } catch (Exception e) {
                logger.error("将接收到的消息保存到数据库时异常, 消息:{}, 异常:{}", message.toString(), e);
                logger.error(e.getMessage());
            }
        }
        logger.debug("Kafka结束消费");
    }
//    @Scheduled(fixedRate=20000)//每20秒执行一次。开始
//    public void testTasks() {
//    }

+ 80 - 0
patient-co-analysis/src/main/java/com/yihu/wlyy/analysis/model/LabelDataModel.java

@ -0,0 +1,80 @@
package com.yihu.wlyy.analysis.model;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import java.io.Serializable;
/**
 * Created by Administrator on 2017/2/7.
 * mongodb实体类
 */
@Document
public class LabelDataModel implements Serializable {
    @Id
    private String id;
    private String createTime;//时间
    private String code;//操作人code
    private String classMethod;//完整的类路径
    private String operaTime;//操作时间
    private String date;//具体的数据
    public LabelDataModel() {
    }
    public LabelDataModel(String createTime, String code, String classMethod, String operaTime, String date) {
        this.createTime = createTime;
        this.code = code;
        this.classMethod = classMethod;
        this.operaTime = operaTime;
        this.date = date;
    }
    public String getDate() {
        return date;
    }
    public void setDate(String date) {
        this.date = date;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getCreateTime() {
        return createTime;
    }
    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }
    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
    public String getClassMethod() {
        return classMethod;
    }
    public void setClassMethod(String classMethod) {
        this.classMethod = classMethod;
    }
    public String getOperaTime() {
        return operaTime;
    }
    public void setOperaTime(String operaTime) {
        this.operaTime = operaTime;
    }
}

+ 13 - 1
patient-co-analysis/src/main/resources/application.yml

@ -1,5 +1,5 @@
server:
  port: 8080
  port: 18080
spring:
@ -44,6 +44,15 @@ spring:
    username: root
    password: 123456
  data:
    mongodb:
      host: 172.19.103.57
      port: 27017
      username: esb
      password: esb
      authenticationDatabase: admin
      database: wlyy
kafka:
  broker:
    address: 172.17.110.201:9092
@ -51,6 +60,9 @@ kafka:
zookeeper:
  broker:
    address: 172.17.110.201:2181
---
spring:
  profiles: prod

+ 1 - 1
patient-co-wlyy/src/main/java/com/yihu/wlyy/logs/InterfaceCallLogs.java

@ -18,7 +18,7 @@ public class InterfaceCallLogs {
     */
    public static void info(long time, String url, String user, String info) {
        try {
            logger.info(user + " - " + url + " - " + time + "ms - " + info.toString());
            logger.info(user + " - " + url + " - " + time + " - " + info.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }