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

11/09/2020

本文内容

可以使用 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.

The latest version of Docker.

提示

若要在 Mac 上安装 Docker,请按 Docker 文档中的步骤操作。To install Docker on your Mac, follow the steps in the Docker documentation. 安装之后,请验证安装。

创建本地容器和设置 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:

使用以下设置更新主机上的 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.

备注

建议直接在 Docker 中修改守护程序,因为 daemon.json 文件的位置可能会因计算机而异。Modifying the daemon directly in Docker is recommended because the location of the daemon.json file can vary from machine to machine. 例如,~/Library/Containers/com.docker.docker/Data/database/com.docker.driver.amd64-linux/etc/docker/daemon.json。For example, ~/Library/Containers/com.docker.docker/Data/database/com.docker.driver.amd64-linux/etc/docker/daemon.json.

提示

测试大型应用程序时,我们建议增加分配给 Docker 的资源。We recommend increasing the resources allocated to Docker when testing large applications. 为此,可以选择 Docker 图标,然后选择“高级”来调整核心数量和内存量。This can be done by selecting the Docker Icon, then selecting Advanced to adjust the number of cores and memory.

启动群集。Start the cluster.

Ubuntu 18.04 LTS:Ubuntu 18.04 LTS:
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 mcr.microsoft.com/service-fabric/onebox:u18
Ubuntu 16.04 LTS:Ubuntu 16.04 LTS:
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 mcr.microsoft.com/service-fabric/onebox:u16

提示

默认情况下,这样会拉取具有最新 Service Fabric 版本的映像。By default, this will pull the image with the latest version of Service Fabric. 对于特定的修订版本,请访问 Docker Hub 上的 Service Fabric Onebox 页。For particular revisions, please visit the Service Fabric Onebox page on Docker Hub.

可选:构建扩展的 Service Fabric 映像。Optional: Build your extended Service Fabric image.

在新目录中创建名为 Dockerfile 的文件,以构建自定义映像:In a new directory, create a file called Dockerfile to build your customized image:

备注

可以使用 Dockerfile 修改上面的映像,以将其他程序或依赖项添加到容器中。You can adapt the image above with a Dockerfile to add additional programs or dependencies into your container.

例如,添加 RUN apt-get install nodejs -y 可以支持将 nodejs 应用程序用作来宾可执行文件。For example, adding RUN apt-get install nodejs -y will allow support for nodejs applications as guest executables.

FROM mcr.microsoft.com/service-fabric/onebox:u18

RUN apt-get install nodejs -y

EXPOSE 19080 19000 80 443

WORKDIR /home/ClusterDeployer

CMD ["./ClusterDeployer.sh"]

提示

默认情况下,这样会拉取具有最新 Service Fabric 版本的映像。By default, this will pull the image with the latest version of Service Fabric. 如需特定的修订版本,请访问 Docker 中心页。For particular revisions, please visit the Docker Hub page.

若要通过 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 .

备注

此操作需要一段时间,但只需执行一次。This operation will take some time but is only needed once.

现在,每当有需要时,都可以运行以下命令,快速启动 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

提示

为容器实例提供一个名称,以更具可读性的方式对其进行处理。Provide a name for your container instance so it can be handled in a more readable manner.

如果应用程序正在侦听特定端口,则必须使用附加的 -p 标记指定这些端口。If your application is listening on certain ports, the ports must be specified by using additional -p tags. 例如,如果应用程序正在侦听端口 8080,请添加下面的 -p 标记:For example, if your application is listening on port 8080, add the following -p tag:

docker run -itd -p 19000:19000 -p 19080:19080 -p 8080:8080 --name sfonebox mcr.microsoft.com/service-fabric/onebox:u18

群集需要一小段时间来启动。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

若要停止并清理容器,请使用以下命令。To stop and clean up 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 服务未运行,并且目前在容器中不受支持。DNS service does not run and is currently not supported within the container. 问题 #132Issue #132

运行基于容器的应用需要在 Linux 主机上运行 SF。Running container-based apps requires running SF on a Linux host. 当前不支持嵌套容器应用。Nested container apps are currently not supported.

在 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.

若要在部署应用程序之前连接到群集,请运行以下命令。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:

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

通过 NPM 在计算机上安装 Yeoman 模板生成器:Install the Yeoman template generator on your machine from NPM:

npm install -g yo

请按入门文档中的步骤,安装首选的 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

安装生成器后,可通过运行 yo azuresfguest 或 yo azuresfcontainer 分别创建来宾可执行文件或容器服务。After you install the generators, create guest executable or container services by running yo azuresfguest or yo azuresfcontainer, respectively.

若要在 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

重要

brew cask install java 的当前版本可能会安装更新版本的 JDK。Current versions of brew cask install java may install a more recent version of the JDK.

请确保安装 JDK 8。Be sure to install JDK 8.

通过 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:

连接到在 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

从项目目录中运行安装脚本:From inside your project directory, run the install script:

cd MyProject

bash install.sh

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

.NET Core Service Fabric 应用程序包在 NuGet.org 上托管。Packages for .NET Core Service Fabric applications are hosted on NuGet.org.

在 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 mcr.microsoft.com/service-fabric/onebox:latest

这些属性定义如下: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.

备注

如果为工作区设置了其他名称/路径,请在 docker run 命令中更新这些值。If you have a different name/path for your workspace, update these values in the docker run command.

如果所启动容器的名称不是 sfonebox,请在 Service Fabric 执行组件 Java 应用程序的 testclient.sh 文件中更新名称值。If you start the container with a name other than sfonebox, update the name value in the testclient.sh file in your Service Fabric actor Java application.

后续步骤Next steps