Docker Compose部署微服務項目上線功能
一、需求說明
編寫一個SpringBoot + Redis 的微服務項目,並提供 hello接口,每訪問一次接口,計數器+1
二、效果圖
三、項目結構
目錄說明
docker-compose.yml :項目的啟動文件,配置編排等
Dockerfile:項目上線所需要的依賴,以及啟動方式
四、核心源碼
♻️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相關源碼
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"
五、部署項目
打包後端項目通過命令
mvn clean package
jar包與配置文件上傳至Linux服務器,新建指定文件夾(通過Filezilla上傳文件)
執行命令啟動
docker-compose up
部署成功後我們查看服務是否啟動
docker ps
本機進行訪問
curl localhost:8080/hello
⛵小結
以上就是【Bug 終結者】對 【雲原生】Docker Compose 進階 – 部署微服務項目上線 的簡單介紹 ,Compose 部署項目,實戰練習,進階!
推薦閱讀:
- Java Mybatis框架由淺入深全解析下篇
- 使用SpringBoot打jar包並部署到Tomcat詳細步驟
- Java Mybatis框架由淺入深全解析上篇
- springboot實現啟動直接訪問項目地址
- 教你使用idea搭建ssm詳細教程(Spring+Spring Mvc+Mybatis)