说说如何搭建微服务开发虚拟机环境?

简介: 因为微服务项目一般涉及的工程较多,所以在开发环境会把这些工程部署到虚拟机中,方便测试。首先给出微服务开发虚拟机环境相关软件清单:Vagrant 2.2.10VirtualBox 6.1.16Windows PowerShell(windows 10 自带)

因为微服务项目一般涉及的工程较多,所以在开发环境会把这些工程部署到虚拟机中,方便测试。

首先给出微服务开发虚拟机环境相关软件清单:

  1. Vagrant 2.2.10
  2. VirtualBox 6.1.16
  3. Windows PowerShell(windows 10 自带)

我们的目标是在 windows 10 下,构建出 Vagrant 管控的、 底层基于 VirtualBox 虚拟机技术的 CentOS 环境。

因为会用到底层虚拟化技术,所以我们需要首先在任务管理器的"性能"页签下,确认是否已启用虚拟化技术,如果没有需要在主板 Bios 中开启。

网络异常,图片无法展示
|

1、Vagrant

Vagrant 是一个虚拟机管理工具,通过它,我们可以快速实现虚拟机镜像分发和使用。

Vagrant 官网地址:
https://www.vagrantup.com/

vagrant /ˈveɪɡrənt/

One who wanders from place to place without a permanent home or a means of livelihood.

(1)下载与安装

从 Vagrant 的下载页 找到对应操作系统版本的 msi 安装程序。

网络异常,图片无法展示
|

下载之后,双击 msi 即可进行安装。

安装过程中,我们可以选择 Vagrant 的安装路径:

网络异常,图片无法展示
|

其它没有什么特别的注意点,就是 Copying new files 过程会稍微久一些:

网络异常,图片无法展示
|

(2)验证

Vagrant 安装成功后,在命令行输入 vagrant version,就可以看到如下输出:

网络异常,图片无法展示
|

也可以使用 vagrant -v命令来查看版本号。

2、VirtualBox

VirtualBox 是一款功能强大的虚拟机软件,由于安装过程较简单,所以这里就不赘述咯。

因为镜像文件占用很大的存储空间,所以最好放在非系统盘中。

在 VirtualBox 中点击管理 -》全局设定

网络异常,图片无法展示
|

进入“常规”设置选项卡,在此可配置虚拟电脑位置:

网络异常,图片无法展示
|

3、安装 Vagrant Box

Vagrant中有两个重要概念:Box和Machine。Box指的是虚拟机应用镜像文件,比如 CentOS 应用。Machine指的是处于运行状态的虚拟机。

这里用到的 CentOS 来源于 app.vagrantup.com/boxes/searc…

网络异常,图片无法展示
|

安装 box: vagrant box add [box_name] file:///[box_path]

形如:vagrant box add --force --name centos7 C:\Users\86135\Documents\deniro-soft\MSA-CentOS7.box

注意:box 所在的 URI 必须是ascii 码,所以 box 所在的文件夹不能出现中文名。

如果需要覆盖已存在的 box,可以在之前的命令中加入 --force。

网络异常,图片无法展示
|

Vagrant2.x 的vagrant box add 命令说明摘录如下:

Usage: vagrant box add [options] <name, url, or path>
Options:
    -c, --clean                      Clean any temporary download files
    -f, --force                      Overwrite an existing box if it exists
        --insecure                   Do not validate SSL certificates
        --cacert FILE                CA certificate for SSL download
        --capath DIR                 CA certificate directory for SSL download
        --cert FILE                  A client SSL cert, if needed
        --location-trusted           Trust 'Location' header from HTTP redirects and use the same credentials for subsequent urls as for the initial one
        --provider PROVIDER          Provider the box should satisfy
        --box-version VERSION        Constrain version of the added box
The box descriptor can be the name of a box on HashiCorp's Vagrant Cloud,
or a URL, or a local .box file, or a local .json file containing
the catalog metadata.
The options below only apply if you're adding a box file directly,
and not using a Vagrant server or a box structured like 'user/box':
        --checksum CHECKSUM          Checksum for the box
        --checksum-type TYPE         Checksum type (md5, sha1, sha256)
        --name BOX                   Name of the box
        --[no-]color                 Enable or disable color output
        --machine-readable           Enable machine readable output
    -v, --version                    Display Vagrant version
        --debug                      Enable debug output
        --timestamp                  Enable timestamps on log output
        --debug-timestamp            Enable debug output with timestamps
        --no-tty                     Enable non-interactive output
