微服務下 Spring Boot 應用多環境發佈

img

說明

解決微服務在多套環境發佈的問題,解決開發環境和預發環境切換的問題,支持 N 套開發環境水平擴展。java

img

Spring Boot Dockerfile 通用模板

FROM registry.cn-hangzhou.aliyuncs.com/micro-java/openjdk:8-jre-alpine
MAINTAINER 545544032@qq.com
ENV TZ="Asia/Shanghai" JVM_PARAMS=" " SPRING_PARAMS=" "
ADD target/*.jar /server.jar
EXPOSE 8080
CMD java $JVM_PARAMS -Djava.security.egd=file:/dev/./urandom -jar /server.jar $SPRING_PARAMS

Note:registry.cn-hangzhou.aliyuncs.com/micro-java/openjdk:8-jre-alpine 基於鏡像 openjdk:8-jre-alpine,額外安裝了工具 curl,用於 docker service 的 health checkspring

開發/測試環境 docker stack 通用模板

version: '3.4'
services:
    "{{ service_name }}":
         image: "{{ registry_url }}/{{ image_name }}:{{ image_version }}"
         environment:
           JVM_PARAMS: "{{ jvm_heap }}"            # 例如: -Xmx800m -Xms800m
           SPRING_PARAMS: " {{ spring_params }}"
         networks:
           - overlay-network
         volumes:
           - type: bind
             source: "/data/spring-boot/{{ service-name }}/application.properties"
             target: /application.properties
         stop_grace_period: 30s 
         deploy:
           replicas: 2
           resources:
             limits:
               memory: "{{ memory_limit }}"              # 例如 : 1024MB
           update_config:
             parallelism: 1            
             delay: 0s                 
             failure_action: rollback  
             max_failure_ratio: 0      
             order: stop-first
           restart_policy:
             condition: on-failure
             max_attempts: 3
         healthcheck:
           test: "curl 127.0.0.1:8080 || exit 1"
           interval: 3s
           timeout: 5s
           retries: 3
           start_period: 1m

networks:
  overlay-network:
    driver: overlay
    attachable: true

Note:此爲 Ansible 中的動態模板,須要先根據環境變量渲染成具體的服務發佈時用的 stack.yml 文件docker


待續。。。app

© 著做權歸做者全部,轉載或內容合做請聯繫做者dom

掃碼關注curl

本文由博客一文多發平臺 OpenWrite 發佈!jvm

相關文章
相關標籤/搜索