graphqlize docker-compose 运行

今天有写过简单的graphqlize 基本运行,但是对于使用容器的不太方便,所以基于profile 调整了
添加容器构建参数替换

原理说明

主要是通过profile 以及mvn 的resource 插件进行代码的构建时替换datasource 的配置,对于不同的环境使用不同的配置

pom.xml

 
<?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>2.2.5.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository -->
 </parent>
 <groupId>com.example</groupId>
 <artifactId>demo</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <name>demo</name>
 <description>Demo project for Spring Boot</description>
 <properties>
  <java.version>1.8</java.version>
 </properties>
 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-jpa</artifactId>
  </dependency>
  <dependency>
   <groupId>org.graphqlize</groupId>
   <artifactId>graphqlize-java</artifactId>
   <version>0.1.0-alpha4</version>
  </dependency>
  <dependency>
   <groupId>org.postgresql</groupId>
   <artifactId>postgresql</artifactId>
   <version>42.2.10</version>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
   <exclusions>
    <exclusion>
     <groupId>org.junit.vintage</groupId>
     <artifactId>junit-vintage-engine</artifactId>
    </exclusion>
   </exclusions>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
 </dependencies>
 <build>
  <finalName>graphqlize-${project.version}</finalName>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
   <plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <configuration>
     <encoding>utf-8</encoding>
     <useDefaultDelimiters>true</useDefaultDelimiters>
     <resources>
      <resource>
       <directory>src/main/resources</directory>
       <filtering>true</filtering>
      </resource>
     </resources>
    </configuration>
   </plugin>
  </plugins>
 </build>
 <profiles>
  <profile>
   <id>local</id>
   <properties>
                <pg-data-source>jdbc:postgresql://localhost:5432/postgres</pg-data-source>
    <pg-username>postgres</pg-username>
    <pg-password>dalong</pg-password>
   </properties>
  </profile>
  <profile>
   <id>docker</id>
   <properties>
    <pg-data-source>jdbc:postgresql://pg:5432/postgres</pg-data-source>
    <pg-username>postgres</pg-username>
    <pg-password>dalong</pg-password>
   </properties>
  </profile>
 </profiles>
</project>

datasource 配置

spring.datasource.url=${pg-data-source}
spring.datasource.username=${pg-username}
spring.datasource.password=${pg-password}

容器dockerfile

基础镜像添加了arthas 支持方便调试

FROM dalongrong/alpine-oraclejdk8:8.131.11-slim-arthas
VOLUME /tmp
ADD target/graphqlize-0.0.1-SNAPSHOT.jar app.jar
RUN sh -c 'touch /app.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

docker-compose 使用

version: "3"
services:
   app:
      build:
         context: ./
         dockerfile: Dockerfile
      ports: 
      - "8080:8080"
   pg:
      image: postgres:11-alpine
      ports:
      - "5432:5432"
      environment:
      - "POSTGRES_PASSWORD=dalong"

说明

以上的代码以及运行都是很简单的,主要是一个工具的集成

参考资料

https://hub.docker.com/repository/docker/dalongrong/alpine-oraclejdk8
https://github.com/graphqlize/graphqlize
https://github.com/rongfengliang/graphqlize-docker-compose

posted on 2020-03-19 23:23  荣锋亮  阅读(322)  评论(0编辑  收藏  举报

导航