当前位置:网站首页>微服务入门
微服务入门
2022-07-04 12:42:00 【华为云】
微服务
当今的应用程序开发和 IT 系统管理由云驱动。 新式云应用程序需要是快速、敏捷、可大规模缩放且可靠的。
微服务体系结构是一种将大型应用程序分解为一组较小的服务的方法。 每个服务都在自己的进程中运行,并使用 HTTP/HTTPS、WebSocket 或 AMQP 等协议与其他进程进行通信。 每个微服务在特定的上下文边界内实现特定的端到端域或业务功能,每个微服务都必须自主开发,并且可以独立部署。 最后,每个微服务应拥有其相关的域数据模型和域逻辑,并且可以基于不同的数据存储技术(SQL、NoSQL)和不同的编程语言。
可以认为,微服务是一种思想,将以往的那种大型整体单体应用拆分成一个个可以独立运行,部署的服务,这里其中的拆分没有详细的划分,一般根据业务场景一个服务就负责实现一个业务功能,而各个服务之间则通过HTTPS/HTTP等协议进行通信(最常使用Restful API)。这就是微服务的基本思想。
当然微服务在实际使用过程中,还会有相当多的延申问题。
容器化是软件开发的一种方法,通过该方法可将应用程序或服务、其依赖项及其配置(抽象化为部署清单文件)一起打包为容器映像。 可以将容器化应用程序作为一个单元进行测试,并将它们作为容器映像实例部署到主机操作系统 (OS)。因此使用容器,是实现微服务体系结构模式的一种有效方法。
典型的微服务架构如下图:
Docker
Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从 Apache2.0 协议开源。Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口(类似 iPhone 的 app),更重要的是容器性能开销极低。Docker 是一个用于开发,交付和运行应用程序的开放平台。Docker 使您能够将应用程序与基础架构分开,从而可以快速交付软件。借助 Docker,您可以与管理应用程序相同的方式来管理基础架构。通过利用 Docker 的方法来快速交付,测试和部署代码,您可以大大减少编写代码和在生产环境中运行代码之间的延迟。
docker 使用
- 安装docker
- Dockerfile
fsutil file createnew Dockerfile 0
命令创建名为 Dockerfile 的文件
并添加如下内容:
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS buildWORKDIR /srcCOPY backend.csproj .RUN dotnet restoreCOPY . .RUN dotnet publish -c release -o /app
这将在调用时按顺序执行以下步骤:
- 拉取 mcr.microsoft.com/dotnet/sdk:6.0 映像并将其命名为 build
- 将映像中的工作目录设置为 /src
- 将在本地找到的名为 backend.csproj 的文件复制到刚创建的 /src 目录中
- 在项目中调用 dotnet restore
- 将本地工作目录中的所有内容复制到映像中
- 在项目中调用 dotnet publish
FROM mcr.microsoft.com/dotnet/aspnet:6.0WORKDIR /appEXPOSE 80EXPOSE 443COPY --from=build /app .ENTRYPOINT ["dotnet", "backend.dll"]
这将在调用时按顺序执行以下步骤:
- 拉取 mcr.microsoft.com/dotnet/aspnet:6.0 映像
- 将映像中的工作目录设置为 /app
- 公开端口 80 和 443
- 将上面创建的 build 映像的 /app 目录中的所有内容复制到此映像的应用目录中
- 将此映像的入口点设置为 dotnet,并将 backend.dll 作为参数传递
- 构建映像
docker build -t mymicroservice .
docker build 命令使用 Dockerfile 构建Docker 映像。
- -t mymicroservice 参数指示它将图像标记(命名)为 mymicroservice。
- 最后一个参数告知它要使用哪个目录来查找 Dockerfile (. 指定当前目录)。
- 此命令将下载并生成所有依赖项以创建 Docker 映像,此操作可能需要一些时间。
- 查看构建结果,并运行
docker images
计算机上可用的所有映像的列表,包括刚刚创建的映像
docker run -it --rm -p 3000:80 --name mymicroservicecontainer mymicroservice
运行刚才创建的映像
docker ps
查看运行的容器
边栏推荐
- Golang sets the small details of goproxy proxy proxy, which is applicable to go module download timeout and Alibaba cloud image go module download timeout
- Play Sanzi chess easily
- 6 分钟看完 BGP 协议。
- [FAQ] summary of common causes and solutions of Huawei account service error 907135701
- 面试官:Redis 过期删除策略和内存淘汰策略有什么区别?
- 8个扩展子包!RecBole推出2.0!
- A data person understands and deepens the domain model
- 阿里云有奖体验:用PolarDB-X搭建一个高可用系统
- WPF双滑块控件以及强制捕获鼠标事件焦点
- Besides, rsync+inotify realizes real-time backup of data
猜你喜欢
[Android kotlin] lambda return statement and anonymous function
Understand bloomfilter in one article
美团·阿里关于多模态召回的应用实践
After the game starts, you will be prompted to install HMS core. Click Cancel, and you will not be prompted to install HMS core again (initialization failure returns 907135003)
Efficient! Build FTP working environment with virtual users
Golang sets the small details of goproxy proxy proxy, which is applicable to go module download timeout and Alibaba cloud image go module download timeout
干货整理!ERP在制造业的发展趋势如何,看这一篇就够了
从0到1建设智能灰度数据体系:以vivo游戏中心为例
PostgreSQL 9.1 soaring Road
PostgreSQL 9.1 飞升之路
随机推荐
Understand bloomfilter in one article
光环效应——谁说头上有光的就算英雄
认知的定义
It is six orders of magnitude faster than the quantum chemical method. An adiabatic artificial neural network method based on adiabatic state can accelerate the simulation of dual nitrogen benzene der
AI 绘画极简教程
[FAQ] summary of common causes and solutions of Huawei account service error 907135701
Etcd storage, watch and expiration mechanism
Two dimensional code coding theory
Comprehensive evaluation of modular note taking software: craft, notation, flowus
AbstractDispatcherServletInitializer 的实现类为什么可以在初始化Web容器的时候被调用
C language array
WPF双滑块控件以及强制捕获鼠标事件焦点
Comparative study of the gods in the twilight Era
CVPR 2022 | transfusion: Lidar camera fusion for 3D target detection with transformer
游戏启动后提示安装HMS Core,点击取消,未再次提示安装HMS Core(初始化失败返回907135003)
Interviewer: what is the difference between redis expiration deletion strategy and memory obsolescence strategy?
室外LED屏幕防水吗?
C#/VB. Net to add text / image watermarks to PDF documents
Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
Is the main thread the same as the UI thread- Is main thread the same as UI thread?