Browse Source

首次提交

huangzhiyong 6 years ago
parent
commit
9384e88964

+ 6 - 0
.gitignore

@ -9,6 +9,12 @@
*.war
*.ear
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

+ 116 - 0
pom.xml

@ -0,0 +1,116 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.yihu.ehr</groupId>
        <artifactId>ehr-cloud-parent</artifactId>
        <version>1.13.1</version>
    </parent>
    <artifactId>svr-eip-logger</artifactId>
    <version>1.13.1</version>
    <packaging>jar</packaging>
    <name>svr-eip-logger</name>
    <description>hos logger</description>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
        </dependency>
        <dependency>
            <groupId>com.yihu.ehr</groupId>
            <artifactId>hos-web-framework</artifactId>
            <version>${hos-version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>hibernate-jpa-2.1-api</artifactId>
                    <groupId>org.hibernate.javax.persistence</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-boot-starter-data-jpa</artifactId>
                    <groupId>org.springframework.boot</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-data-jpa</artifactId>
                    <groupId>org.springframework.data</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-spring-boot</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-jms</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-core</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${version.spring-boot}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>com.yihu.hos.logger.HosLoggerApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

+ 14 - 0
src/main/java/com/yihu/hos/logger/HosLoggerApplication.java

@ -0,0 +1,14 @@
package com.yihu.hos.logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication
public class HosLoggerApplication {
	public static void main(String[] args) {
		SpringApplication.run(HosLoggerApplication.class, args);
	}
}

+ 28 - 0
src/main/java/com/yihu/hos/logger/camel/aggregator/EHRLogAggregator.java

@ -0,0 +1,28 @@
package com.yihu.hos.logger.camel.aggregator;
import ch.qos.logback.classic.spi.ILoggingEvent;
import org.apache.camel.Exchange;
import org.apache.camel.processor.aggregate.AggregationStrategy;
import java.util.ArrayList;
/**
 * Created by l4qiang on 2017-04-14.
 */
public class EHRLogAggregator implements AggregationStrategy {
    @Override
    public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
        Object newBody = newExchange.getIn().getBody();
        ArrayList<Object> list;
        if (oldExchange == null) {
            list = new ArrayList<>();
            list.add(newBody);
            newExchange.getIn().setBody(list);
            return newExchange;
        } else {
            list = oldExchange.getIn().getBody(ArrayList.class);
            list.add(newBody);
            return oldExchange;
        }
    }
}

+ 13 - 0
src/main/java/com/yihu/hos/logger/camel/expression/EHRLogExpression.java

@ -0,0 +1,13 @@
package com.yihu.hos.logger.camel.expression;
import ch.qos.logback.classic.spi.ILoggingEvent;
/**
 * Created by l4qiang on 2017-04-14.
 */
public class EHRLogExpression {
    public String getBreadcrumbId(ILoggingEvent event) {
        String routeId = event.getMDCPropertyMap().get("camel.routeId").substring(0, 2);
        return routeId + "#" + event.getMDCPropertyMap().get("camel.breadcrumbId");
    }
}

+ 89 - 0
src/main/java/com/yihu/hos/logger/camel/processor/EHRLogProcesser.java

@ -0,0 +1,89 @@
package com.yihu.hos.logger.camel.processor;
import ch.qos.logback.classic.spi.ILoggingEvent;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Map;
/**
 * ehr log处理
 *
 * @author Airhead
 * @since 2017/3/30.
 */