-h, --help                       Print this help

使用 vagrant box remove <name>,可以删除指定名称的 box。

4、初始化 Vagrant Box

打开 PowerShell(win10 自带,直接在搜索框中可以找到)

网络异常,图片无法展示
|

Windows PowerShell 是一种命令行外壳程序和脚本环境。

通过该命令 vagrant box list我们可以查看刚才安装的 CentOS Box:

网络异常,图片无法展示
|

切换到需要安装虚拟机的路径,然后运行 vagrant init <box_name>,执行初始化 Box 操作。

网络异常,图片无法展示
|

命令执行成功后,会在当前路径下生成 Vagrantfile 配置文件。

5、vagrantfile 配置

在 vagrantfile 中,我们一般会配置以下这些参数。

(1)配置 box 名称

config.vm.box = "centos7"

(2)配置私有网络

私有网络的意思是:只有当前主机才可以访问这个虚拟机。语法示例为:config.vm.network "private_network", ip: "192.168.33.10"

(3)配置共享文件夹

Vagrant 提供了将本机目录挂载到虚拟机目录的功能,默认是将Vagrant 配置文件所在目录挂载到虚拟机 /vagrant 目录下。语法示例为:config.vm.synced_folder "C:/VM/Vagrant/data", "/host_data"。注意:本机目录(示例中的 C:/VM/Vagrant/data)必须预先创建好。

配置好后,就会在 vagrant up 命令启动后,看到输出日志:

网络异常,图片无法展示
|

通过 vagrant ssh 登录 CentOS 系统后,通过 cd / 进入根目录,就会看到主机所共享出来的目录:

网络异常,图片无法展示
|

(4)配置虚拟机内存与 CPU 数

vb.memory = "2048"
vb.cpus = 2

完整 vagrantfile 内容如下:

# -*- mode: ruby -*-
# vi: set ft=ruby :
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.
  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "centos7"
  #config.vm.boot_timeout = 600
  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false
  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  # config.vm.network "forwarded_port", guest: 80, host: 8080
  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"
  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  config.vm.network "private_network", ip: "192.168.33.10"
  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"
  # config.vm.network "public_network",use_dhcp_assigned_default_route: true
  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  config.vm.synced_folder "C:/VM/Vagrant/data", "/vagrant_data"
  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
   config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
     vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
   end
  #
  # View the documentation for the provider you are using for more
  # information on available options.
  config.vm.provider "virtualbox" do |vb|
    vb.memory = "2048"
    vb.cpus = 2
  end
  # Enable provisioning with a shell script. Additional provisioners such as
  # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  #   apt-get update
  #   apt-get install -y apache2
  # SHELL
end

6、Vagrant 常用命令

网络异常,图片无法展示
|

7、启动虚拟机

在 Windows PowerShell 中运行 vagrant up,等待片刻,就可以在 VirtualBox 中看到虚拟机已经启动起来了。

网络异常,图片无法展示
|

虚拟机启动成功的输出日志为:

网络异常,图片无法展示
|

最后通过 vagrant ssh 命令就可以登录虚拟机的 CentOS 系统。

本文就是愿天堂没有BUG给大家分享的内容,大家有收获的话可以分享下,想学习更多的话可以到微信公众号里找我,我等你哦。

