首页 > 编程语言 >docker-compose 部署java微服务项目

docker-compose 部署java微服务项目

时间:2023-06-29 10:46:41浏览次数:51  
标签:compose java network spot application docker yml

1、准备条件:安装docker,安装docker-compose,docker安装可自行百度,docker-compose安装由于太慢,我这里提供两个版本:win和linux版本的百度网盘版,大家可根据需要自行下载:

链接:https://pan.baidu.com/s/10W81TX6cWQqyi92xyeuZQQ
提取码:2evg

这里一linux系统为例:下载docker-compose-linux-x86_64后上传到linux 的/usr/bin 下,然后使用 mv docker-compose-linux-x86_64 docker-compose 命名后,使用chmod +x docker-compose 赋予该文件执行权限,再复制一份到/usr/local/bin下,至此,你可以使用docker-compose --version 验证docker-compose是否安装ok

 

2、jar包准备:新建一个文件夹,如docker_images,将需要的jar 以 文件夹 + jar + 该jar 的配置文件的形式,如spot-contract/spot-contract.jar 、spot-contract/application.yml,之后编写统一的Dockerfile文件:

FROM openjdk:8
WORKDIR /javaxh_docker/jdk
VOLUME /tmp
ARG JAR_FILE=./*.jar
ADD ${JAR_FILE} app.jar
ENTRYPOINT ["java", "-Dfile.encoding=UTF8", "-Duser.timezone=GMT+08", "-jar", "app.jar"]

将该Dockerfile复制一份到 spot-contract下,形成如下目录结构

|-docker_images

|----spot-contract

|--------spot-contract.jar

|--------Dockerfile

|--------application.yml

|----spot-base

|--------spot-base.jar

|--------Dockerfile

|--------application.yml

|----docker-compose.yml

使用docker build -t spot-contract:latest spot-contract/ 构建镜像

其他服务遵循同样的方式

3.编写docker-compose.yml

version: '3.3'
services:
  web-sichuan:
    image: nginx:latest
    networks:
      - sichuan-network
    environment:
      - TZ=Asia/Shanghai
    ports:
      - "18089:10881"
    volumes:
      - "./sichuan-web/sichuan/:/usr/share/nginx/html"
      - "./sichuan-web/conf:/etc/nginx/conf.d"
  hdpm-platform-sc:
    image: tomcat:sc
    networks:
      - sichuan-network
    environment:
      - TZ=Asia/Shanghai
    ports:
      - "19288:9090"
    volumes:
      - "./tomcat-sc/apache-tomcat-sc/webapps/:/javaxh_docker/jdk/apache-tomcat-sc/webapps/"
      - "./tomcat-sc/apache-tomcat-sc/conf/:/javaxh_docker/jdk/apache-tomcat-sc/conf"
  spot-public-data-sichuan:
    image: spot-public-data:sc
    networks:
      - sichuan-network
    environment:
      - TZ=Asia/Shanghai
    command: ["--spring.profiles.active=prod"]
    ports:
      - "19224:8224"
    volumes:
      - "./spot-public-data-sichuan/application.yml:/javaxh_docker/jdk/config/application-prod.yml"
  eureka-server:
    image: eureka:common
    networks:
      - sichuan-network
      - fujian-network
      - mengxi-network
    environment:
      - TZ=Asia/Shanghai
    command: [ "--spring.profiles.active=prod" ]
    ports:
      - "18761:8761"
    volumes:
      - "./eureka/application-common.yml:/javaxh_docker/jdk/config/application-prod.yml"
  gateway:
    image: gateway:common
    networks:
      - sichuan-network
      - fujian-network
      - mengxi-network
    environment:
      - TZ=Asia/Shanghai
    command: [ "--spring.profiles.active=prod" ]
    ports:
      - "18769:8769"
    volumes:
      - "./gateway/application.yml:/javaxh_docker/jdk/config/application-prod.yml"
  process-common:
    image: hdpm-process:common
    networks:
      - sichuan-network
      - fujian-network
      - mengxi-network
    environment:
      - TZ=Asia/Shanghai
    command: [ "--spring.profiles.active=prod" ]
    ports:
      - "18024:8024"
    volumes:
      - "./process/mx/application.yml:/javaxh_docker/jdk/config/application-prod.yml"
  spot-trading-result-sichuan:
    image: spot-trading-result:sc
    networks:
      - sichuan-network
      - fujian-network
      - mengxi-network
    environment:
      - TZ=Asia/Shanghai
    command: [ "--spring.profiles.active=prod" ]
    ports:
      - "18191:8191"
    volumes:
      - "./spot-trading-result/spot-trading-result-sichuan/application.yml:/javaxh_docker/jdk/config/application-prod.yml"
  configserver:
    image: configserver:common
    networks:
      - sichuan-network
      - fujian-network
      - mengxi-network
    environment:
      - TZ=Asia/Shanghai
    command: [ "--spring.profiles.active=prod" ]
    ports:
      - "18090:8090"
    volumes:
      - "./configserver/application.yml:/javaxh_docker/jdk/config/application-prod.yml"
  spot-day-ahead:
    image: spot-day-ahead:common
    networks:
      - sichuan-network
      - fujian-network
      - mengxi-network
    environment:
      - TZ=Asia/Shanghai
    command: [ "--spring.profiles.active=prod" ]
    ports:
      - "18084:8084"
    volumes:
      - "./spot-day-ahead/application.yml:/javaxh_docker/jdk/config/application-prod.yml"
  spot-contract:
    image: spot-contract:common
    networks:
      - sichuan-network
      - fujian-network
      - mengxi-network
    environment:
      - TZ=Asia/Shanghai
    command: [ "--spring.profiles.active=prod" ]
    ports:
      - "18755:8755"
    volumes:
      - "./spot-contract/application.yml:/javaxh_docker/jdk/config/application-prod.yml"
  spot-base-data:
    image: spot-base-data:common
    networks:
      - sichuan-network
      - fujian-network
      - mengxi-network
    environment:
      - TZ=Asia/Shanghai
    command: [ "--spring.profiles.active=prod" ]
    ports:
      - "18755:8755"
    volumes:
      - "./spot-base-data/application.yml:/javaxh_docker/jdk/config/application-prod.yml"

networks:
  sichuan-network: { }
  fujian-network: { }
  mengxi-network: { }

4、服务的启动与关闭

docker-compose -f docker-compose.yml  up -d 启动所有服务

docker-compose -f docker-compose.yml  down 关闭所有服务

docker-compose -f docker-compose.yml  up -d spot-base 只启动spot-base服务

docker-compose -f docker-compose.yml  restart  spot-base 重启spot-base服务

docker-compose -f docker-compose.yml  start spot-base 启动spot-base服务(需要先执行过up操作)

docker-compose -f docker-compose.yml  stop spot-base 停止spot-base服务

5、关于服务配置文件

eureka:
  client:
    serviceUrl:
      defaultZone: http://eureka-server:8761/eureka/
    healthcheck:
      enabled: true # 开启健康检查(需要spring-boot-starter-actuator依赖),配置项必须设置在application.yml中
  instance:
    lease-renewal-interval-in-seconds: 30    # 续约更新时间间隔(默认30秒)
    lease-expiration-duration-in-seconds: 90 # 续约到期时间(默认90秒)

由于使用的是docker容器,docker-compose可通过服务名来通讯,也可使用127.0.0.1:映射到宿主机的端口访问

标签:compose,java,network,spot,application,docker,yml
From: https://www.cnblogs.com/g177w/p/17513336.html

相关文章

  • JavaScript 链表的增删改查
       //节点对象classNode{constructor(data){this.data=data;//存储节点数据this.next=null;//存储下一个节点的引用,默认为null}}//链表对象classLinkedList{constructor(){this.head=null;//链表头节点,默认为null}......
  • java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check t
    问题报错代码org.apache.ibatis.exceptions.PersistenceException:###Errorqueryingdatabase.Cause:java.sql.SQLSyntaxErrorException:YouhaveanerrorinyourSQLsyntax;checkthemanualthatcorrespondstoyourMySQLserverversionfortherightsyntax......
  • Java 13 新特性
    如题:文本块(TextBlocks):Java13引入了文本块,或称为多行字符串。使用三个双引号(""")可以定义一个文本块,这样可以更方便地编写多行字符串,而无需使用转义字符或连接符。这在编写HTML、JSON、SQL等需要包含大量换行和格式的字符串时非常有用。......
  • Java中获取request对象的几种方法
    1.通过注解获取(很简单,推荐):publicclassHello{@AutowiredHttpServletRequestrequest;//这里可以获取到request} 2.在web.xml中配置一个监听:<listener><listener-class>org.springframework.web.context.request.RequestContextListener</listener-class></liste......
  • Java学习——循环结构
    循环结构while循环do...while循环for循环在Java5中引入了一种主要用于数组的增强型for循环breakcontinue一、while循环while(布尔表达式){ //循环内容只要布尔表达式为true,循环就会一直执行下去我们大多数情况是会让循环停止下来的,我们需要一个让表达式失......
  • Java流程控制——Scanner对象
    Scanner对象java.until.Scanner是Java5的新特性,可以通过Scanner类来获取用户的输入,实现计算机与人的交互。基础语法:Scanners=newScanner(System.in);通过Scanner类的next()与nextLine()方法获取输入的字符串在读取之前一般需要使用hasNext()和hasNextLine()判断是否还有输......
  • java第二次blog
    1.前言:4~6次pta题目集难度上升,代码量增加,考察了对类的设计以及如何实现类间关系等。难度较大。涉及到了去重排序循环遍历等。还有API接口,JAVA自带的实现类,包装类,成员方法等,涉及的知识面更广更难。 2.设计分析: 7-1菜单计价程序-3:设计点菜计价程序,根据输入的信息,计算......
  • 【后端面经-Java】AQS详解
    目录1.AQS是什么?2.AQS核心思想2.1基本框架2.1.1资源state2.1.2CLH双向队列2.2AQS模板3.源码分析3.1acquire(int)3.1.1tryAcquire(int)3.1.2addWaiter(Node.EXCLUSIVE)3.1.3acquireQueued(Nodenode,intarg)3.2release(int)3.2.1tryRelease(int)3.2.2unparkSuccess......
  • 【转】Java 单元测试
    Java单元测试在测试方法上添加@Test注解:   ......
  • Linux - Docker日志文件清理
    1.容器日志文件默认存放路径:/var/lib/docker,docker日志文件后缀是containerID+"-json.log",     查看各个日志文件大小:   1) ls-lh$(find/var/lib/docker/containers/-name*-json.log)       查询结果sample:  -rw-r-----1rootroot2......