在 Mac OS X 上设置开发环境Set up your development environment on Mac OS X

可以使用 Mac OS X 生成在 Linux 群集上运行的 Azure Service Fabric 应用程序。本文档介绍了如何设置用于开发的 Mac。You can build Azure Service Fabric applications to run on Linux clusters by using Mac OS X. This document covers how to set up your Mac for development.

先决条件Prerequisites

Azure Service Fabric 不在 Mac OS X 本机上运行。为了运行本地 Service Fabric 群集,我们提供了预配置的 Docker 容器映像。Azure Service Fabric doesn't run natively on Mac OS X. To run a local Service Fabric cluster, a pre-configured Docker container image is provided. 准备事项:Before you get started, you need:

  • 至少 4 GB 的 RAM。At least 4 GB of RAM.
  • 最新版的 DockerThe latest version of Docker.

Tip

若要在 Mac 上安装 Docker,请按 Docker 文档中的步骤操作。 安装之后,请验证安装

创建本地容器和设置 Service FabricCreate a local container and set up Service Fabric

若要设置本地 Docker 容器并在其上运行 Service Fabric 群集,请执行以下步骤:To set up a local Docker container and have a Service Fabric cluster running on it, perform the following steps:

  1. 使用以下设置更新主机上的 Docker 守护程序配置并重启 Docker 守护程序:Update the Docker daemon configuration on your host with the following settings and restart the Docker daemon:

    {
        "ipv6": true,
        "fixed-cidr-v6": "fd00::/64"
    }
    

    可以在 Docker 安装路径的 daemon.json 文件中直接更新这些设置。You can update these settings directly in the daemon.json file in your Docker installation path. 可直接在 Docker 中修改守护程序配置设置。You can directly modify the daemon configuration settings in Docker. 选择 Docker 图标,然后选择“首选项” > “守护程序” > “高级”。Select the Docker icon, and then select Preferences > Daemon > Advanced.

    Note

    建议直接在 Docker 中修改守护程序,因为 daemon.json 文件的位置可能会因计算机而异。 例如,~/Library/Containers/com.docker.docker/Data/database/com.docker.driver.amd64-linux/etc/docker/daemon.json。

    Tip

    测试大型应用程序时,我们建议增加分配给 Docker 的资源。 为此,可以选择 Docker 图标,然后选择“高级”来调整核心数量和内存量。

  2. 在新目录中创建名为 Dockerfile 的文件,以生成 Service Fabric 映像:In a new directory create a file called Dockerfile to build your Service Fabric Image:

    FROM microsoft/service-fabric-onebox
    WORKDIR /home/ClusterDeployer
    RUN ./setup.sh
    #Generate the local
    RUN locale-gen en_US.UTF-8
    #Set environment variables
    ENV LANG=en_US.UTF-8
    ENV LANGUAGE=en_US:en
    ENV LC_ALL=en_US.UTF-8
    EXPOSE 19080 19000 80 443
    #Start SSH before running the cluster
    CMD /etc/init.d/ssh start && ./run.sh
    

    Note

    可以修改此文件,以便在容器中添加更多程序或依赖项。 例如,添加 RUN apt-get install nodejs -y 可以支持将 nodejs 应用程序用作来宾可执行文件。

    Tip

    默认情况下,这样会拉取具有最新 Service Fabric 版本的映像。 如需特定的修订版本,请访问 Docker 中心页。

  3. 若要通过 Dockerfile 生成可重用的映像,请打开终端并运行 cd 切换到 Dockerfile 所在的目录,然后运行:To build your reusable image from the Dockerfile open a terminal and cd to the directly holding your Dockerfile then run:

    docker build -t mysfcluster .
    

    Note

    此操作需要一段时间,但只需执行一次。

  4. 现在,每当有需要时,都可以运行以下命令,快速启动 Service Fabric 的本地副本:Now you can quickly start a local copy of Service Fabric, whenever you need it, by running:

    docker run --name sftestcluster -d -v /var/run/docker.sock:/var/run/docker.sock -p 19080:19080 -p 19000:19000 -p 25100-25200:25100-25200 mysfcluster
    

    Tip

    为容器实例提供一个名称,以更具可读性的方式对其进行处理。

    如果应用程序正在侦听特定端口,则必须使用附加的 -p 标记指定这些端口。 例如,如果应用程序正在侦听端口 8080,请添加下面的 -p 标记:

    docker run -itd -p 19080:19080 -p 8080:8080 --name sfonebox microsoft/service-fabric-onebox

  5. 群集需要一小段时间来启动。The cluster will take a moment to start. 运行时,可以使用以下命令查看日志,或者通过跳转到仪表板来查看群集运行状况 http://localhost:19080When it is running, you can view logs using the following command or jump to the dashboard to view the clusters health http://localhost:19080:

    docker logs sftestcluster
    
  6. 若要停止并清理容器,请使用以下命令。To stop and cleanup the container, use the following command. 但是,我们将在下一步中使用此容器。However, we will be using this container in the next step.

    docker rm -f sftestcluster
    

已知限制Known Limitations

以下是在 Mac 的容器中运行的本地群集的已知限制:The following are known limitations of the local cluster running in a container for Mac's:

  • DNS 服务无法运行且不受支持 问题 #132DNS service does not run and is not supported Issue #132

在 Mac 上设置 Service Fabric CLI (sfctl)Set up the Service Fabric CLI (sfctl) on your Mac

按照 Service Fabric CLI 中的说明在 Mac 上安装 Service Fabric CLI (sfctl)。Follow the instructions at Service Fabric CLI to install the Service Fabric CLI (sfctl) on your Mac. CLI 命令支持与 Service Fabric 实体(包括群集、应用程序和服务)交互。The CLI commands support interacting with Service Fabric entities, including clusters, applications, and services.

  1. 若要在部署应用程序之前连接到群集,请运行以下命令。To connect to the cluster before deploying applications run the command below.