相关文章
|
2月前
|
应用服务中间件 Linux nginx
在虚拟机Docker环境下部署Nginx的步骤。
以上就是在Docker环境下部署Nginx的步骤。需要注意,Docker和Nginix都有很多高级用法和细节需要掌握,以上只是一个基础入门级别的教程。如果你想要更深入地学习和使用它们,请参考官方文档或者其他专业书籍。
157 5
|
8月前
|
人工智能 安全 Java
智慧工地源码,Java语言开发,微服务架构,支持分布式和集群部署,多端覆盖
智慧工地是“互联网+建筑工地”的创新模式,基于物联网、移动互联网、BIM、大数据、人工智能等技术,实现对施工现场人员、设备、材料、安全等环节的智能化管理。其解决方案涵盖数据大屏、移动APP和PC管理端,采用高性能Java微服务架构,支持分布式与集群部署,结合Redis、消息队列等技术确保系统稳定高效。通过大数据驱动决策、物联网实时监测预警及AI智能视频监控,消除数据孤岛,提升项目可控性与安全性。智慧工地提供专家级远程管理服务,助力施工质量和安全管理升级,同时依托可扩展平台、多端应用和丰富设备接口,满足多样化需求,推动建筑行业数字化转型。
297 5
|
8月前
|
人工智能 Java 数据库
飞算 JavaAI:革新电商订单系统 Spring Boot 微服务开发
在电商订单系统开发中,传统方式耗时约30天,需应对复杂代码、调试与测试。飞算JavaAI作为一款AI代码生成工具,专注于简化Spring Boot微服务开发。它能根据业务需求自动生成RESTful API、数据库交互及事务管理代码,将开发时间缩短至1小时,效率提升80%。通过减少样板代码编写,提供规范且准确的代码,飞算JavaAI显著降低了开发成本,为软件开发带来革新动力。
|
5月前
|
IDE Java API
Java 17 新特性与微服务开发的实操指南
本内容涵盖Java 11至Java 17最新特性实战,包括var关键字、字符串增强、模块化系统、Stream API、异步编程、密封类等,并提供图书管理系统实战项目,帮助开发者掌握现代Java开发技巧与工具。
299 1
|
消息中间件 API 持续交付
后端开发中的微服务架构实践####
【10月更文挑战第21天】 本文深入探讨了微服务架构在后端开发中的应用,从基本概念出发,详细阐述了微服务的核心优势、设计原则及关键技术。通过实际案例分析,揭示了微服务如何助力企业应对复杂业务需求,提升系统的可扩展性、灵活性与可靠性。同时,也指出了实施微服务过程中可能面临的挑战,并提供了相应的解决方案和最佳实践。 ####
168 3
|
7月前
|
人工智能 数据可视化 JavaScript
颠覆开发效率!国内首个微服务编排框架Juggle开源啦!
Juggle是国内首个开源的微服务编排框架,专注于解决企业微服务进程中接口重复开发、系统对接复杂等问题。它提供零代码、低代码和AI增强功能,通过可视化拖拽快速组装简单API为复杂接口,支持多协议、多语言脚本和流程多版本管理。相比国外框架如Conductor,Juggle更贴合国内需求,具备高效开发、企业级可靠性及信创适配等优势,助力企业实现敏捷创新与数字化转型。
颠覆开发效率!国内首个微服务编排框架Juggle开源啦!
|
10月前
|
Shell Go 开发工具
【环境】Rocky8使用gvm配置Go多版本管理的微服务开发环境(go-zero)
通过本文的介绍,我们详细讲解了如何在Rocky8上使用gvm来管理多个Go版本,并配置go-zero框架的开发环境。通过gvm的灵活管理,开发者可以轻松切换不同的Go版本,以适应不同项目的需求。同时,go-zero框架的使用进一步提升了微服务开发的效率和质量。希望本文能帮助开发者构建高效的Go语言开发环境,提高项目开发的灵活性和稳定性。
330 63
|
6月前
|
Java API 微服务
Java 21 与 Spring Boot 3.2 微服务开发从入门到精通实操指南
《Java 21与Spring Boot 3.2微服务开发实践》摘要: 本文基于Java 21和Spring Boot 3.2最新特性,通过完整代码示例展示了微服务开发全流程。主要内容包括:1) 使用Spring Initializr初始化项目,集成Web、JPA、H2等组件;2) 配置虚拟线程支持高并发;3) 采用记录类优化DTO设计;4) 实现JPA Repository与Stream API数据访问;5) 服务层整合虚拟线程异步处理和结构化并发;6) 构建RESTful API并使用Springdoc生成文档。文中特别演示了虚拟线程配置(@Async)和StructuredTaskSco
780 0
|
9月前
|
虚拟化 网络虚拟化 Windows
导入虚拟机到Hyper-V环境时,理解并配置网络适配器设置是确保网络通信的关键
在Hyper-V环境中,正确配置虚拟机的网络适配器是确保其网络通信的关键。需先启用Hyper-V功能并创建虚拟交换机。接着,在Hyper-V管理器中选择目标虚拟机,添加或配置网络适配器,选择合适的虚拟交换机(外部、内部或私有),并根据需求配置VLAN、MAC地址等选项。最后,启动虚拟机并验证网络连接,确保其能正常访问外部网络、与主机及其他虚拟机通信。常见问题包括无法访问外部网络或获取IP地址,需检查虚拟交换机和适配器设置。
|
10月前
|
安全 Linux 开发工具
【Azure 环境】Azure 虚拟机上部署 DeepSeek R1 模型教程(1.5B参数)【失败】
遇见错误一:operator torchvision::nms does not exist 遇见错误二:RuntimeError: Failed to infer device type
915 22

热门文章

最新文章