Commit 4d234e47 by wnb0128

车家智能数据服务 推送kafka--think.wang

1 parent b6dc18cd
......@@ -63,16 +63,10 @@
</dependency>
<dependency>
<groupId>com.glsx</groupId>
<artifactId>rest-admin-kafka</artifactId>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.kafka</groupId>-->
<!-- <artifactId>spring-kafka</artifactId>-->
<!-- <version>2.5.6.RELEASE</version>-->
<!-- </dependency>-->
<dependency>
<groupId>com.glsx</groupId>
<artifactId>rest-admin-swagger2</artifactId>
......@@ -85,6 +79,11 @@
<artifactId>biz-user-service</artifactId>
<version>3.1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_2.10</artifactId>
<version>0.8.1.1</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.glsx.smart.config;
import com.alibaba.fastjson.JSON;
import com.glsx.smart.constant.Constants;
import com.glsx.smart.pojo.DeviceMessage;
import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
......@@ -22,7 +23,10 @@ import javax.annotation.Resource;
public class DeviceAddressProducer {
@Resource
private KafkaTemplate kafkaTemplate;
private Producer producer;
@Autowired
private KafkaConfig kafkaConfig;
/**
* 推送设备经纬度数据
......@@ -32,13 +36,19 @@ public class DeviceAddressProducer {
*/
public void pushUserDeviceData(String deviceId, String lngAndLat, Integer type) {
//kafka 发送消息数据封装
DeviceMessage message = new DeviceMessage();
message.setDeviceId(deviceId);
message.setLngAndLat(lngAndLat);
message.setType(type);
log.info("推送kafka topic【{}】的数据:{}", Constants.DEVICE_DATA_TOPIC, message.toString());
//推送kafka
kafkaTemplate.send(Constants.DEVICE_DATA_TOPIC, JSON.toJSONString(message));
DeviceMessage data = new DeviceMessage();
data.setDeviceId(deviceId);
data.setLngAndLat(lngAndLat);
data.setType(type);
try {
String topic = kafkaConfig.getDeviceTopic();
String sendMessage = JSON.toJSONString(data);
log.info("推送kafka topic【{}】的数据:{}", topic, sendMessage);
KeyedMessage<byte[], byte[]> message = new KeyedMessage<>(topic, deviceId.getBytes(), sendMessage.getBytes());
producer.send(message);
}catch (Exception e){
}
}
}
package com.glsx.smart.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
* @author fengzhi
* @version 1.0.0
* @ClassName KafkaProductorConfig.java
* @createTime 2020年03月12日 16:21:00
*/
@Data
@Configuration
@ConfigurationProperties("mq.kafka")
public class KafkaConfig {
/**
* kafka 生产者服务端地址
*/
private String productServer;
/**
* 设备围栏数据topic
*/
private String deviceTopic;
}
package com.glsx.smart.config;
import kafka.javaapi.producer.Producer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import kafka.producer.ProducerConfig;
import java.util.Properties;
/**
* @author fengzhi
* @version 1.0.0
* @ClassName KafkaProductorConfig.java
* @description: kafka生产消息
* @createTime 2021年03月12日 16:21:00
*/
@Slf4j
@Configuration
public class KafkaProducerConfig {
@Autowired
private KafkaConfig kafkaConfig;
@Bean("producer")
public Producer getProducer() {
ProducerConfig producerConfig = new ProducerConfig(initProperties());
return new Producer(producerConfig);
}
private Properties initProperties() {
Properties properties = new Properties();
properties.setProperty("serializer.class", "kafka.serializer.DefaultEncoder");
properties.setProperty("key.serializer.class", "kafka.serializer.DefaultEncoder");
properties.setProperty("metadata.broker.list", kafkaConfig.getProductServer());
properties.setProperty("producer.type", "async");
log.info("kafka_server" + kafkaConfig.getProductServer());
return properties;
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!