sfctl cluster select --endpoint http://localhost:19080

使用 Yeoman 在 Mac 上创建应用程序Create your application on your Mac by using Yeoman

Service Fabric 提供基架工具,可以借助此类工具,使用 Yeoman 模板生成器从终端创建 Service Fabric 应用程序。Service Fabric provides scaffolding tools that help you to create a Service Fabric application from the terminal by using the Yeoman template generator. 执行以下步骤,确保已经有可以在计算机上运行的 Service Fabric Yeoman 模板生成器:Use the following steps to ensure that the Service Fabric Yeoman template generator is working on your machine:

  1. Node.js 和 Node 包管理器 (NPM) 必须安装在 Mac 上。Node.js and Node Package Manager (NPM) must be installed on your Mac. 此软件可以使用 HomeBrew 进行安装,如下所示:The software can be installed by using HomeBrew, as follows:

    brew install node
    node -v
    npm -v
    
  2. 通过 NPM 在计算机上安装 Yeoman 模板生成器:Install the Yeoman template generator on your machine from NPM:

    npm install -g yo
    
  3. 请按入门文档中的步骤,安装首选的 Yeoman 生成器。Install the Yeoman generator that you prefer by following the steps in the getting started documentation. 若要使用 Yeoman 来创建 Service Fabric 应用程序,请执行以下步骤:To create Service Fabric applications by using Yeoman, follow these steps:

    npm install -g generator-azuresfjava       # for Service Fabric Java Applications
    npm install -g generator-azuresfguest      # for Service Fabric Guest executables
    npm install -g generator-azuresfcontainer  # for Service Fabric Container Applications
    
  4. 安装生成器后,可通过运行 yo azuresfguestyo azuresfcontainer 分别创建来宾可执行文件或容器服务。After you install the generators, create guest executable or container services by running yo azuresfguest or yo azuresfcontainer, respectively.

  5. 若要在 Mac 上生成 Service Fabric Java 应用程序,必须在主机上安装 JDK 1.8 和 Gradle。To build a Service Fabric Java application on your Mac, JDK version 1.8 and Gradle must be installed on the host machine. 此软件可以使用 HomeBrew 进行安装,如下所示:The software can be installed by using HomeBrew, as follows:

    brew update
    brew cask install java
    brew install gradle
    

    Tip

    请务必验证并确保已安装 JDK 的正确版本。

通过 Terminal 在 Mac 上部署应用程序Deploy your application on your Mac from the terminal

创建和生成 Service Fabric 应用程序以后,即可使用 Service Fabric CLI 部署该应用程序:After you create and build your Service Fabric application, you can deploy your application by using the Service Fabric CLI:

  1. 连接到在 Mac 的容器实例中运行的 Service Fabric 群集:Connect to the Service Fabric cluster that is running inside the container instance on your Mac:

    sfctl cluster select --endpoint http://localhost:19080
    
  2. 从项目目录中运行安装脚本:From inside your project directory, run the install script:

    cd MyProject
    bash install.sh
    

设置 .NET Core 2.0 开发Set up .NET Core 2.0 development

安装用于 Mac 的 .NET Core 2.0 SDK,开始创建 C# Service Fabric 应用程序Install the .NET Core 2.0 SDK for Mac to start creating C# Service Fabric applications. .NET Core 2.0 Service Fabric 应用程序包托管在目前以预览版形式推出的 NuGet.org 上。Packages for .NET Core 2.0 Service Fabric applications are hosted on NuGet.org, which is currently in preview.

在 Mac 上为 Eclipse 安装 Service Fabric 插件Install the Service Fabric plug-in for Eclipse on your Mac

Azure Service Fabric 为适用于 Java IDE 的 Eclipse Neon(或更高版本)提供插件。Azure Service Fabric provides a plug-in for Eclipse Neon (or later) for the Java IDE. 该插件可简化创建、生成和部署 Java 服务的过程。The plug-in simplifies the process of creating, building, and deploying Java services. 若要为 Eclipse 安装 Service Fabric 插件或将其更新到最新版本,请执行这些步骤To install or update the Service Fabric plug-in for Eclipse to the latest version, follow these steps. 适用于 Eclipse 的 Service Fabric 文档中的其他步骤也适用:生成应用程序、向应用程序添加服务、卸载应用程序,等等。The other steps in the Service Fabric for Eclipse documentation are also applicable: build an application, add a service to an application, uninstall an application, and so on.

最后一步是使用与主机共享的路径实例化该容器。The last step is to instantiate the container with a path that is shared with your host. 该插件需要此类实例化才能与 Mac 上的 Docker 容器配合使用。The plug-in requires this type of instantiation to work with the Docker container on your Mac. 例如:For example:

docker run -itd -p 19080:19080 -v /Users/sayantan/work/workspaces/mySFWorkspace:/tmp/mySFWorkspace --name sfonebox microsoft/service-fabric-onebox

这些属性定义如下:The attributes are defined as follows:

  • /Users/sayantan/work/workspaces/mySFWorkspace 是 Mac 上的工作区的完全限定路径。/Users/sayantan/work/workspaces/mySFWorkspace is the fully qualified path of the workspace on your Mac.
  • /tmp/mySFWorkspace 是容器内部的路径,应将工作区映射到此路径。/tmp/mySFWorkspace is the path that is inside of the container to where the workspace should be mapped.

Note

如果为工作区设置了其他名称/路径,请在 docker run 命令中更新这些值。

如果所启动容器的名称不是 sfonebox,请在 Service Fabric 执行组件 Java 应用程序的 testclient.sh 文件中更新名称值。

后续步骤Next steps