public class EHRLogProcesser implements Processor {
    @Override
    public void process(Exchange exchange) throws Exception {
        exchange.getOut().setBody("");
        exchange.getOut().setHeader("empty", true);
        ArrayList arrayList = exchange.getIn().getBody(ArrayList.class);
        if (arrayList.size() != 4) {
            return;
        }
        Object first = arrayList.get(0);
        Object last = arrayList.get(arrayList.size() - 1);
        try {
            ILoggingEvent request = (ILoggingEvent) first;
            ILoggingEvent response = (ILoggingEvent) last;
            Map<String, String> mdcPropertyMap = request.getMDCPropertyMap();
            if (mdcPropertyMap == null || !mdcPropertyMap.get("camel.routeId").equals("api")) {
                return;
            }
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode requestNode = objectMapper.readValue(request.getMessage(), JsonNode.class);
            JsonNode responseNode = objectMapper.readValue(response.getMessage(), JsonNode.class);
            JsonNode inNode = requestNode.with("in");
            JsonNode outNode = responseNode.with("out");
            ObjectNode objectNode = objectMapper.createObjectNode();
            objectNode.put("time", new Timestamp(request.getTimeStamp()).toString());
            objectNode.put("logType", 1);
            objectNode.put("caller", inNode.get("appKey").asText());
            ObjectNode dataNode = objectMapper.createObjectNode();
            dataNode.put("responseTime", response.getTimeStamp() - request.getTimeStamp());
            dataNode.put("url", inNode.get("CamelHttpUrl").asText());
            dataNode.put("responseCode", outNode.get("CamelHttpResponseCode").asText());
            dataNode.put("response", outNode.get("body").asText());
            dataNode.put("api", inNode.get("api").asText());
            dataNode.put("appKey", inNode.get("appKey").asText());
            ObjectNode paramNode = objectMapper.createObjectNode();
            paramNode.put("api", inNode.get("api").asText());
            paramNode.put("appKey", inNode.get("appKey").asText());
            paramNode.put("sign", inNode.get("sign").asText());
            paramNode.put("timestamp", inNode.get("timestamp").asText());
            paramNode.put("v", inNode.get("v").asText());
            if (inNode.has("param")) {
                paramNode.put("param", inNode.get("param").asText());
            }
            if (inNode.has("token")) {
                paramNode.put("token", inNode.get("token").asText());
            }
            dataNode.set("params", paramNode);
            objectNode.set("data", dataNode);
            String content = objectMapper.writeValueAsString(objectNode);
            exchange.getOut().setBody(content);
            exchange.getOut().setHeader("empty", false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

+ 42 - 0
src/main/java/com/yihu/hos/logger/camel/route/EHRLogRouterBuilder.java

@ -0,0 +1,42 @@
package com.yihu.hos.logger.camel.route;
import com.yihu.hos.logger.camel.aggregator.EHRLogAggregator;
import com.yihu.hos.logger.camel.expression.EHRLogExpression;
import com.yihu.hos.logger.camel.processor.EHRLogProcesser;
import com.yihu.hos.logger.configuration.LoggerConfiguration;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jms.JmsComponent;
import org.apache.camel.model.ModelCamelContext;
import org.springframework.beans.factory.annotation.Autowired;
import javax.jms.ConnectionFactory;
/**
 * @author Airhead
 * @since 2017/3/30.
 */
//@Component
public class EHRLogRouterBuilder extends RouteBuilder {
    private static final String DESTINATION_NAME = "business-log-ehr";
    private static final String QUEUE_NAME = "business.log.queue";
    @Autowired
    private LoggerConfiguration loggerConfiguration;
    @Override
    public void configure() throws Exception {
        ModelCamelContext context = this.getContext();
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
                loggerConfiguration.getActiveMqUser(), loggerConfiguration.getActiveMqPassword(), loggerConfiguration.getBrokerURL());
        context.addComponent(DESTINATION_NAME, JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));
        from(DESTINATION_NAME + ":topic:" + QUEUE_NAME)
                .aggregate(method(new EHRLogExpression(), "getBreadcrumbId"), new EHRLogAggregator())
                .completionSize(4)
                .completionTimeout(20 * 1000)
                .process(new EHRLogProcesser())
                .choice()
                .when(header("empty").isNotEqualTo(true)).to("file://" + loggerConfiguration.getEhrLogDir())
                .endChoice();
    }
}

+ 34 - 0
src/main/java/com/yihu/hos/logger/camel/route/HosLogRouteBuilder.java

@ -0,0 +1,34 @@
package com.yihu.hos.logger.camel.route;
import com.yihu.hos.logger.configuration.LoggerConfiguration;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jms.JmsComponent;
import org.apache.camel.model.ModelCamelContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.jms.ConnectionFactory;
/**
 * Created by l4qiang on 2017-04-14.
 */
@Component
public class HosLogRouteBuilder extends RouteBuilder {
    static public final String DESTINATION_NAME = "business-log-hos";
    private static final String QUEUE_NAME = "business.log.queue";
    @Autowired
    private LoggerConfiguration loggerConfiguration;
    @Override
    public void configure() throws Exception {
        ModelCamelContext context = this.getContext();
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
                loggerConfiguration.getActiveMqUser(), loggerConfiguration.getActiveMqPassword(), loggerConfiguration.getBrokerURL());
        // Note we can explicit name the component
        context.addComponent(DESTINATION_NAME, JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));
        from(DESTINATION_NAME + ":topic:" + QUEUE_NAME)
                .to("bean:businessLogService?method=log");
    }
}

