开发者

SpringBoot3 整合Docker-Compose的实现步骤

目录
  • 它有什么优势
  • 添加依赖
  • compose推荐配置
  • docker-compose生命管理器
  • 注意事项

它有什么优势

同样的约定大于配置,使用docker-compose时写的配置可以被SpringBoot3自动发现,在使用它时我们可以减少更多的书写配置,一切由它自动完成,比如常见的mysql,Redis等都可以省略配置了,后面有具体的例子

添加依赖

 <!--  springboot3 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.3.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

 <!--  docker-compose-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-docker-compose</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

 <!--  mysql-->
         <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>

 <!--  redis-->
        编程客栈 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

compose推荐配置

注意skip 需要在start_only下才能正常执行,不然在start_and_stop模式下应用启动->启动容器->关闭容器->关机 ,skip等于无效参数

spring:
  docker:
 编程客栈   compose:
      profiles:
        active: dev # 指定当前运行环境
      enabled: true # 开启compose
      start:
        skip: if_running  # 如果容器在运行就不再去再启动了
      lifecycle-jsmanagement: start_only # 这个参数不会在停机后关闭容器
services:
  mysql:
    image: 'mysql:latest'
    restart: always
    environment:
      - MYSQL_DATABASE=root
      - MYSQL_ROOT_PASSWORD=123456
    ports:
      - '3306:3306'
    profiles:
      - dev
    labels:
      # 特殊参数特殊处理
      org.springframework.boot.jdbc.parameters: useUnicode=true&allowpublicKeyRetrieval=true&characterEncoding=utf-8&useSSL=false
  redis:
    image: 'redis:latest'
    restart: always
    environment:
      - 'REDIS_PASSWORD=redis'
    ports:
      - '6379:6379'
    profiles:
      - dev

docker-compose生命管理器

org.springframework.boot.docker.compose.lifecycle.DockerComposeLifecycleManager 负责整合后的compose生命管理,包括容器创建,启动,停止,以及最后的配置自动发现,其中我们需要的配置也在读取docker-compose.yml后存了下来,

然后在容器启动完成后随之发布事件DockerComposeServicesReadyEvent,在被DockerComposeServiceConnectionsApplicationListener监听到后开始注册配置

    private void registerC编程客栈onnectionDetails(BeanDefinitionRegistry registry, List<RunningService> runningServices) {
         //  runningServices是前面docker-compose中写的service
        for (RunningService runningService : runningServices) {
            DockerComposeConnectionSource source = new DockerComposeConnectionSource(runningService);
            // getConnectionDetails获取`spring.factories`里面注册的`ConnectionDetailsFactory`具体实现
            this.factories.getConnectionDetails(source, false).forEach((connectionDetailsType, connectionDetails) -> {
                register(registry, runningService, connectionDetailsType, connectionDetails);
                this.factories.getConnectionDetails(connectionDetails, false)
   http://www.devze.com                 .forEach((adaptedType, adaptedDetails) -> register(registry, runningService, adaptedType,
                            adaptedDetails));
            });
        }
    };
        }
    }

最后跟以前的配置自动发现一样由org.springframework.boot.autoconfigure.service.connection.ConnectionDetails

这个接口负责,比如其中关于mysqldocker-compose整合的实现类为org.springframework.boot.docker.compose.service.connection.mysql.MySqlJdbcDockerComposeConnectionDetailsFactory

注意事项

  • skip 需要搭配 lifecycle-management使用

  • 常规的environment 参数由SpringBoot自动发现,一些特殊参数,比如mysql连接参数可以放到labels下面,格式类似于org.springframework.boot.jdbc.parameters,具体的话可以翻看源码,一般会直接定义在ConnectionDetails实现类里面

到此这篇关于SpringBoot3 整合Docker-Compose的实现步骤的文章就介绍到这了,更多相关SpringBoot3 整合Docker-Compose内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)! 

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新开发

开发排行榜