docker搭建redis
1,在 Docker 中拉取 Redis 镜像:
docker pull redis
2,创建挂在目录:
liunx 下redis.conf文件位置: /home/redis/myredis/redis.conf
liunx 下redis的data文件位置 : /home/redis/myredis/data
mkdir -p /home/redis/myredis/data
redis.conf文件内容:
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1
#bind 127.0.0.1
protected-mode no
port 6379
tcp-backlog 511
requirepass 123456
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile ""
databases 30
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly yes
appendfilename "appendonly.aof"
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-max-len 128
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
3,安装redis
docker run --restart=always --log-opt max-size=100m --log-opt max-file=2 -p 6379:6379 --name myredis -v /home/redis/myredis/redis.conf:/etc/redis/redis.conf -v /home/redis/myredis/data:/data -d redis redis-server /etc/redis/redis.conf --appendonly yes --requirepass 123456
-----------------------------------------------------------------------------------------------------------
springboot+docker+docker-compose+swagger
springboot项目使用docker、docker-compose部署,并用swagger测试
1,添加相关依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.5</version>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.7.0</version>
<configuration>
<!-- 指定该Main Class为全局的唯一入口 -->
<mainClass>org.example.App</mainClass>
</configuration>
<executions>
<execution>
<goals>
<!--可以把依赖的包都打包到生成的Jar包中-->
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
注: 如果生成的jar包使用java -jar xxx.jar报错,则需要看下有没有指定入口类
另外,maven插件与spring boot版本有对应关系,可以尝试升/降级版本
控制台没打开一个terminal,如果不关闭,则该命令窗口会一直在后台执行占用文件,需要手动关闭,否则会在 mvn clean等命令执行时报错
2,dockerfile及docker-compose、application.yml配置文件
dockerfile:
FROM hub.c.163.com/library/java:8-alpine
WORKDIR /java_demo
COPY target/java_demo-1.0-SNAPSHOT.jar java_demo.jar
#设置镜像的时区,避免出现8小时的误差
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
EXPOSE 8080
ENTRYPOINT ["java","-Xms256m","-Xmx512m","-jar","java_demo.jar"]
docker-compose:
version: "3"
services:
demo:
# 指定启动后的容器名
container_name: demo
build: .
ports:
- "8080:8080"
yml配置文件
server:
port: 8080
spring:
application:
name: demo
swagger:
base-package: com.gj.web.api
3,swagger配置类SwaggerConfig:
package org.example.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import java.util.ArrayList;
@Configuration
public class SwaggerConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations(
"classpath:/static/");
registry.addResourceHandler("swagger-ui.html").addResourceLocations(
"classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations(
"classpath:/META-INF/resources/webjars/");
}
@Bean
public Docket docket() {
return new Docket(new DocumentationType("SWAGGER", "2.0"))
.apiInfo(getApiInfo())
// .host("http://localhost:8080")
// .groupName("zhihao")
//.globalResponseMessage(RequestMethod.GET, Lists.newArrayList())
//.pathMapping("AAAAA")// 上下文
.enableUrlTemplating(true)
.select()
.apis(RequestHandlerSelectors.basePackage("org.example.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo getApiInfo() {
Contact contact = new Contact("zhihao", "https://gitee.com/hecanhc/dashboard/projects", "1020205116@qq.com");
return new ApiInfo("swagger文档", "描述", "2.0", "https://gitee.com/hecanhc/dashboard/projects",
contact, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList());
}
}
4,启动类:
@SpringBootApplication
//swagger ui需要以下两个注解,最后一个注解跟swagger版本有关系
@EnableSwagger2
@EnableWebMvc
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class);
}
}
5,打包部署并访问
mvn clean package
docker-compose up