Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deploy Java microservices with one click of Docker-Compose

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to use Docker-Compose to deploy Java micro-services. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Docker Compose to easily and efficiently manage containers and define multiple containers to run

Official introduction

Define and run multiple containers. YAML file configuration file. Single command . What are the orders? Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application's services. Then, with a single command, you create and start all the services from your configuration. To learn more about all the features of Compose, see the list of features. Compose can be used in all environments. Compose works in all environments: production, staging, development, testing, as well as CI workflows. You can learn more about each case in Common Use Cases. Three steps: Using Compose is basically a three-step process:

Define your app's environment with a Dockerfile so it can be reproduced anywhere.

Dockerfile ensures that our project can be run anywhere.

Define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment.

Services what is a service?

Docker-compose.yml how to write this file!

Run docker-compose up and Compose starts and runs your entire app.

Start the project

Function: batch container layout.

Understand

Compose is the official open source project of Docker. Need to install! Dockerfile lets the program run anywhere. Web service. Redis 、 mysql 、 nginx... Multiple containers. Run Compose

Version: # optional since v1.27.0services: web: build:. Ports:-"5000 code 5000" volumes: -.: / code-logvolume01:/var/log links:-redis redis: image: redisvolumes: logvolume01: {}

Docker-compose up 100th service. Compose: an important concept. Service services, container. Application. (web, redis, mysql....) Project project. A set of associated containers. Blog. Web 、 mysql .

Download and install Docker-Compose

Download Docker-Compose

Sudo curl-L "https://github.com/docker/compose/releases/download/1.28.6/docker-compose-$(uname-s)-$(uname-m)"-o / usr/local/bin/docker-compose# this may be faster! Curl-L https://get.daocloud.io/docker/compose/releases/download/1.28.6/docker-compose-`uname-s`-`uname-m` > / usr/local/bin/docker-compose

Set permissions

Sudo chmod + x / usr/local/bin/docker-compose

Test, enter docker-compose in any directory

Official experience

Python application. Counter. Redis!

Official website address: https://docs.docker.com/compose/gettingstarted/

It is necessary to install the latest version of docker-compose, otherwise the version will be reported as incompatible.

1. App.py 2 and Dockerfile applications are packaged into image 3 and Docker-compose yaml files (define the entire service and the required environment. Web, redis) complete online service! 4. Start the compose project (docker-compose up)

Process flow

1. Create network 2, execute Docker-compose yaml 3, and start the service.

Official website process

Create a folder

Mkdir composetest cd composetest

Create app.py

Import timeimport redisfrom flask import Flaskapp = Flask (_ _ name__) cache = redis.Redis (host='redis' Port=6379) def get_hit_count (): retries = 5 while True: try: return cache.incr ('hits') except redis.exceptions.ConnectionError as exc: if retries = = 0: raise exc retries-= 1 time.sleep (0.5) @ app.route (' /') def hello (): count = get_hit_count () return 'Hello World! I have been seen {} times.\ n'.format (count)

Create requirements.txt

Flaskredis

Create Dockerfile

FROM python:3.7-alpineWORKDIR / codeENV FLASK_APP=app.pyENV FLASK_RUN_HOST=0.0.0.0RUN apk add-- no-cache gcc musl-dev linux-headersCOPY requirements.txt requirements.txtRUN pip install-r requirements.txtEXPOSE 5000COPY. .CMD ["flask", "run"]

Create docker-compose.yml

Version: "3.9" services: web: build: Ports:-"5000 redis:alpine" redis: image: "redis:alpine"

Run docker-compose up

Successful screenshot:

Java micro-service project actual combat counter deployed using DockerCompose

Create a new project to complete the counting function

Package com.atxiaodei.hellodockercompose.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * @ ClassName HelloController * @ Deacription TODO * @ Author Wang Meng * @ Date 18:58 on 2021-4-6 * @ Version 1.0 * * / @ RestControllerpublic class HelloController {@ Autowired StringRedisTemplate redisTemplate @ RequestMapping ("/ hello") public String helloCount () {Long view = redisTemplate.opsForValue () .increment ("view"); return "deploy Java microservice counter function using dockercompose, view" + view;}}

Write Dockerfile

FROM java:8COPY * .jar / app.jarCMD ["--server.port=8080"] EXPOSE 8080ENTRYPOINT ["java", "- jar", "/ app.jar"]

Write docker-compose.yml

Version: '3.9'services: wangmengapp: build: .image: wangmengapp depends_on:-redis ports:-"8080 wangmengapp 8080" redis: image: "redis:alpine"

Upload files to the server

Execute docker-compose up

Successful screenshot

Summary:

As long as there are docker-compose files for future projects. According to this rule, start the choreography container.!

Company: docker-compose. Start it directly.

Online open source project: docker-compose with one click.

Suppose the project is going to redeploy packaged docker-compose up-- build # rebuild!

On how to use Docker-Compose click to deploy java micro services to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report