杰瑞科技汇

CloudSim安装教程,环境配置与运行步骤详解?

CloudSim 安装完整教程

CloudSim 是一个在 Java 环境下运行的仿真工具,因此安装的核心是配置好 Java 开发环境,然后将 CloudSim 库文件导入到你的 IDE(如 Eclipse)中。


第一部分:环境准备

在开始之前,请确保你已经安装并配置好了以下软件:

Java Development Kit (JDK)

CloudSim 是一个 Java 项目,你需要 JDK 来编译和运行它。

  • 版本要求:CloudSim 3.0 及更高版本推荐使用 JDK 8 (Java 8) 或更高版本,请确保你的 JDK 版本 >= 1.8。
  • 下载地址Oracle JDK 官网OpenJDK 官网 (推荐使用 OpenJDK,免费开源)。
  • 安装与配置
    1. 运行安装程序,按照提示完成安装。
    2. 配置环境变量
      • 打开“系统属性” -> “高级” -> “环境变量”。
      • 在“系统变量”中,新建一个变量:
        • 变量名:JAVA_HOME
        • 变量值:JDK 的安装路径(C:\Program Files\Java\jdk-11.0.12
      • 找到 Path 变量,点击“编辑”,然后新建一条,输入 %JAVA_HOME%\bin
      • 验证安装:打开一个新的命令提示符(CMD),输入 java -version,如果显示出版本信息,则配置成功。

集成开发环境 (IDE)

我们需要一个 IDE 来编写和管理 CloudSim 项目,Eclipse 是最常用和兼容性最好的选择。

  • 推荐版本:Eclipse IDE for Java Developers。
  • 下载地址Eclipse 官网
  • 安装:解压下载的压缩包即可,无需安装。

第二部分:下载 CloudSim

CloudSim 的主要代码和示例都托管在 GitHub 上。

  1. 访问 GitHub 仓库

    • 主仓库地址:https://github.com/Cloudslab/cloudsim
    • 注意:CloudSim 已经发展出多个版本,目前主流的是 CloudSim 4.0CloudSim Plus(CloudSim 的增强版,功能更多,API 更友好)。本教程以 CloudSim 4.0 为例,但步骤对 CloudSim Plus 同样适用。
  2. 下载源代码

    • 在 GitHub 页面,点击绿色的 Code 按钮,然后选择 Download ZIP
    • 将下载的 cloudsim-master.zip 文件解压到一个你方便找到的目录,D:\dev\cloudsim

第三部分:在 Eclipse 中配置 CloudSim

这是整个安装过程的核心,我们将把 CloudSim 的库文件导入到 Eclipse 中,以便我们的项目能够使用它们。

创建用户库(推荐)

这种方法最规范,可以在多个项目中复用 CloudSim 库。

  1. 打开 Eclipse,切换到 Java 透视图。

  2. 创建用户库

    • 点击菜单栏的 Window -> Preferences
    • 在弹出的窗口中,导航到 Java -> Build Path -> User Libraries
    • 点击右侧的 New... 按钮,输入库名称,CloudSim 4.0,然后点击 OK
  3. 添加 JAR 文件

    • 在左侧列表中选中你刚刚创建的 CloudSim 4.0 库。
    • 点击右侧的 Add External JARs... 按钮。
    • 在弹出的文件选择器中,导航到你解压 CloudSim 的目录,进入 cloudsim-4.0 文件夹。
    • 按住 Ctrl 键,依次选中以下所有 .jar 文件,然后点击 Open
      • cloudsim-4.0.jar
      • cloudsim-3.0.3.jar (CloudSim 4.0 依赖于这个旧版本)
      • simgrid.jar (一个可选的依赖,但最好加上)
      • colt-1.2.0.jar (一个高性能科学计算库,CloudSim 使用它)
      • commons-math3-3.6.1.jar (Apache Commons Math 库)
    • 添加成功后,你会在 User Libraries 窗口中看到这些 JAR 文件,点击 Apply and Close

直接导入项目(适合初学者)

如果你想直接运行 CloudSim 自带的示例,可以导入整个项目。

  1. 导入项目

    • 在 Eclipse 中,点击 File -> Import
    • 展开 General,选择 Existing Projects into Workspace,点击 Next
    • 点击 Browse...,选择你解压 CloudSim 的目录
    • Eclipse 会自动扫描到目录下的 cloudsim-4.0 项目,确保它被勾选,然后点击 Finish
  2. 修复项目依赖

    • 导入后,cloudsim-4.0 项目可能会因为找不到依赖的 JAR 文件而报错。
    • Package Explorer 中右键点击 cloudsim-4.0 项目,选择 Properties
    • 导航到 Java Build Path -> Libraries
    • 你会看到一些 JAR 文件路径是红色的(表示找不到),选中它们,点击 Remove
    • 然后点击 Add External JARs...,像方法一一样,手动添加 cloudsim-3.0.3.jar, simgrid.jar, colt-1.2.0.jar, commons-math3-3.6.1.jar
    • 点击 Apply and Close

第四部分:创建并运行第一个 CloudSim 项目

现在环境已经配置好了,我们来创建一个新项目并运行一个简单的示例。

  1. 创建新 Java 项目

    • 在 Eclipse 中,点击 File -> New -> Java Project
    • 输入项目名称,MyCloudSimProject
    • 点击 Finish
  2. 添加 CloudSim 库到新项目

    • Package Explorer 中右键点击 MyCloudSimProject,选择 Properties
    • 导航到 Java Build Path -> Libraries
    • 点击 Add Library...
    • 选择 User Library,点击 Next
    • 在列表中勾选我们之前创建的 CloudSim 4.0,点击 FinishApply and Close
  3. 编写和运行示例代码

    • MyCloudSimProject 上右键,New -> Class
    • 输入包名(org.cloudbus.cloudsim.examples)和类名(HelloWorld),然后点击 Finish
    • 将 CloudSim 自带的 HelloWorld.java 示例代码复制粘贴到这个新创建的类中,你可以在你解压的 cloudsim-4.0 文件夹的 src\main\java\org\cloudbus\cloudsim\examples 目录下找到它。
    • HelloWorld.java 的代码通常如下:
    package org.cloudbus.cloudsim.examples;
    import org.cloudbus.cloudsim.Cloudlet;
    import org.cloudbus.cloudsim.CloudletSchedulerTimeShared;
    import org.cloudbus.cloudsim.Datacenter;
    import org.cloudbus.cloudsim.DatacenterBroker;
    import org.cloudbus.cloudsim.DatacenterCharacteristics;
    import org.cloudbus.cloudsim.Host;
    import org.cloudbus.cloudsim.Log;
    import org.cloudbus.cloudsim.Pe;
    import org.cloudbus.cloudsim.Storage;
    import org.cloudbus.cloudsim.Vm;
    import org.cloudbus.cloudsim.VmAllocationPolicySimple;
    import org.cloudbus.cloudsim.VmSchedulerTimeShared;
    import org.cloudbus.cloudsim.core.CloudSim;
    import org.cloudbus.cloudsim.provisioners.BwProvisionerSimple;
    import org.cloudbus.cloudsim.provisioners.PeProvisionerSimple;
    import org.cloudbus.cloudsim.provisioners.RamProvisionerSimple;
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.List;
    public class HelloWorld {
        /**
         * The main method.
         * @param args the arguments
         */
        public static void main(String[] args) {
            try {
                // First step: Initialize the CloudSim package. It should be called
                // before creating any entities.
                int num_user = 1; // number of cloud users
                Calendar calendar = Calendar.getInstance();
                boolean trace_flag = false; // mean trace events
                CloudSim.init(num_user, calendar, trace_flag);
                // Second step: Create Datacenters
                // Datacenters are the resource providers in CloudSim. We need at
                // one to run a CloudSim simulation
                Datacenter datacenter0 = createDatacenter("Datacenter_0");
                // Third step: Create Broker
                DatacenterBroker broker = createBroker();
                int brokerId = broker.getId();
                // Fourth step: create one virtual machine
                int vmid = 0;
                int mips = 1000;
                long size = 10000; // image size (MB)
                int ram = 512; // vm memory (MB)
                long bw = 1000; // vm bandwidth
                int pesNumber = 1; // number of cpus
                String vmm = "Xen"; // VMM name
                // create VM
                Vm vm = new Vm(vmid, brokerId, mips, pesNumber, ram, bw, size, vmm, new CloudletSchedulerTimeShared());
                // submit vm to the broker
                broker.submitVm(vm);
                // Fifth step: Create one Cloudlet
                int cloudletId = 0;
                long length = 40000; // in Million Instructions (MI)
                long fileSize = 300; // file size (in bytes)
                long outputSize = 300; // output size (in bytes)
                UtilizationModel utilizationModel = new UtilizationModelFull();
                Cloudlet cloudlet = new Cloudlet(cloudletId, length, pesNumber, fileSize, outputSize, utilizationModel, utilizationModel, utilizationModel);
                cloudlet.setUserId(brokerId);
                // submit cloudlet to the broker
                broker.submitCloudlet(cloudlet);
                // Sixth step: Starts the simulation
                CloudSim.startSimulation();
                // Final step: Print results when simulation is over
                List<Cloudlet> newList = broker.getCloudletReceivedList();
                CloudSim.stopSimulation();
                printCloudletList(newList);
                // Print the debt of each user to each datacenter
                datacenter0.printDebts();
            } catch (Exception e) {
                e.printStackTrace();
                Log.printLine("The simulation has been terminated due to an unexpected error");
            }
        }
        private static Datacenter createDatacenter(String name) {
            // Here are the steps needed to create a Datacenter:
            // 1. Create a MachineList
            // 2. Create a DatacenterCharacteristics object that stores the
            //    properties of a data center: architecture, OS, list of
            //    Machines, allocation policy, time zone and its price.
            // 3. Create a Datacenter object.
            // 1. Create a MachineList
            List<Host> hostList = new ArrayList<Host>();
            List<Pe> peList = new ArrayList<Pe>();
            int mips = 1000;
            // 2. Create PEs and add these into a list.
            peList.add(new Pe(0, new PeProvisionerSimple(mips))); // need to store Pe id and MIPS Rating
            // 3. Create Hosts with its id and list of PEs and add them to the
            //    list of machines
            int ram = 2048; // host memory (MB)
            long storage = 1000000; // host storage
            int bw = 10000; // network bandwidth
            hostList.add(
                    new Host(
                            0,
                            new RamProvisionerSimple(ram),
                            new BwProvisionerSimple(bw),
                            storage,
                            peList,
                            new VmSchedulerTimeShared(peList)
                    )
            ); // This is our first machine
            // 4. Create a DatacenterCharacteristics object that stores the
            //    properties of a data center: architecture, OS, list of
            //    Machines, allocation policy, time zone and its price
            String arch = "x86";
            String os = "Linux";
            String vmm = "Xen";
            double time_zone = 10.0; // Number of hours away from UTC
            double cost = 3.0; // the cost of using processing in this resource
            double costPerMem = 0.05; // the cost of using memory in this resource
            double costPerStorage = 0.001; // the cost of using storage in this resource
            double costPerBw = 0.0; // the cost of using bw in this resource
            DatacenterCharacteristics characteristics = new DatacenterCharacteristics(
                    arch, os, vmm, hostList, time_zone, cost, costPerMem, costPerStorage, costPerBw);
            // 5. Finally, we need to create a Datacenter object.
            Datacenter datacenter = null;
            try {
                datacenter = new Datacenter(name, characteristics, new VmAllocationPolicySimple(hostList), new ArrayList<Storage>(), 0);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return datacenter;
        }
        // We strongly encourage users to develop their own broker policies, to
        // submit vms and cloudlets according to the specific rules of the
        // simulated scenario
        private static DatacenterBroker createBroker() {
            DatacenterBroker broker = null;
            try {
                broker = new DatacenterBroker("Broker");
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
            return broker;
        }
        /**
         * Prints the Cloudlet objects.
         * @param list list of Cloudlets
         */
        private static void printCloudletList(List<Cloudlet> list) {
            int size = list.size();
            Cloudlet cloudlet;
            String indent = "    ";
            Log.printLine();
            Log.printLine("========== OUTPUT ==========");
            Log.printLine("Cloudlet ID" + indent + "STATUS" + indent +
                    "Datacenter ID" + indent + "VM ID" + indent + "Time" + indent +
                    "Start Time" + indent + "Finish Time");
            for (int i = 0; i < size; i++) {
                cloudlet = list.get(i);
                Log.print(indent + cloudlet.getCloudletId() + indent + indent);
                if (cloudlet.getCloudletStatus() == Cloudlet.SUCCESS) {
                    Log.print("SUCCESS");
                    Log.printLine(indent + indent + cloudlet.getResourceId() + indent + indent + indent + cloudlet.getVmId() +
                            indent + indent + cloudlet.getActualCPUTime() + indent + indent + indent + cloudlet.getExecStartTime() +
                            indent + indent + indent + cloudlet.getFinishTime());
                }
            }
        }
    }
  4. 运行代码

    • 在 Eclipse 的代码编辑器中,右键点击任意空白处,选择 Run As -> Java Application
    • 如果一切配置正确,你将在 Eclipse 的 Console 窗口中看到输出结果,类似于:

第五部分:常见问题与解决方法

ClassNotFoundExceptionNoClassDefFoundError

  • 问题:运行程序时,控制台报错,提示找不到 CloudSim 的某个类(如 org.cloudbus.cloudsim.CloudSim)。
  • 原因:项目的 Java Build Path 中没有正确添加 CloudSim 的 JAR 文件。
  • 解决
    1. 检查你创建的 MyCloudSimProject 的 Build Path。
    2. 进入 Properties -> Java Build Path -> Libraries
    3. 确认 CloudSim 4.0 用户库已经被正确添加,如果没有,按照第三部分的方法一重新添加。
    4. 如果库已添加,但 JAR 文件前有红色叉号,说明 JAR 文件的路径有问题,可以尝试删除后重新添加一次。

Could not find the main class

  • 问题:运行时提示找不到主类。
  • 原因
    • 运行的类不是 public class
    • 包名和文件路径不匹配。
    • 运行的是错误的类。
  • 解决
    1. 确保你运行的类是 public 的。
    2. 确保文件路径与包名一致,包名为 org.example 的类,应该位于 src/org/example/ 目录下。
    3. 在 Eclipse 的 Package Explorer 中右键点击要运行的 .java 文件本身,然后选择 Run As -> Java Application

在 Linux/macOS 上安装

  • 过程基本相同:安装 JDK (使用 aptyum),安装 Eclipse (解压即可),下载 CloudSim 源码。
  • 环境变量配置:在 Linux/macOS 中,配置 JAVA_HOMEPATH 是在 .bashrc, .zshrc/etc/profile 文件中完成的。
  • 路径问题:Linux/macOS 是大小写敏感的操作系统,确保所有路径和文件名的大小写完全正确。

恭喜!如果你已经成功运行了 HelloWorld 示例,那么你的 CloudSim 开发环境就已经完全配置好了,现在你可以:

  • 阅读并理解 HelloWorld 的代码,了解 CloudSim 仿真的基本流程(初始化 -> 创建数据中心 -> 创建代理 -> 创建虚拟机和云任务 -> 运行仿真 -> 查看结果)。
  • 尝试修改 HelloWorld 中的参数(如虚拟机数量、云任务长度等),观察结果的变化。
  • 探索 cloudsim-4.0 项目中的 examples 包,里面有更多复杂的示例,如数据中心网络仿真 (NetworkExample)、经济模型 (economically) 等,这些都是学习 CloudSim 的绝佳材料。
分享:
扫描分享到社交APP
上一篇
下一篇