小知识:Docker Compose部署微服务项目上线功能

一、需求说明

编写一个SpringBoot + Redis 的微服务项目,并提供 hello接口,每访问一次接口,计数器+1

二、效果

%小知识:Docker Compose部署微服务项目上线功能-猿站网-插图

三、项目结构

%小知识:Docker Compose部署微服务项目上线功能-1猿站网-插图

目录说明

dockercompose.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”]

dockercompose.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部署微服务项目上线功能-2猿站网-插图

执行命令启动

docker-compose up

%小知识:Docker Compose部署微服务项目上线功能-3猿站网-插图

部署成功后我们查看服务是否启动

docker ps

%小知识:Docker Compose部署微服务项目上线功能-4猿站网-插图

本机进行访问

curl localhost:8080/hello

%小知识:Docker Compose部署微服务项目上线功能-5猿站网-插图

小结

以上就是【Bug 终结者】对 【云原生】Docker Compose 进阶 – 部署微服务项目上线 的简单介绍 ,Compose 部署项目,实战练习,进阶!

原文地址:https://blog.csdn.net/weixin_45526437/article/details/125520064

声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。
建站知识

小知识:windows 10安装和使用docker

2023-3-6 12:59:34

建站知识

小知识:docker搭建memcached的详细步骤

2023-3-6 13:06:32

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索