+ 45 - 0
src/main/java/com/yihu/hos/logger/configuration/LoggerConfiguration.java

@ -0,0 +1,45 @@
package com.yihu.hos.logger.configuration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
/**
 * @author Airhead
 * @since 2017/1/19.
 */
@Configuration
public class LoggerConfiguration {
    @Value("${spring.activemq.broker-url}")
    private String brokerURL;
    @Value("${spring.activemq.user}")
    private String activeMqUser;
    @Value("${spring.activemq.password}")
    private String activeMqPassword;
    @Value("${logger.ehr}")
    private String ehrLogDir;
    @Value("${logger.tenant}")
    private String tenant;
    public String getBrokerURL() {
        return brokerURL;
    }
    public String getActiveMqUser() {
        return activeMqUser;
    }
    public String getActiveMqPassword() {
        return activeMqPassword;
    }
    public String getEhrLogDir() {
        return ehrLogDir;
    }
    public String getTenant() {
        return tenant;
    }
}

+ 192 - 0
src/main/java/com/yihu/hos/logger/model/HosLog.java

@ -0,0 +1,192 @@
package com.yihu.hos.logger.model;
import com.yihu.hos.core.datatype.DateUtil;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.Date;
/**
 * @created Airhead 2016/8/8.
 */
@Document(collection = "business")
public class HosLog {
    @Id
    private String id;
    @Indexed
    private String exchangeId;
    private String messageId;
    private String correlationId;
    private String transactionKey;
    @Indexed
    private String routeId;
    @Indexed
    private String breadcrumbId;
    private String order;
    private Integer totalServers;
    private String code;
    private String camelContextId;
    private String body;
    private Integer bodyLength;
    private String fireTimeSource;
    private Date updateTime;
    private Date createTime;
    private String tenant;
    public HosLog() {
        this.createTime = DateUtil.getSysDateTime();
    }
    //@Indexed(name = "fireTime_1", expireAfterSeconds = 30)
    @Indexed
    private Date fireTime;  //后期建议转成TTL
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getExchangeId() {
        return exchangeId;
    }
    public void setExchangeId(String exchangeId) {
        this.exchangeId = exchangeId;
    }
    public String messageId() {
        return this.messageId;
    }
    public void setMessageId(String messageId) {
        this.messageId = messageId;
    }
    public String getCorrelationId() {
        return correlationId;
    }
    public void setCorrelationId(String correlationId) {
        this.correlationId = correlationId;
    }
    public String getTransactionKey() {
        return transactionKey;
    }
    public void setTransactionKey(String transactionKey) {
        this.transactionKey = transactionKey;
    }
    public String getRouteId() {
        return routeId;
    }
    public void setRouteId(String routeId) {
        this.routeId = routeId;
    }
    public String getBreadcrumbId() {
        return breadcrumbId;
    }
    public void setBreadcrumbId(String breadcrumbId) {
        this.breadcrumbId = breadcrumbId;
    }
    public String getCamelContextId() {
        return camelContextId;
    }
    public void setCamelContextId(String camelContextId) {
        this.camelContextId = camelContextId;
    }
    public String getBody() {
        return body;
    }
    public void setBody(String body) {
        this.body = body;
    }
    public Date getFireTime() {
        return fireTime;
    }
    public void setFireTime(Date fireTime) {
        this.fireTime = fireTime;
    }
    public Integer getBodyLength() {
        return bodyLength;
    }
    public void setBodyLength(Integer bodyLength) {
        this.bodyLength = bodyLength;
    }
    public String getFireTimeSource() {
        return fireTimeSource;
    }
    public void setFireTimeSource(String fireTimeSource) {
        this.fireTimeSource = fireTimeSource;
    }
    public Integer getTotalServers() {
        return totalServers;
    }
    public void setTotalServers(Integer totalServers) {
        this.totalServers = totalServers;
    }
    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
    public String getOrder() {
        return order;
    }
    public void setOrder(String order) {
        this.order = order;
    }
    public Date getUpdateTime() {
        return updateTime;
    }
    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }
    public Date getCreateTime() {
        return createTime;
    }
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
    public String getMessageId() {
        return messageId;
    }
    public String getTenant() {
        return tenant;
    }
    public void setTenant(String tenant) {
        this.tenant = tenant;
    }
}

+ 55 - 0
src/main/java/com/yihu/hos/logger/service/HosLogService.java

@ -0,0 +1,55 @@
package com.yihu.hos.logger.service;
import ch.qos.logback.classic.spi.ILoggingEvent;
import com.yihu.hos.logger.configuration.LoggerConfiguration;
import com.yihu.hos.logger.model.HosLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.Map;
/**
 * @created Airhead 2016/8/8.
 */
