docker+jib集成springboot+mysql项目
第一步:在 IDE 上快速创建一个 springboot 项目(博主用的是 IDEA)
项目依赖org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-data-jpa com.alibaba druid 1.1.10 com.alibaba druid-spring-boot-starter 1.1.10 mysql mysql-connector-java org.projectlombok lombok true
项目结构:
第二步:快速创建一个 UserMsg 的实体类
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import lombok.Getter; import lombok.Setter; import javax.persistence.*; import java.io.Serializable; /** * @author chaoyou * @email * @date 2020-8-27 18:51 * @Description * @Reference */ @Entity @Table(name = "user_msg") @Setter @Getter @JsonIgnoreProperties({"handler", "hibernateLazyInitializer"}) public class UserMsg implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @Column private String username; @Column private String password; public UserMsg() { } public UserMsg(String username, String password) { this.username = username; this.password = password; } }UserMsg实体类
import lombok.Getter; import lombok.Setter; /** * @author chaoyou * @email * @date 2020-8-27 19:11 * @Description * @Reference */ @Setter @Getter public class UserMsgForm { private String username; private String password; }UserMsg表单类
第三步:UserMsg 实体类的 dao 层
import com.example.demo.model.UserMsg; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; import java.io.Serializable; /** * @author chaoyou * @email * @date 2020-8-27 18:55 * @Description * @Reference */ public interface UserMsgDao extends CrudRepositoryDao 层{ @Query(value = "select * from user_msg where username = ?1", nativeQuery = true) UserMsg getByUsername(String username); UserMsg getByUsernameAndPassword(String username, String password); }
第四步:UserMsg 实体类的 service 层
import com.example.demo.model.UserMsg; /** * @author chaoyou * @email * @date 2020-8-27 18:57 * @Description * @Reference */ public interface UserMsgService { public String save(UserMsg userMsg); public Object get(String username); }service
import com.example.demo.dao.UserMsgDao; import com.example.demo.model.UserMsg; import com.example.demo.service.UserMsgService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * @author chaoyou * @email * @date 2020-8-27 18:58 * @Description * @Reference */ @Service public class UserMsgServiceImpl implements UserMsgService { private UserMsgDao dao; @Autowired public UserMsgServiceImpl(UserMsgDao dao) { this.dao = dao; } @Override public String save(UserMsg userMsg) { String reuslt = ""; try { UserMsg userMsg1 = dao.getByUsernameAndPassword(userMsg.getUsername(), userMsg.getPassword()); if (userMsg1 != null){ return "【username=" + userMsg.getUsername() + ", password=" + userMsg.getPassword() + "】账号信息已经存在!"; } dao.save(new UserMsg(userMsg.getUsername(), userMsg.getPassword())); reuslt = "success"; } catch (Exception e){ reuslt = "新建账号失败!"; } return reuslt; } @Override public Object get(String username) { Object reuslt = ""; try { reuslt = dao.getByUsername(username); } catch (Exception e){ reuslt = "查询账户信息失败!"; } return reuslt; } }service 实现类
第五步:UserMsg 实体类的 controller 层
import com.example.demo.model.UserMsg; import com.example.demo.model.form.UserMsgForm; import com.example.demo.service.UserMsgService; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.util.ObjectUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author chaoyou * @email * @date 2020-8-27 19:09 * @Description * @Reference */ @RestController @RequestMapping("/user") public class UserMsgController { @Autowired private UserMsgService service; @PostMapping(value = "/save", produces = "application/json;charset=utf-8") public ResponseEntity save(UserMsgForm userMsgForm){ String result = ""; if (ObjectUtils.isEmpty(userMsgForm)){ result = "账号信息不能为空!"; } else { UserMsg userMsg = new UserMsg(); BeanUtils.copyProperties(userMsgForm, userMsg, "id"); result = service.save(userMsg); } return ResponseEntity.ok(result); } @GetMapping(value = "/get", produces = "application/json;charset=utf-8") public ResponseEntity get(String username){ Object result = ""; if (ObjectUtils.isEmpty(username)){ result = "账号信息不能为空!"; } else { result = service.get(username); } return ResponseEntity.ok(result); } }controller
第六步:设置运行环境配置文件
server.address=0.0.0.0 server.port=8080 server.tomcat.uri-encoding=UTF-8 server.connection-timeout=1000000 server.error.whitelabel.enabled=false server.max-http-header-size=102400 spring.datasource.name=mysql spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.druid.filters=stat spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.druid.url=jdbc:mysql://localhost:3306/db?useSSL=false&useUnicode=true&characterEncoding=utf-8&characterSetResults=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&autoReconnect=true&failOverReadOnly=false&createDatabaseIfNotExist=true&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true spring.datasource.druid.username=root spring.datasource.druid.password=123456 spring.datasource.druid.initial-size=6 spring.datasource.druid.min-idle=2 spring.datasource.druid.max-active=700 spring.datasource.druid.max-wait=600000 spring.datasource.druid.time-between-eviction-runs-millis=600000 spring.datasource.druid.min-evictable-idle-time-millis=3000000 spring.datasource.druid.validation-query=SELECT 'x' spring.datasource.druid.test-while-idle=true spring.datasource.druid.test-on-borrow=false spring.datasource.druid.test-on-return=false spring.datasource.druid.pool-prepared-statements=false spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20 spring.devtools.restart.additional-paths=src/main/java spring.devtools.restart.exclude=static/**,public/** spring.output.ansi.enabled=always spring.mvc.throw-exception-if-no-handler-found=true spring.mvc.static-path-pattern=/** spring.resources.add-mappings=true spring.resources.chain.strategy.content.enabled=true spring.resources.chain.strategy.content.paths=/** spring.servlet.multipart.max-file-size=128KB spring.servlet.multipart.max-request-size=128KB spring.servlet.multipart.enabled=false ## springdata \uFFFD\uFFFD\uFFFD\u00F2\uFFFD\uFFFD\uFFFD ## jpa 开启驼峰命名规则 spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl ## hibernate 自动建表 spring.jpa.hibernate.ddl-auto=update # 控制台打印 sql 语句 spring.jpa.show-sql=true # 数据库方言 spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect debug=false trace=false spring.jpa.open-in-view=true # 解决 hibernate 懒加载问题 spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true #spring.jackson.mapper.use-std-bean-naming=true #// 允许字段命名不规范 解决首字母大写的问题(不转小写) 犹豫之前对接的都是小写 所以这个属性不放开 沿用原来的对接 # json 转对象 忽略大小写问题 spring.jackson.mapper.ACCEPT_CASE_INSENSITIVE_PROPERTIES=true # 日志规范 logging.level.org.hibernate.sql=DEBUG logging.level.org.hibernate.type=ERROR logging.level.org.springframework.web=ERROR logging.path=build/log logging.level.root=warn logging.pattern.console=%d{yyyy-MM-dd} [%thread] %-5level %logger{50} - %msg%n logging.level.cc=error spring.http.encoding.force=true spring.http.encoding.charset=UTF-8 spring.http.encoding.enabled=true # 设置多线程系统参数 book.core.poolsize=20 book.max.poolsize=30 book.queue.capacity=300 book.keepAlive.seconds=100 book.thread.name.prefix=taskExecutorWbswryxx-application-dev.properties
# 设置项目的运行环境(dev:开发、prod:生产、test:测试) spring.profiles.active=dev #spring.profiles.active=test #spring.profiles.active=prodapplication.properties
第七步:在 pom.xml 中设置 jib-maven-plugin 插件信息
<?xml version="1.0" encoding="UTF-8"?>pom.xmlxsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.6.RELEASE com.example demo 0.0.1-SNAPSHOT demo Demo project for Spring Boot UTF-8 1.8 1.8 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-data-jpa com.alibaba druid 1.1.10 com.alibaba druid-spring-boot-starter 1.1.10 mysql mysql-connector-java org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test io.projectreactor reactor-test test org.springframework.boot spring-boot-maven-plugin com.google.cloud.tools jib-maven-plugin 1.3.0 openjdk:8-jdk-stretch chaoyou/${project.name}:${project.version} -Xms128m -Xmx128m 8080 true
第八步:把项目推送到 Linux 上
第九步:在 docker 上跑一个 mysql 容器
##搜索 mysql 镜像 ——docker search mysql8.0 ##拉取 mysql8.0 镜像 ——docker pull mysql8.0 ##跑 mysql 镜像 ——docker run -d -p 3306:3306 --name mysqltest -e MYSQL_ROOT_PASSWORD=123456 docker.io/mysql:8.0 ##进入 mysqltest 容器内部 ——docker exec -it mysqltest /bin/bash ##查看容器内部ip ——cat /etc/hosts
第十步:进入项目的配置文件中修改数据库的 ip 地址
#进入application-dev.properties ——cd src/main/resources #修改 application-dev.properties 信息(把ip换成mysqltest容器的内部ip) ——vi application.properties
第十一步:进入到该项目的 pom.xml 同级目录下
接下来利用 jib 插件对项目进行打包成镜像包(jib-image.tar),有个前提 Linux 环境有安装 jdk 和 maven(安装参考:https://www.cnblogs.com/chaoyou/p/13572888.html)
#打包命令
[root@localhost demo]# mvn compile jib:buildTar
打包完成进入 target 目录下就可以看到镜像包( jib-image.tar )
第十二步:把镜像包( jib-image.tar )推进 docker 镜像库( docker images )
##把镜像包( jib-image.tar )推进 docker 仓库
[root@localhost target]# docker load < jib-image.tar
##把 docker 镜像库中的镜像打包成 tar 镜像包
[root@localhost target]# docker save -o /chaoyou/demo.tar chaoyou/demo:0.0.1-SNAPSHOT
第十三步:跑 chaoyuou/demo 镜像
#跑镜像
——docker run --rm -p 8080:8080 chaoyou/demo:0.0.1-SNAPSHOT