Docker Compose部署微服務(wù)項(xiàng)目上線功能
一、需求說(shuō)明
編寫(xiě)一個(gè)SpringBoot + Redis 的微服務(wù)項(xiàng)目,并提供 hello接口,每訪問(wèn)一次接口,計(jì)數(shù)器+1
二、效果圖
三、項(xiàng)目結(jié)構(gòu)
目錄說(shuō)明
docker-compose.yml :項(xiàng)目的啟動(dòng)文件,配置編排等
Dockerfile:項(xiàng)目上線所需要的依賴,以及啟動(dòng)方式
四、核心源碼
??Java依賴與接口
依賴文件
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 http://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.5.5</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>org.example</groupId> <artifactId>demo</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <exclusions> <exclusion> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> </resource> <resource> <directory>libs/</directory> <targetPath>libs</targetPath> <includes> <include>**/*.jar</include> </includes> </resource> </resources> </build> </project>
接口
HelloController
package com.wanshi.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * @author whc * @date 2022/6/9 10:06 */ @RestController public class HelloController { @Autowired private StringRedisTemplate redisTemplate; @GetMapping("/hello") public String hello() { Long views = redisTemplate.opsForValue().increment("views"); return "hello, xiaowang, views:" + views; } }
配置文件
application.yml
server: port: 8080 spring: redis: host: redis
??Docker相關(guān)源碼
Dockerfile
FROM java:8 COPY *.jar /app.jar CMD ["--server.port=8080"] EXPOSE 8080 ENTRYPOINT ["java", "-jar", "/app.jar"]
docker-compose.yml
version: '3.8' services: xiwoangapp: build: . image: xiaowangapp depends_on: - redis ports: - "8080:8080" redis: image: "redis:3.0.7"
五、部署項(xiàng)目
打包后端項(xiàng)目通過(guò)命令
mvn clean package
jar包與配置文件上傳至Linux服務(wù)器,新建指定文件夾(通過(guò)Filezilla上傳文件)
執(zhí)行命令啟動(dòng)
docker-compose up
部署成功后我們查看服務(wù)是否啟動(dòng)
docker ps
本機(jī)進(jìn)行訪問(wèn)
curl localhost:8080/hello
?小結(jié)
以上就是【Bug 終結(jié)者】對(duì) 【云原生】Docker Compose 進(jìn)階 – 部署微服務(wù)項(xiàng)目上線 的簡(jiǎn)單介紹 ,Compose 部署項(xiàng)目,實(shí)戰(zhàn)練習(xí),進(jìn)階!
相關(guān)文章
Docker+Nginx打包部署前后端分離步驟實(shí)現(xiàn)
這篇文章主要介紹了Docker+Nginx打包部署前后端分離步驟實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2023-01-01Docker容器之間數(shù)據(jù)傳輸?shù)膶?shí)現(xiàn)
本文主要介紹了Docker容器之間數(shù)據(jù)傳輸?shù)膶?shí)現(xiàn),文中根據(jù)實(shí)例編碼詳細(xì)介紹的十分詳盡,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03寶塔創(chuàng)建Docker容器配置nginx的實(shí)現(xiàn)步驟
本文主要介紹了寶塔創(chuàng)建Docker容器配置nginx的實(shí)現(xiàn)步驟,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-06-06Docker安裝配置Oracle詳細(xì)步驟記錄(以作持久化處理)
docker是一個(gè)用Go語(yǔ)言實(shí)現(xiàn)的開(kāi)源項(xiàng)目,可以讓我們方便的創(chuàng)建和使用容器,下面這篇文章主要給大家介紹了關(guān)于Docker安裝配置Oracle詳細(xì)步驟的相關(guān)資料,需要的朋友可以參考下2024-03-03yum下載rpm以及相關(guān)依賴的方式離線安裝docker
今天小編就為大家分享一篇關(guān)于yum下載rpm以及相關(guān)依賴的方式離線安裝docker,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12