Skip to content

Commit

Permalink
Update spring boot to 2.1.13.RELEASE
Browse files Browse the repository at this point in the history
  • Loading branch information
zccodere committed Apr 28, 2020
1 parent e5da4b3 commit 0109e8d
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 71 deletions.
51 changes: 41 additions & 10 deletions 49-kafka/49-kafka-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,31 @@
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<artifactId>49-kafka</artifactId>
<groupId>com.myimooc</groupId>
<version>1.0-SNAPSHOT</version>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>49-kafka-example</artifactId>

<properties>
<spring.boot.version>2.0.4.RELEASE</spring.boot.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<spring.boot.version>2.1.13.RELEASE</spring.boot.version>
<fastjson.version>1.2.66</fastjson.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-parent</artifactId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
Expand All @@ -28,6 +35,20 @@
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand All @@ -36,25 +57,35 @@
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.36</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
</dependencies>

<build>
<!-- 需要声明Spring Boot打包插件 -->
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
import org.springframework.kafka.annotation.EnableKafka;

/**
* <br>
* 标题: 启动类<br>
* 描述: 启动类<br>
* 时间: 2018/09/09<br>
* 启动类
*
* @author zc
* @date 2018/09/09
*/
@SpringBootApplication
@EnableKafka
@SpringBootApplication
public class ExampleApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.myimooc.kafka.example.common;

/**
* <br>
* 标题: 错误编码<br>
* 描述: 错误编码<br>
* 时间: 2018/09/09<br>
* 错误编码
*
* @author zc
* @date 2018/09/09
*/
public class ErrorCode {

/**
* 成功
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
import java.util.Objects;

/**
* <br>
* 标题: 消息实体<br>
* 描述: 消息实体<br>
* 时间: 2018/09/09<br>
* 消息实体
*
* @author zc
* @date 2018/09/09
*/
public class MessageEntity {

/**
* 标题
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
import java.io.Serializable;

/**
* <br>
* 标题: REST请求统一响应对象<br>
* 描述: REST请求统一响应对象<br>
* 时间: 2018/09/09<br>
* REST请求统一响应对象
*
* @author zc
* @date 2018/09/09
*/
public class Response implements Serializable {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.alibaba.fastjson.JSONObject;
import com.myimooc.kafka.example.common.MessageEntity;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -13,12 +14,10 @@
import java.util.Optional;

/**
* <br>
* 标题: 消费者<br>
* 描述: 消费者<br>
* 时间: 2018/09/09<br>
* 消费者
*
* @author zc
* @date 2018/09/09
*/
@Component
public class SimpleConsumer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@
import com.myimooc.kafka.example.common.MessageEntity;
import com.myimooc.kafka.example.common.Response;
import com.myimooc.kafka.example.producer.SimpleProducer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* <br>
* 标题: 生产者Controller<br>
* 描述: 生产者Controller<br>
* 时间: 2018/09/09<br>
* 生产者Controller
*
* @author zc
* @date 2018/09/09
*/
@RestController
@RequestMapping("/producer")
Expand All @@ -26,7 +28,7 @@ public class ProducerController {
private Logger logger = LoggerFactory.getLogger(getClass());

@Autowired
private SimpleProducer simpleProducer;
private SimpleProducer<MessageEntity> simpleProducer;

@Value("${kafka.topic.default}")
private String topic;
Expand All @@ -39,10 +41,10 @@ public Response sendKafka(@RequestBody MessageEntity message) {
logger.info("kafka的消息:{}", JSON.toJSONString(message));
this.simpleProducer.send(topic, KEY, message);
logger.info("kafka消息发送成功!");
return new Response(ErrorCode.SUCCESS,"kafka消息发送成功");
return new Response(ErrorCode.SUCCESS, "kafka消息发送成功");
} catch (Exception ex) {
logger.error("kafka消息发送失败:", ex);
return new Response(ErrorCode.EXCEPTION,"kafka消息发送失败");
return new Response(ErrorCode.EXCEPTION, "kafka消息发送失败");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.myimooc.kafka.example.producer;

import com.alibaba.fastjson.JSON;

import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;
import org.slf4j.Logger;
Expand All @@ -13,49 +14,49 @@
import org.springframework.util.concurrent.ListenableFutureCallback;

/**
* <br>
* 标题: 生产者<br>
* 描述: 生产者<br>
* 时间: 2018/09/09<br>
* 生产者
*
* @author zc
* @date 2018/09/09
*/
@Component
public class SimpleProducer<T> {

private Logger logger = LoggerFactory.getLogger(getClass());

@Autowired
private KafkaTemplate<String, Object> kafkaTemplate;
private KafkaTemplate<String, String> kafkaTemplate;

public void send(String topic, String key, Object entity) {
public void send(String topic, String key, T entity) {
logger.info("发送消息入参:{}", entity);
ProducerRecord<String, Object> record = new ProducerRecord<>(

// 将任意对象转换为JSON字符串进行消息发送
ProducerRecord<String, String> record = new ProducerRecord<>(
topic,
key,
JSON.toJSONString(entity)
);

long startTime = System.currentTimeMillis();
ListenableFuture<SendResult<String, Object>> future = this.kafkaTemplate.send(record);
future.addCallback(new ListenableFutureCallback<SendResult<String, Object>>() {
ListenableFuture<SendResult<String, String>> future = this.kafkaTemplate.send(record);
future.addCallback(new ListenableFutureCallback<SendResult<String, String>>() {
@Override
public void onFailure(Throwable ex) {
logger.error("消息发送失败:{}", ex);
logger.error("消息发送失败:", ex);
}

@Override
public void onSuccess(SendResult<String, Object> result) {
public void onSuccess(SendResult<String, String> result) {
long elapsedTime = System.currentTimeMillis() - startTime;

RecordMetadata metadata = result.getRecordMetadata();

StringBuilder record = new StringBuilder(128);
record.append("message(")
.append("key = ").append(key).append(",")
.append("message = ").append(entity).append(")")
.append("send to partition(").append(metadata.partition()).append(")")
.append("with offset(").append(metadata.offset()).append(")")
.append("in ").append(elapsedTime).append(" ms");
record.append("message(");
record.append("key = ").append(key).append(",");
record.append("message = ").append(entity).append(")");
record.append("send to partition(").append(metadata.partition()).append(")");
record.append("with offset(").append(metadata.offset()).append(")");
record.append("in ").append(elapsedTime).append(" ms");
logger.info("消息发送成功:{}", record.toString());
}
});
Expand Down
16 changes: 16 additions & 0 deletions 49-kafka/49-kafka-example/src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout
pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="org.springframework.web.servlet" level="DEBUG"/>
<Logger name="com.myimooc" level="DEBUG"/>
<Root level="INFO">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
15 changes: 1 addition & 14 deletions 49-kafka/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,10 @@
<groupId>com.myimooc</groupId>
<artifactId>49-kafka</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<version>1.0.0-SNAPSHOT</version>

<modules>
<module>49-kafka-example</module>
</modules>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

</project>

0 comments on commit 0109e8d

Please sign in to comment.