MOKASZ\lw12420 2 years ago
parent
commit
06df383a41

+ 126 - 49
pom.xml

@@ -5,18 +5,79 @@
     <parent>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-parent</artifactId>
-        <version>3.1.5</version>
+        <version>3.2.0</version>
         <relativePath/> <!-- lookup parent from repository -->
     </parent>
+
     <groupId>com.launch</groupId>
     <artifactId>app</artifactId>
     <version>0.0.1-SNAPSHOT</version>
     <name>platform</name>
     <description>platform</description>
+
+    <!-- 当前Maven工程的打包方式,可选值有下面三种: -->
+    <!-- jar:表示这个工程是一个Java工程  -->
+    <!-- war:表示这个工程是一个Web工程 -->
+    <!-- pom:表示这个工程是“管理其他工程”的工程 -->
+    <packaging>jar</packaging>
+
     <properties>
+        <!-- 工程构建过程中读取源码时使用的字符集 -->
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         <java.version>21</java.version>
+        <spring-cloud.version>2023.0.0-RC1</spring-cloud.version>
+        <nacos.client.version>2.2.1</nacos.client.version>
+        <revision>2022.0.0.0</revision>
     </properties>
+
     <dependencies>
+        <!--web -->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+        </dependency>
+        <!--cloud -->
+        <dependency>
+            <groupId>org.springframework.cloud</groupId>
+            <artifactId>spring-cloud-starter-config</artifactId>
+        </dependency>
+        <!--Nacos-->
+        <dependency>
+            <groupId>com.alibaba.nacos</groupId>
+            <artifactId>nacos-client</artifactId>
+            <version>${nacos.client.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>com.alibaba.cloud</groupId>
+            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
+            <version>${revision}</version>
+        </dependency>
+        <dependency>
+            <groupId>com.alibaba.cloud</groupId>
+            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
+            <version>${revision}</version>
+        </dependency>
+        <!-- 使用bootstrap.yml必须依赖此类 -->
+        <dependency>
+            <groupId>org.springframework.cloud</groupId>
+            <artifactId>spring-cloud-starter-bootstrap</artifactId>
+        </dependency>
+        <!--webflux-->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-webflux</artifactId>
+        </dependency>
+        <!-- SpringCloud Openfeign -->
+        <dependency>
+            <groupId>org.springframework.cloud</groupId>
+            <artifactId>spring-cloud-starter-openfeign</artifactId>
+        </dependency>
+        <!-- SpringCloud Loadbalancer -->
+
+        <dependency>
+            <groupId>org.springframework.cloud</groupId>
+            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
+        </dependency>
         <!-- db -->
         <dependency>
             <groupId>org.springframework.boot</groupId>
@@ -35,89 +96,105 @@
             <groupId>io.r2dbc</groupId>
             <artifactId>r2dbc-pool</artifactId>
         </dependency>
-        <!--r2dbc mysql 库-->
-        <!--        <dependency>-->
-        <!--            <groupId>dev.miku</groupId>-->
-        <!--            <artifactId>r2dbc-mysql</artifactId>-->
-        <!--            <version>0.8.2.RELEASE</version>-->
-        <!--        </dependency>-->
+
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-data-jdbc</artifactId>
         </dependency>
-        <!-- web -->
-        <dependency>
-            <groupId>jakarta.validation</groupId>
-            <artifactId>jakarta.validation-api</artifactId>
-        </dependency>
+        <!--springdoc openapi swagger ui -->
         <dependency>
-            <groupId>com.google.code.findbugs</groupId>
-            <artifactId>jsr305</artifactId>
-            <version>3.0.2</version>
+            <groupId>org.springdoc</groupId>
+            <artifactId>springdoc-openapi-starter-webflux-ui</artifactId>
+            <version>2.2.0</version>
         </dependency>
         <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-starter-webflux</artifactId>
+            <groupId>org.springdoc</groupId>
+            <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
+            <version>2.2.0</version>
         </dependency>
-        <!-- dev -->
+
+        <!--搭配校验使用-->
         <dependency>
             <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-starter-actuator</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>org.springdoc</groupId>
-            <artifactId>springdoc-openapi-starter-webflux-ui</artifactId>
-            <version>2.0.0</version>
+            <artifactId>spring-boot-starter-validation</artifactId>
+            <version>3.2.0</version>
         </dependency>
+        <!--让响应结果更美观 -->
         <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-devtools</artifactId>
-            <scope>runtime</scope>
-            <optional>true</optional>
+            <groupId>com.alibaba.cola</groupId>
+            <artifactId>cola-component-dto</artifactId>
+            <version>4.3.2</version>
         </dependency>
+        <!--elasticsearch -->
         <dependency>
             <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-configuration-processor</artifactId>
-            <optional>true</optional>
+            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
         </dependency>
+        <!--lombok -->
         <dependency>
             <groupId>org.projectlombok</groupId>
             <artifactId>lombok</artifactId>
             <optional>true</optional>
         </dependency>
-        <!-- test -->
+        <!--test -->
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-test</artifactId>
             <scope>test</scope>
         </dependency>
-        <dependency>
-            <groupId>io.projectreactor</groupId>
-            <artifactId>reactor-test</artifactId>
-            <scope>test</scope>
-        </dependency>
-        <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
-        </dependency>
     </dependencies>
 
+    <dependencyManagement>
+        <dependencies>
+            <dependency>
+                <groupId>org.springframework.cloud</groupId>
+                <artifactId>spring-cloud-dependencies</artifactId>
+                <version>${spring-cloud.version}</version>
+                <type>pom</type>
+                <scope>import</scope>
+            </dependency>
+            <dependency>
+                <groupId>com.alibaba.cloud</groupId>
+                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
+                <version>${revision}</version>
+                <type>pom</type>
+                <scope>import</scope>
+            </dependency>
+        </dependencies>
+    </dependencyManagement>
+
     <build>
         <plugins>
             <plugin>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
-                <configuration>
-                    <excludes>
-                        <exclude>
-                            <groupId>org.projectlombok</groupId>
-                            <artifactId>lombok</artifactId>
-                        </exclude>
-                    </excludes>
-                </configuration>
+                <version>3.2.0</version>
+                <executions>
+                    <execution>
+                        <id>pre-integration-test</id>
+                        <goals>
+                            <goal>start</goal>
+                        </goals>
+                    </execution>
+                    <execution>
+                        <id>post-integration-test</id>
+                        <goals>
+                            <goal>stop</goal>
+                        </goals>
+                    </execution>
+                </executions>
             </plugin>
         </plugins>
-
     </build>
+    <repositories>
+        <repository>
+            <id>spring-milestones</id>
+            <name>Spring Milestones</name>
+            <url>https://repo.spring.io/milestone</url>
+            <snapshots>
+                <enabled>false</enabled>
+            </snapshots>
+        </repository>
+    </repositories>
 
 </project>

+ 6 - 2
src/main/java/com/launch/api/DemoApplication.java

@@ -3,10 +3,14 @@ package com.launch.api;
 
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
 import org.springframework.transaction.annotation.EnableTransactionManagement;
 
-@EnableTransactionManagement
-@SpringBootApplication(scanBasePackages = {"com.*"})
+@EnableTransactionManagement //r2dbc开启事务
+@EnableDiscoveryClient  //开启springclound
+//@EnableFeignClients (scanBasePackages = {"com.*"})
+@SpringBootApplication
+
 public class DemoApplication {
 
     public static void main(String[] args) {

+ 18 - 0
src/main/java/com/launch/api/elasticSearch/entity/Index1.java

@@ -0,0 +1,18 @@
+package com.launch.api.elasticSearch.entity;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import org.springframework.data.annotation.Id;
+import org.springframework.data.elasticsearch.annotations.Document;
+import org.springframework.data.elasticsearch.annotations.Field;
+import org.springframework.data.elasticsearch.annotations.FieldType;
+
+@Document(indexName = "index001")
+@Data
+@AllArgsConstructor
+public class Index1 {
+    @Id
+    private Long id;
+    @Field(name = "name",type = FieldType.Annotated_Text)
+    private String name;
+}

+ 13 - 0
src/main/java/com/launch/api/elasticSearch/entity/Index2.java

@@ -0,0 +1,13 @@
+package com.launch.api.elasticSearch.entity;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import org.springframework.data.annotation.Id;
+import org.springframework.data.elasticsearch.annotations.Document;
+import org.springframework.data.elasticsearch.annotations.Field;
+import org.springframework.data.elasticsearch.annotations.FieldType;
+
+@Document(indexName = "index001")
+public record Index2(@Id  Long id, @Field(name = "name",type = FieldType.Annotated_Text) String name) {
+
+}

+ 28 - 0
src/main/java/com/launch/api/elasticSearch/service/TweeTEsService.java

@@ -0,0 +1,28 @@
+package com.launch.api.elasticSearch.service;
+
+import com.launch.api.elasticSearch.entity.Index1;
+import com.launch.api.elasticSearch.entity.Index2;
+import jakarta.annotation.Resource;
+import org.springframework.data.elasticsearch.client.elc.ReactiveElasticsearchTemplate;
+import org.springframework.stereotype.Service;
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+
+@Service
+public class TweeTEsService {
+
+
+
+    @Resource
+    private ReactiveElasticsearchTemplate reactiveElasticsearchTemplate;
+
+    public Mono<Index1> mySave(Index1 es) {
+        return reactiveElasticsearchTemplate.save(es);
+    }
+    public Mono<Index2> mySave2(Index2 es) {
+        return reactiveElasticsearchTemplate.save(es);
+    }
+    public Mono<Index2> get() {
+        return reactiveElasticsearchTemplate.get("1",Index2.class);
+    }
+}

+ 18 - 0
src/main/java/com/launch/api/global/GlobalExceptionHandler.java

@@ -0,0 +1,18 @@
+package com.launch.api.global;
+
+import com.alibaba.cola.dto.Response;
+import org.springframework.http.HttpStatus;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.bind.annotation.ResponseStatus;
+import org.springframework.web.bind.annotation.RestControllerAdvice;
+
+@RestControllerAdvice
+public class GlobalExceptionHandler {
+    @ResponseBody
+    @ResponseStatus(HttpStatus.BAD_REQUEST)
+    @ExceptionHandler(Exception.class)
+    public Response handleException(Exception e) {
+        return Response.buildFailure("未知异常", e.getMessage());
+    }
+}

+ 40 - 0
src/main/java/com/launch/api/mysql/controller/TweetController.java

@@ -0,0 +1,40 @@
+package com.launch.api.mysql.controller;
+
+import com.launch.api.elasticSearch.entity.Index1;
+import com.launch.api.elasticSearch.entity.Index2;
+import com.launch.api.elasticSearch.service.TweeTEsService;
+import com.launch.api.mysql.entity.test.Tweet;
+import com.launch.api.mysql.service.test.TweetService;
+import jakarta.annotation.Resource;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+
+
+@RestController
+@RequestMapping("tweet")
+public class TweetController {
+
+    @Resource
+    private TweeTEsService repository;
+
+    @Resource
+    private TweetService tweetService;
+    @GetMapping("getAll")
+    public Flux<Tweet> getAll(){
+       return tweetService.getAll();
+    }
+
+    @GetMapping("saveEs")
+    public Mono<Index1> saveEs(){
+        Index1 index1 = new Index1(1l,"xiao");
+        return repository.mySave(index1);
+    }
+    @GetMapping("getEs")
+    public Mono<Index2> getEs(){
+        return repository.get();
+    }
+
+}

+ 12 - 0
src/main/java/com/launch/api/mysql/entity/test/Tweet.java

@@ -0,0 +1,12 @@
+package com.launch.api.mysql.entity.test;
+
+import org.springframework.data.annotation.Id;
+import org.springframework.data.relational.core.mapping.Table;
+
+/**
+ * Created by rajeevkumarsingh on 08/09/17.
+ */
+@Table("t_weet")
+public record Tweet(@Id Long id, String text) {
+
+}

+ 9 - 0
src/main/java/com/launch/api/mysql/repository/test/TweetRepository.java

@@ -0,0 +1,9 @@
+package com.launch.api.mysql.repository.test;
+
+import com.launch.api.mysql.entity.test.Tweet;
+import org.springframework.data.r2dbc.repository.R2dbcRepository;
+import org.springframework.stereotype.Repository;
+
+@Repository
+public interface TweetRepository extends R2dbcRepository<Tweet,Long> {
+}

+ 20 - 0
src/main/java/com/launch/api/mysql/service/test/TweetService.java

@@ -0,0 +1,20 @@
+package com.launch.api.mysql.service.test;
+
+import com.launch.api.mysql.entity.test.Tweet;
+import com.launch.api.mysql.repository.test.TweetRepository;
+import jakarta.annotation.Resource;
+import org.springframework.data.r2dbc.repository.R2dbcRepository;
+import org.springframework.stereotype.Service;
+import reactor.core.publisher.Flux;
+
+@Service
+public class TweetService {
+    @Resource
+    private R2dbcRepository repository;
+    @Resource
+    private TweetRepository tweetRepository;
+
+    public Flux<Tweet> getAll(){
+       return tweetRepository.findAll();
+    }
+}

+ 23 - 0
src/main/java/com/launch/api/openApi/WebFluxKnife4jConfig.java

@@ -0,0 +1,23 @@
+package com.launch.api.openApi;
+
+
+
+
+import io.swagger.v3.oas.models.ExternalDocumentation;
+import io.swagger.v3.oas.models.OpenAPI;
+import io.swagger.v3.oas.models.info.Info;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+public class WebFluxKnife4jConfig {
+    @Bean
+    public OpenAPI swaggeropenApi(){
+        return new OpenAPI()
+                .info(new Info()
+                        .title("投放")
+                        .description("平台").version("v1.0.0"))
+                .externalDocs(new ExternalDocumentation().description("文档")
+                        .url("http://localhost:8080/launch/v3/api-docs"));
+    }
+}

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

@@ -1,10 +0,0 @@
-server:
-  port: 8080
-spring:
-  elasticsearch:
-    rest:
-      uris: 127.0.0.1:9200
-  r2dbc:
-    url: r2dbcs:mysql://127.0.0.1:3306/test?serverZoneId=GMT%2b8&characterEncoding=UTF-8
-    username: root
-    password: 'QWERqwer!@#123'

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

@@ -0,0 +1,37 @@
+server:
+  port: 8080
+#  servlet:
+#    context-path: /
+#  compression:
+#    enabled: true
+#    mime-types: text/html,text/xml,text/plain,text/css,text/javascript,application/javascript,application/json
+#    min-response-size: 1024
+#  http2:
+#    enabled: true
+
+springdoc:
+  swagger-ui:
+    path: /launch/index.html
+    enabled: true
+  api-docs:
+    path: /launch/v3/api-docs
+    enabled: true #开启swagger界面 依赖openApi 需要openApi打开
+
+spring:
+  application:
+    name: launch-webflux
+  cloud:
+    nacos:
+      discovery:
+        server-addr: 172.18.71.46:8848  # 127.0.0.1:8848  #
+  elasticsearch:
+    rest:
+      uris: 127.0.0.1:9200
+  r2dbc:
+    url: r2dbcs:mysql://172.18.71.27:3306/launch_platform?serverZoneId=GMT%2b8&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8  # r2dbcs:mysql://127.0.0.1:3306/test?serverZoneId=GMT%2b8&characterEncoding=UTF-8
+    username: launch_platform # root
+    password: launch_platform # 'QWERqwer!@#123'
+
+#    url: jdbc:mysql://172.18.71.27:3306/launch_platform?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
+#      username: launch_platform
+#      password: launch_platform

+ 2 - 0
src/test/java/com/launch/api/DemoApplicationTests.java

@@ -3,6 +3,8 @@ package com.launch.api;
 import org.junit.jupiter.api.Test;
 import org.springframework.boot.test.context.SpringBootTest;
 
+
+
 @SpringBootTest
 class DemoApplicationTests {