@Component("businessLogService")
public class HosLogService {
    @Autowired
    private MongoOperations mongoOperations;
    @Autowired
    private LoggerConfiguration loggerConfiguration;
    /**
     * camel.exchangeId The exchange id
     * camel.messageId The message id
     * camel.correlationId The correlation id of the exchange if it's correlated. For example a sub message from the Splitter EIP
     * camel.transactionKey The id of the transaction for transacted exchanges.
     * Note the id is not unique, but its the id of the transaction template that marks the transaction boundary for the given transaction.
     * Hence we decided to name the key transactionKey and not transactionID to point out this fact.
     * camel.routeId The id of the route, in which the exchange is currently being routed
     * camel.breadcrumbId An unique id used for tracking messages across transports.
     * camel.contextId The camel context id used for tracking the message from different camel context.
     *
     * @param event
     */
    public void log(ILoggingEvent event) {
        Map<String, String> mdcPropertyMap = event.getMDCPropertyMap();
        HosLog hosLog = new HosLog();
        hosLog.setFireTime(new Date(event.getTimeStamp()));
        hosLog.setExchangeId(mdcPropertyMap.get("camel.exchangeId"));
        hosLog.setMessageId(mdcPropertyMap.get("camel.messageId"));
        hosLog.setCorrelationId(mdcPropertyMap.get("camel.correlationId"));
        hosLog.setTransactionKey(mdcPropertyMap.get("camel.transactionKey"));
        hosLog.setRouteId(mdcPropertyMap.get("camel.routeId"));
        hosLog.setBreadcrumbId(mdcPropertyMap.get("camel.breadcrumbId"));
        hosLog.setCamelContextId(mdcPropertyMap.get("camel.contextId"));
        hosLog.setBody(event.getMessage());
        hosLog.setBodyLength(event.getMessage().length());
        hosLog.setTenant(loggerConfiguration.getTenant());
        mongoOperations.save(hosLog);
    }
}

+ 74 - 0
src/main/resources/application.yml

@ -0,0 +1,74 @@
server:
  port: 0
security:
  basic:
    enabled: false
# the name of Camel
camel:
  springboot:
    name: hos-logger
---
spring:
  profiles: dev
  activemq:
      broker-url: tcp://172.19.103.42:61616
      user: admin
      password: admin
  data:
    mongodb:
      host: 172.19.103.57
      port: 27017
      username: esb
      password: esb
      authenticationDatabase: admin
      database: log
      gridFsDatabase: dfs
logger:
  tenant: jkzl
  ehr: d:/hos-logger-log
---
spring:
  profiles: test
  activemq:
      broker-url: tcp://172.19.103.57:61616
      user: admin
      password: admin
  data:
    mongodb:
      host: 172.19.103.57
      port: 27017
      username: esb
      password: esb
      authenticationDatabase: admin
      database: log
      gridFsDatabase: dfs
logger:
  tenant: yichang
  ehr: /usr/local/esb-logs/rolling
---
spring:
  profiles: prod
  activemq:
      broker-url: tcp://192.168.200.60:61616
      user: admin
      password: admin
  data:
    mongodb:
      host: 192.168.50.26
      port: 20000
      username: jkzlesb
      password: fgu$^ezl
      authenticationDatabase: admin
      database: log
      gridFsDatabase: dfs
logger:
  tenant: jkzl
  ehr: /usr/local/esb-logs/rolling

+ 37 - 0
src/main/resources/bootstrap.yml

@ -0,0 +1,37 @@
spring:
  application:
    name: hos-logger
security:
  basic:
    enabled: false
---
spring:
  profiles: dev
  cloud:
    config:
      username: user
      password: configuration
      uri: ${spring.config.uri:http://172.19.103.73:1221}
      label: ${spring.config.label:dev}
---
spring:
  profiles: test
  cloud:
    config:
      username: user
      password: configuration
      uri: ${spring.config.uri:http://172.19.103.73:1221}
      label: ${spring.config.label:dev}
---
spring:
  profiles: prod
  cloud:
    config:
      username: user
      password: configuration
      uri: ${spring.config.uri}
      label: ${spring.config.label}

+ 16 - 0
src/test/java/com/yihu/hos/logger/HosLoggerApplicationTests.java

@ -0,0 +1,16 @@
package com.yihu.hos.logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class HosLoggerApplicationTests {
	@Test
	public void contextLoads() {
	}
}