From c03020069e35edd2a542f1482c4671941191e0a9 Mon Sep 17 00:00:00 2001 From: dingzhaojun Date: Mon, 14 Aug 2023 09:19:43 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitattributes | 1 + .gitignore | 28 ++++++++ docker-compose.yml | 59 ++++++++++++++++ pom.xml | 67 +++++++++++++++++++ .../net/risesoft/demo/Demo01Application.java | 13 ++++ .../net/risesoft/demo/OnApplicationReady.java | 29 ++++++++ .../demo/controller/UserController.java | 27 ++++++++ .../java/net/risesoft/demo/entity/User.java | 20 ++++++ .../demo/repository/UserRepository.java | 11 +++ ...DockerComposeConnectionDetailsFactory.java | 40 +++++++++++ src/main/resources/META-INF/spring.factories | 4 ++ src/main/resources/application.properties | 1 + 12 files changed, 300 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 docker-compose.yml create mode 100644 pom.xml create mode 100644 src/main/java/net/risesoft/demo/Demo01Application.java create mode 100644 src/main/java/net/risesoft/demo/OnApplicationReady.java create mode 100644 src/main/java/net/risesoft/demo/controller/UserController.java create mode 100644 src/main/java/net/risesoft/demo/entity/User.java create mode 100644 src/main/java/net/risesoft/demo/repository/UserRepository.java create mode 100644 src/main/java/org/springframework/boot/docker/compose/service/connection/kafka/KafkaDockerComposeConnectionDetailsFactory.java create mode 100644 src/main/resources/META-INF/spring.factories create mode 100644 src/main/resources/application.properties diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..94f480d --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto eol=lf \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4532bdd --- /dev/null +++ b/.gitignore @@ -0,0 +1,28 @@ +# Build and Release Folders +target/ + +# Eclipse +.settings/ +*.classpath +*.project +*.factorypath + +# IntelliJ IDEA +.idea +*.iml + +# vscode +.vscode + +# JRebel +rebel.xml + +# macOS +.DS_Store + +# svn +.svn/ + +# vue +kernel-standard/ +node_modules/ \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..9e1592a --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,59 @@ +version: '3.1' + +services: + db: + image: mysql:latest + container_name: mysql01 + restart: always + ports: + - "3309:3306" + command: --default-authentication-plugin=mysql_native_password + environment: + MYSQL_ROOT_PASSWORD: 'Y9i-83204585' + MYSQL_DATABASE: 'demo01' + volumes: + - d:/docker-data/mysql:/var/lib/mysql + + redis: + image: redis:latest + container_name: redis01 + restart: always + ports: + - "7379:6379" + command: redis-server --requirepass "y9i-83204585" + volumes: + - d:/docker-data/redis:/data + + elasticsearch: + image: elasticsearch:8.8.1 + container_name: elastic01 + restart: always + ports: + - '9200:9200' + - '9300:9300' + volumes: + - d:/docker-data/es:/usr/share/elasticsearch/data + environment: + - discovery.type=single-node + - xpack.security.enabled=false + labels: + org.springframework.boot.service-connection: elasticsearch + + kafka: + image: bitnami/kafka:3.5 + container_name: kafka01 + restart: always + ports: + - "9092:9092" + - "9094:9094" + environment: + - ALLOW_PLAINTEXT_LISTENER=yes + - KAFKA_ENABLE_KRAFT=yes + - KAFKA_CFG_LISTENERS=PLAINTEXT://:9092,CONTROLLER://:9093,EXTERNAL://:9094 + - KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://kafka:9092,EXTERNAL://localhost:9094 + - KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=CONTROLLER:PLAINTEXT,EXTERNAL:PLAINTEXT,PLAINTEXT:PLAINTEXT + - KAFKA_CFG_INTER_BROKER_LISTENER_NAME=PLAINTEXT + volumes: + - d:/docker-data/kafka:/bitnami/kafka + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..4a4b44d --- /dev/null +++ b/pom.xml @@ -0,0 +1,67 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.1.1 + + + net.risesoft + demo01 + 1.0 + demo01 + Demo project for Spring Boot + + 17 + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-web + + + + com.mysql + mysql-connector-j + runtime + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-docker-compose + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + + + diff --git a/src/main/java/net/risesoft/demo/Demo01Application.java b/src/main/java/net/risesoft/demo/Demo01Application.java new file mode 100644 index 0000000..02ac729 --- /dev/null +++ b/src/main/java/net/risesoft/demo/Demo01Application.java @@ -0,0 +1,13 @@ +package net.risesoft.demo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Demo01Application { +// + public static void main(String[] args) { + SpringApplication.run(Demo01Application.class, args); + } + +} diff --git a/src/main/java/net/risesoft/demo/OnApplicationReady.java b/src/main/java/net/risesoft/demo/OnApplicationReady.java new file mode 100644 index 0000000..0eff663 --- /dev/null +++ b/src/main/java/net/risesoft/demo/OnApplicationReady.java @@ -0,0 +1,29 @@ +package net.risesoft.demo; + +import org.springframework.boot.autoconfigure.service.connection.ConnectionDetails; +import org.springframework.boot.context.event.ApplicationReadyEvent; +import org.springframework.context.ApplicationListener; +import org.springframework.stereotype.Component; + +import net.risesoft.demo.entity.User; +import net.risesoft.demo.repository.UserRepository; + +@Component +public class OnApplicationReady implements ApplicationListener { + + @Override + public void onApplicationEvent(ApplicationReadyEvent event) { + System.out.println(event.getApplicationContext().getEnvironment().getProperty("path")); + String[] ss = event.getApplicationContext().getBeanNamesForType(ConnectionDetails.class); + for (String s : ss) { + System.out.println(s); + } + + UserRepository userRepository = event.getApplicationContext().getBean(UserRepository.class); + User user = new User(); + user.setName("张三"); + user.setAge(20); + userRepository.save(user); + + } +} diff --git a/src/main/java/net/risesoft/demo/controller/UserController.java b/src/main/java/net/risesoft/demo/controller/UserController.java new file mode 100644 index 0000000..cd138bb --- /dev/null +++ b/src/main/java/net/risesoft/demo/controller/UserController.java @@ -0,0 +1,27 @@ +package net.risesoft.demo.controller; + +import java.util.List; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import net.risesoft.demo.entity.User; +import net.risesoft.demo.repository.UserRepository; + +@RestController +@RequestMapping("/api/users") +public class UserController { + + private final UserRepository userRepository; + + public UserController(UserRepository userRepository) { + super(); + this.userRepository = userRepository; + } + + @GetMapping + public List findAll(){ + return userRepository.findAll(); + } +} diff --git a/src/main/java/net/risesoft/demo/entity/User.java b/src/main/java/net/risesoft/demo/entity/User.java new file mode 100644 index 0000000..52a968d --- /dev/null +++ b/src/main/java/net/risesoft/demo/entity/User.java @@ -0,0 +1,20 @@ +package net.risesoft.demo.entity; + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Entity +@Data +@NoArgsConstructor +public class User { + + @Id + @GeneratedValue + private Integer id; + private String name; + private Integer age; + +} diff --git a/src/main/java/net/risesoft/demo/repository/UserRepository.java b/src/main/java/net/risesoft/demo/repository/UserRepository.java new file mode 100644 index 0000000..3efd09a --- /dev/null +++ b/src/main/java/net/risesoft/demo/repository/UserRepository.java @@ -0,0 +1,11 @@ +package net.risesoft.demo.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import net.risesoft.demo.entity.User; + +@Repository +public interface UserRepository extends JpaRepository{ + +} diff --git a/src/main/java/org/springframework/boot/docker/compose/service/connection/kafka/KafkaDockerComposeConnectionDetailsFactory.java b/src/main/java/org/springframework/boot/docker/compose/service/connection/kafka/KafkaDockerComposeConnectionDetailsFactory.java new file mode 100644 index 0000000..cc2782c --- /dev/null +++ b/src/main/java/org/springframework/boot/docker/compose/service/connection/kafka/KafkaDockerComposeConnectionDetailsFactory.java @@ -0,0 +1,40 @@ +package org.springframework.boot.docker.compose.service.connection.kafka; + +import java.util.List; + +import org.springframework.boot.autoconfigure.kafka.KafkaConnectionDetails; +import org.springframework.boot.docker.compose.core.RunningService; +import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionDetailsFactory; +import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionSource; + +class KafkaDockerComposeConnectionDetailsFactory extends DockerComposeConnectionDetailsFactory { + + KafkaDockerComposeConnectionDetailsFactory() { + super("bitnami/kafka"); + } + + @Override + protected KafkaConnectionDetails getDockerComposeConnectionDetails(DockerComposeConnectionSource source) { + return new KafkaDockerComposeConnectionDetails(source.getRunningService()); + } + + static class KafkaDockerComposeConnectionDetails extends DockerComposeConnectionDetails + implements KafkaConnectionDetails { + + private final List bootstrapServers; + + KafkaDockerComposeConnectionDetails(RunningService service) { + super(service); + this.bootstrapServers = List.of("localhost:9094"); + // this.bootstrapServers = + // List.of(service.env().get("bootstrapServers").split(",")); + } + + @Override + public List getBootstrapServers() { + return this.bootstrapServers; + } + + } + +} diff --git a/src/main/resources/META-INF/spring.factories b/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000..e8828a2 --- /dev/null +++ b/src/main/resources/META-INF/spring.factories @@ -0,0 +1,4 @@ +# Connection Details Factories +org.springframework.boot.autoconfigure.service.connection.ConnectionDetailsFactory=\ +org.springframework.boot.docker.compose.service.connection.kafka.KafkaDockerComposeConnectionDetailsFactory + diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..224e355 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.jpa.generate-ddl=true