SpringBoot MySQL ElasticSearch
【代码】SpringBoot MySQL。
·
SpringBoot MySQL ElasticSearch
1 Spring依赖
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.9</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xu</groupId>
<artifactId>spring-mysql</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-mysql</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>24</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.40</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter-test</artifactId>
<version>3.0.5</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<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>
</plugin>
</plugins>
</build>
</project>
2 Spring配置
server:
port: 8080
servlet:
context-path: /spring
spring:
application:
name: spring
# MySQL配置
datasource:
url: jdbc:mysql://localhost:3306/ry-vue?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: root
password: 123456
# Elasticsearch配置
elasticsearch:
uris: http://localhost:9200
password: elastic
username: elastic
# Redis配置
data:
redis:
# 地址
host: localhost
# 端口,默认为6379
port: 6379
# 数据库索引
database: 0
# 密码
password:
# 连接超时时间
timeout: 10s
lettuce:
pool:
# 连接池中的最小空闲连接
min-idle: 0
# 连接池中的最大空闲连接
max-idle: 8
# 连接池的最大数据库连接数
max-active: 8
# #连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1ms
mybatis:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath:mapper/*.xml
logging:
file:
name: logs/spring-oracle.log
level:
root: INFO
com.xu: INFO
3 Redis配置
package com.xu.spring.conf;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
// 创建RedisTemplate实例
RedisTemplate<String, Object> template = new RedisTemplate<>();
// 设置连接工厂
template.setConnectionFactory(connectionFactory);
// 配置Key的序列化器(String类型)
StringRedisSerializer stringSerializer = new StringRedisSerializer();
template.setKeySerializer(stringSerializer);
template.setHashKeySerializer(stringSerializer);
// 配置Value的序列化器(JSON类型,支持对象序列化)
GenericJackson2JsonRedisSerializer jsonSerializer = new GenericJackson2JsonRedisSerializer();
template.setValueSerializer(jsonSerializer);
template.setHashValueSerializer(jsonSerializer);
// 初始化模板
template.afterPropertiesSet();
return template;
}
}
4 Dockerfile配置
# 基础镜像
FROM eclipse-temurin:24-jre-alpine
# 设置工作目录
WORKDIR /app
# 创建用户、组及日志目录(合并命令)
RUN addgroup -S appgroup -g 1001 && \
adduser -S tom -u 1001 -G appgroup && \
mkdir -p /app/logs && \
chown -R tom:appgroup /app/logs /app
# 切换用户
USER tom
# 复制应用(已包含权限设置)
COPY --chown=tom:appgroup ./target/*.jar app.jar
# 暴露端口
EXPOSE 8080
# 启动命令
ENTRYPOINT ["java", "-XX:+UseContainerSupport", "-jar", "app.jar"]
1 Dockerfile运行
正在部署 'test Dockerfile: Dockerfile'…
[+] Building 11.2s (9/9) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.1s
=> => transferring dockerfile: 581B 0.0s
=> [internal] load metadata for docker.io/library/eclipse-temurin:24-jre-alpine 9.8s
=> [internal] load .dockerignore 0.1s
=> => transferring context: 2B 0.0s
=> [1/4] FROM docker.io/library/eclipse-temurin:24-jre-alpine@sha256:4044b6c87cb088885bcd0220f7dc7a8a4aab76577605fa471945d2e98270741f 0.0s
=> [internal] load build context 0.8s
=> => transferring context: 55.87MB 0.7s
=> CACHED [2/4] WORKDIR /app 0.0s
=> [3/4] RUN addgroup -S appgroup -g 1001 && adduser -S tom -u 1001 -G appgroup && mkdir -p /app/logs && chown -R tom:appgro 0.4s
=> [4/4] COPY --chown=tom:appgroup ./target/*.jar app.jar 0.2s
=> exporting to image 0.2s
=> => exporting layers 0.1s
=> => writing image sha256:1302d15a50e56929fcdbbcafb8fad9248cae46ded5b4c61d41569a62eb129330 0.0s
=> => naming to docker.io/library/test 0.0s
View build details: docker-desktop://dashboard/build/default/default/nx4gchwgsfcpmxq9xcs7cw1yh
Creating container…
Container Id: 6b6081b9bea1718302b8d75479db6b138d5d14fe7734842ad4321d96446cdc16
Container name: 'test'
Starting container 'test'
已成功部署 'test Dockerfile: Dockerfile'。
2 Docker容器运行
2025-09-18T01:47:33.996465292Z
2025-09-18T01:47:33.996598113Z . ____ _ __ _ _
2025-09-18T01:47:33.996604657Z /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
2025-09-18T01:47:33.996605974Z ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
2025-09-18T01:47:33.996607033Z \\/ ___)| |_)| | | | | || (_| | ) ) ) )
2025-09-18T01:47:33.996607902Z ' |____| .__|_| |_|_| |_\__, | / / / /
2025-09-18T01:47:33.996609069Z =========|_|==============|___/=/_/_/_/
2025-09-18T01:47:33.996609918Z
2025-09-18T01:47:34.000276310Z :: Spring Boot :: (v3.4.9)
2025-09-18T01:47:34.000301827Z
2025-09-18T01:47:34.045680910Z 2025-09-18T01:47:34.044Z INFO 1 --- [spring] [ main] com.xu.SpringMysqlApplication : Starting SpringMysqlApplication v0.0.1-SNAPSHOT using Java 24.0.2 with PID 1 (/app/app.jar started by tom in /app)
2025-09-18T01:47:34.046652119Z 2025-09-18T01:47:34.046Z INFO 1 --- [spring] [ main] com.xu.SpringMysqlApplication : No active profile set, falling back to 1 default profile: "default"
2025-09-18T01:47:34.546368742Z 2025-09-18T01:47:34.545Z INFO 1 --- [spring] [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode
2025-09-18T01:47:34.547656523Z 2025-09-18T01:47:34.547Z INFO 1 --- [spring] [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Elasticsearch repositories in DEFAULT mode.
2025-09-18T01:47:34.556567238Z 2025-09-18T01:47:34.556Z INFO 1 --- [spring] [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 4 ms. Found 0 Elasticsearch repository interfaces.
2025-09-18T01:47:34.560925598Z 2025-09-18T01:47:34.560Z INFO 1 --- [spring] [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode
2025-09-18T01:47:34.561219598Z 2025-09-18T01:47:34.561Z INFO 1 --- [spring] [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Reactive Elasticsearch repositories in DEFAULT mode.
2025-09-18T01:47:34.562877971Z 2025-09-18T01:47:34.562Z INFO 1 --- [spring] [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 0 ms. Found 0 Reactive Elasticsearch repository interfaces.
2025-09-18T01:47:34.580052547Z 2025-09-18T01:47:34.579Z INFO 1 --- [spring] [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode
2025-09-18T01:47:34.580543901Z 2025-09-18T01:47:34.580Z INFO 1 --- [spring] [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2025-09-18T01:47:34.588062918Z 2025-09-18T01:47:34.587Z INFO 1 --- [spring] [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 0 ms. Found 0 Redis repository interfaces.
2025-09-18T01:47:34.975409676Z 2025-09-18T01:47:34.974Z INFO 1 --- [spring] [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 8080 (http)
2025-09-18T01:47:34.987954730Z 2025-09-18T01:47:34.987Z INFO 1 --- [spring] [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2025-09-18T01:47:34.988288411Z 2025-09-18T01:47:34.987Z INFO 1 --- [spring] [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.44]
2025-09-18T01:47:35.009676417Z 2025-09-18T01:47:35.009Z INFO 1 --- [spring] [ main] o.a.c.c.C.[.[localhost].[/spring] : Initializing Spring embedded WebApplicationContext
2025-09-18T01:47:35.009838584Z 2025-09-18T01:47:35.009Z INFO 1 --- [spring] [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 925 ms
2025-09-18T01:47:35.109389798Z WARNING: A terminally deprecated method in sun.misc.Unsafe has been called
2025-09-18T01:47:35.109419778Z WARNING: sun.misc.Unsafe::allocateMemory has been called by io.netty.util.internal.PlatformDependent0$2 (jar:nested:/app/app.jar/!BOOT-INF/lib/netty-common-4.1.124.Final.jar!/)
2025-09-18T01:47:35.109421380Z WARNING: Please consider reporting this to the maintainers of class io.netty.util.internal.PlatformDependent0$2
2025-09-18T01:47:35.109422455Z WARNING: sun.misc.Unsafe::allocateMemory will be removed in a future release
2025-09-18T01:47:35.591246619Z Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
2025-09-18T01:47:36.044246908Z 2025-09-18T01:47:36.043Z INFO 1 --- [spring] [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path '/spring'
2025-09-18T01:47:36.063133012Z 2025-09-18T01:47:36.062Z INFO 1 --- [spring] [ main] com.xu.SpringMysqlApplication : Started SpringMysqlApplication in 2.383 seconds (process running for 2.796)
更多推荐
所有评论(0)