[eShopOnContainers 学习系列] - 03 - 在远程 Ubuntu 16.04 上配置开发环境

直接把 md 粘出来了,博客园的富文本编辑器换成 markdown,没啥效果呀 ,先凑合吧。实在不行换地方

# 在远程 Ubuntu 16.04 上配置开发环境

## 零、因

为什么要用这么麻烦的方法配置远程服务器来开发 eshopOnContainers 项目?

很简单,本子配置不够用。

你说不玩游戏的,i5 + 8G 就足够了,而这个要跑起来,docker 需要分配 3 cpu 和 至少 4G 内存。

好在公司放了台 8cup 64g 的服务器,跑这个正合适。

## 一、 安装 xrdp

>[http://c-nergy.be/blog/?p=8952](http://c-nergy.be/blog/?p=8952)

- 安装 xrdp

``` shell

sudo apt-get install xrdp

sudo apt-get update

sudo apt-get install mate-core mate-desktop-environment mate-notification-daemon

sudo sed -i.bak '/fi/a #xrdp multiple users configuration \n mate-session \n' /etc/xrdp/startwm.sh

```

- win+R ,输入 mstsc ,打开 windows 远程连接,直接输入 Ip / 账号 / 密码 连接即可

## 二、 安装 vs code

>[https://code.visualstudio.com/](https://code.visualstudio.com/)

- 直接下载 deb 安装就行;

- 连上远程后打不开

``` shell

sudo sed -i 's/BIG-REQUESTS/_IG-REQUESTS/' /usr/lib/x86_64-linux-gnu/libxcb.so.1

```

## 三、安装 nodejs 和 npm / bower

>[https://docs.npmjs.com/getting-started/installing-node](https://docs.npmjs.com/getting-started/installing-node)

1. 安装 nodejs

``` shell

curl -sL https://deb.nodesource.com/setup_9.x | sudo -E bash -

sudo apt-get install -y nodejs

```

2. 安装 npm

``` shell

sudo apt-get install -y npm

sudo npm install npm@latest -g

```

3. 安装 Bower

``` shell

sudo npm install -g bower

```

## 四、安装 docker 和 git

>官方文档:[https://docs.docker.com/engine/installation/linux/docker-ce/ubuntu/](https://docs.docker.com/engine/installation/linux/docker-ce/ubuntu/)

1. 删除旧版本

``` shell

sudo apt-get remove docker docker-engine docker.io

```

2. 安装 Docker CE

``` shell

sduo apt-get update

sudo apt-get install apt-transport-https ca-certificates curl software-properties-common

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

sudo apt-key fingerprint 0EBFCD88

pub 4096R/0EBFCD88 2017-02-22

Key fingerprint = 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88

uid Docker Release (CE deb) <docker@docker.com>

sub 4096R/F273FCD8 2017-02-22

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

sudo apt-get update

sudo apt-get install docker-ce

```

3. 安装 git

sudo apt-get git

## 五、安装 dotnet core sdk

>[https://www.microsoft.com/net/download/thank-you/dotnet-sdk-2.1.3-linux-x64-binaries](https://www.microsoft.com/net/download/thank-you/dotnet-sdk-2.1.3-linux-x64-binaries)

建议直接下载安装,不用 apt-get

下载后,在 shell 中运行

``` shell

mkdir -p $HOME/dotnet && tar zxf dotnet.tar.gz -C $HOME/dotnet

export PATH=$PATH:$HOME/dotnet

```

注意更换文件名 dotnet.tar.gz 为真实文件名;export 可以直接设置在 /etc/profile 中。

## 六、下载 eShopOnContainers 并配置

>[https://github.com/dotnet-architecture/eShopOnContainers/wiki/04.-Setting-eShopOnContainer-solution-up-in-a-Mac,-VS-for-Mac-or-with-CLI-environment--(dotnet-CLI,-Docker-CLI-and-VS-Code)](https://github.com/dotnet-architecture/eShopOnContainers/wiki/04.-Setting-eShopOnContainer-solution-up-in-a-Mac,-VS-for-Mac-or-with-CLI-environment--(dotnet-CLI,-Docker-CLI-and-VS-Code))

linux 的和 mac 的差不多,所以官方文档只提供了 mac 的。

1. git clone

``` shell

sudo git clone https://github.com/dotnet/eShopOnContainers.git

```

2. 配置 spa 应用(可省略)

>[https://github.com/dotnet/eShopOnContainers/wiki/06.-Setting-the-Web-SPA-application-up](https://github.com/dotnet/eShopOnContainers/wiki/06.-Setting-the-Web-SPA-application-up)

``` shell

cd eShopOnContainers\src\Web\WebSPA

sudo npm install

sudo npm run build:prod

```

## 七、安装 docker-compose

>[https://docs.docker.com/compose/install/](https://docs.docker.com/compose/install/)

``` shell

sudo curl -L https://github.com/docker/compose/releases/download/1.18.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

sudo chmod +x /usr/local/bin/docker-compose

docker-compose --version

```

## 八、使用 CLI 环境和 vs code 来作为编译器

>Use a CLI environment (dotnet CLI, Docker CLI with the bash shell) and VS Code as plain editor

- 因:vs studio 现在还没有 linux 版本,所以在 Linux 下开发,只能使用 vs code。

也有的同学说,vs studio 2017 支持远程编译调试 linux 下程序,我暂时还没有试,等以后有空了再试试。

1. 简单方式

``` shell

# 克隆

git clone https://github.com/dotnet-architecture/eShopOnContainers.git

# 进入项目文件夹

cd eShopOnContainers

# 使用配置进行 docker 编译

docker-compose -f docker-compose.ci.build.yml up

# 运行镜像

docker-compose up

``

问题:

我在运行 docker-compose -f docker-compose.ci.build.yml up 时,提示 sdk 2.1.2 指定版本不存在,暂时还没找到更改错误的地方。所以使用的是第二种手动配置

2. 手动方式

``` shell

cd eShopOnContainers/cli-linux

sudo /bin/bash build-bits-linux.sh

# 回到 eShopOnContainers 文件夹

cd ..

# 重新生成镜像

docker-compose build

# 根据镜像生成容器并运行

docker-compose up

```

这个 build-bits-linux.sh shell脚本,可以将所有的项目,使用 dotnet publish 进行编译,生成二进制 dll 以供使用。

并删除旧的 docker images

如果出错,一般是路径问题,下面附上我更改后的 sh 文件

``` shell

#!/bin/bash

declare -x path=$1

if [ -z "$path" ]; then

$path="$(pwd)/../src";

echo -e "\e[33mNo path passed. Will use $path"

fi

# 替换下面的路径为自己的 eshop src 路径

path="/home/用户名/eShopOnContainers/src"

declare -a projectList=(

"$path/Web/WebSPA"

"$path/Services/Catalog/Catalog.API"

"$path/Services/Basket/Basket.API"

"$path/Services/Ordering/Ordering.API"

"$path/Services/Identity/Identity.API"

"$path/Services/Location/Locations.API"

"$path/Services/Marketing/Marketing.API"

"$path/Services/Payment/Payment.API"

"$path/Web/WebMVC"

"$path/Web/WebStatus"

)

# Build SPA app

# pushd $(pwd)../src/Web/WebSPA

# npm run build:prod

for project in "${projectList[@]}"

do

echo -e "\e[33mWorking on $project"

echo -e "\e[33m\tRemoving old publish output"

pushd $project

rm -rf obj/Docker/publish

echo -e "\e[33m\tBuilding and publishing $project"

~/dotnet/dotnet publish -c Release -o obj/Docker/publish --verbosity quiet

popd

done

## remove old docker images:

images=$(docker images --filter=reference="eshop/*" -q)

if [ -n "$images" ]; then

docker rm $(docker ps -a -q) -f

echo "Deleting eShop images in local Docker repo"

echo $images

docker rmi $(docker images --filter=reference="eshop/*" -q) -f

fi

# No need to build the images, docker build or docker compose will

# do that using the images and containers defined in the docker-compose.yml file.

#

#

```

## 九、测试结果

- MVC web app: `http://localhost:5100`

User: demouser@microsoft.com

Password: Pass@word1

- SPA web app: `http://localhost:5105`

User: demouser@microsoft.com

Password: Pass@word1

- Health Status web app: `http://localhost:5107`

- Catalog Microservice API: `http://localhost:5101`

- Ordering Microservice API: `http://localhost:5102`

- Basket Microservice API: `http://localhost:5103`

- Identity Microservice API: `http://localhost:5105`

- Payment API: `http://localhost:5108`

- Marketing API: `http://localhost:5110`

- Locations API: `http://localhost:5109`

Mvc 应该是不能正常登录的,请更改 docker-compose.override.yml

主要就是改 IdentityUrl,代码中原来的是针对 windows 环境

``` xml

webmvc:

environment:

- ASPNETCORE_ENVIRONMENT=Development

- ASPNETCORE_URLS=http://0.0.0.0:80

- CatalogUrl=http://catalog.api

- OrderingUrl=http://ordering.api

- BasketUrl=http://basket.api

- LocationsUrl=http://locations.api

- IdentityUrl=localhost:5105

- MarketingUrl=http://marketing.api

- CatalogUrlHC=http://catalog.api/hc

- OrderingUrlHC=http://ordering.api/hc

- IdentityUrlHC=http://identity.api/hc

- BasketUrlHC=http://basket.api/hc

- MarketingUrlHC=http://marketing.api/hc

- PaymentUrlHC=http://payment.api/hc

- UseCustomizationData=True

- ApplicationInsights__InstrumentationKey=${INSTRUMENTATION_KEY}

- OrchestratorType=${ORCHESTRATOR_TYPE}

- UseLoadTest=${USE_LOADTEST:-False}

ports:

- "5100:80"

```

然后重新 docker-compose build ,docker-compose up 即可