微服务的部署
一、项目实施过程
1.1 Docker compose编排工具
1.1.1 Docker compose的安装与卸载
1.安装条件。在安装compose之前检查机器上是否已经安装docker
2.安装curl插件
3.安装Compose 使用curl命令从GitHub的Compose仓库拉取Docker Compose
4.更改Docker Compose的可执行文件权限
5.查看安装的Docker Compose版本
6.卸载Compose
1.1.2 Compose File文件
1.编写Dockerfile文件
FROM java:8-jre
MAINTAINER shirx <shirx@qq.com>
ADD ./target/microservice-eureka-server-0.0.1-SNAPSHOT.jar /app/microservice-eureka-service.jar
CMD ["java", "-Xmx200m", "-jar", "/app/microservice-eureka-service.jar"]
EXPOSE 8761
2.定义yml文件
version: "3"
services:
mysql:
image: mysql:5.6
restart: on-failure
ports:
- 3306:3306
volumes:
- microservice-mysql:/var/lib/mysql
networks:
- microservice-net
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: microservice_mallmanagement
deploy:
replicas: 1
restart_policy:
condition: on-failure
placement:
constraints: [node.role == manager]
eureka-server:
image: 192.168.10.117:5000/microservice-eureka-server:0.0.1-SNAPSHOT
restart: on-failure
ports:
- 8761:8761
networks:
- microservice-net
deploy:
replicas: 1
restart_policy:
condition: on-failure
gateway-zuul:
image: 192.168.10.117:5000/microservice-gateway-zuul:0.0.1-SNAPSHOT
restart: on-failure
ports:
- 8050:8050
networks:
- microservice-net
depends_on:
- eureka-server
deploy:
replicas: 1
restart_policy:
condition: on-failure
placement:
constraints: [node.role == manager]
order-service:
image: 192.168.10.117:5000/microservice-orderservice:0.0.1-SNAPSHOT
restart: on-failure
ports:
- 7900:7900
networks:
- microservice-net
depends_on:
- mysql
- eureka-server
deploy:
replicas: 2
restart_policy:
condition: on-failure
user-service:
image: 192.168.10.117:5000/microservice-userservice:0.0.1-SNAPSHOT
restart: on-failure
ports:
- 8030:8030
networks:
- microservice-net
depends_on:
- mysql
- eureka-server
deploy:
replicas: 2
restart_policy:
condition: on-failure
visualizer:
image: dockersamples/visualizer:stable
ports:
- 8081:8080
volumes:
- /var/run/docker.sock:/var/run/docker.sock
deploy:
placement:
constraints: [node.role == manager]
networks:
- microservice-net
networks:
microservice-net:
volumes:
microservice-mysql:
1.2 微服务与Docker的整合
1.对每个子项目编写对应的Dockerfile文件
microservice-eureka-server:服务注册中心
FROM java:8-jre
MAINTAINER shirx <shirx@qq.com>
ADD ./target/microservice-eureka-server-0.0.1-SNAPSHOT.jar /app/microservice-eureka-service.jar
CMD ["java", "-Xmx200m", "-jar", "/app/microservice-eureka-service.jar"]
EXPOSE 8761
microservice-gateway-zuul:API网关
FROM java:8-jre
MAINTAINER shirx <shirx@qq.com>
ADD ./target/microservice-gateway-zuul-0.0.1-SNAPSHOT.jar /app/microservice-gateway-zuul.jar
CMD ["java", "-Xmx200m", "-jar", "/app/microservice-gateway-zuul.jar"]
EXPOSE 8050
microservice-orderservice:订单管理服务
FROM java:8-jre
MAINTAINER shirx <shirx@qq.com>
ADD ./target/microservice-orderservice-0.0.1-SNAPSHOT.jar /app/microservice-orderservice.jar
CMD ["java", "-Xmx200m", "-jar", "/app/microservice-orderservice.jar"]
EXPOSE 7900
microservice-userservice:用户管理服务
FROM java:8-jre
MAINTAINER shirx <shirx@qq.com>
ADD ./target/microservice-userservice-0.0.1-SNAPSHOT.jar \
/app/microservice-userservice.jar
CMD ["java", "-Xmx200m", "-jar", "/app/microservice-userservice.jar"]
EXPOSE 8030
2.在四个子模块中分别添加Dockerfile-maven插件
<build>
<plugins>
<!-- dockerfile plugin -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<!-- 生成的镜像仓库名称 -->
<repository>${docker.image.prefix}/${project.artifactId}</repository>
<!-- 生成的镜像版本 -->
<tag>${project.version}</tag>
<!-- 推送到私有镜像仓库时需要开启安全认证 -->
<useMavenSettingsForAuth>true</useMavenSettingsForAuth>
</configuration>
<!-- 直接使用mvn install命令打包项目,就会自动构建镜像和推送镜像 -->
<executions>
<execution>
<id>default</id>
<phase>install</phase>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
<docker.image.prefix>192.168.10.117:5000</docker.image.prefix>
3.为microservice-mallmanagement添加docker-compose.yml编排文件
version: "3"
services:
mysql:
image: mysql:5.6
restart: on-failure
ports:
- 3306:3306
volumes:
- microservice-mysql:/var/lib/mysql
networks:
- microservice-net
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: microservice_mallmanagement
deploy:
replicas: 1
restart_policy:
condition: on-failure
placement:
constraints: [node.role == manager]
eureka-server:
image: 192.168.10.117:5000/microservice-eureka-server:0.0.1-SNAPSHOT
restart: on-failure
ports:
- 8761:8761
networks:
- microservice-net
deploy:
replicas: 1
restart_policy:
condition: on-failure
gateway-zuul:
image: 192.168.10.117:5000/microservice-gateway-zuul:0.0.1-SNAPSHOT
restart: on-failure
ports:
- 8050:8050
networks:
- microservice-net
depends_on:
- eureka-server
deploy:
replicas: 1
restart_policy:
condition: on-failure
placement:
constraints: [node.role == manager]
order-service:
image: 192.168.10.117:5000/microservice-orderservice:0.0.1-SNAPSHOT
restart: on-failure
ports:
- 7900:7900
networks:
- microservice-net
depends_on:
- mysql
- eureka-server
deploy:
replicas: 2
restart_policy:
condition: on-failure
user-service:
image: 192.168.10.117:5000/microservice-userservice:0.0.1-SNAPSHOT
restart: on-failure
ports:
- 8030:8030
networks:
- microservice-net
depends_on:
- mysql
- eureka-server
deploy:
replicas: 2
restart_policy:
condition: on-failure
visualizer:
image: dockersamples/visualizer:stable
ports:
- 8081:8080
volumes:
- /var/run/docker.sock:/var/run/docker.sock
deploy:
placement:
constraints: [node.role == manager]
networks:
- microservice-net
networks:
microservice-net:
volumes:
microservice-mysql:
1.3 环境搭建以及镜像准备
1.3.1搭建docker主机
docker主机IP:
manager1:192.168.10.117
worker1: 192.168.10.118
worker2: 192.168.10.119
1.3.2 安装应用编译工具JDK
- 下载jdk-8u144-linux-x64.tar.gz安装包
2.上传到主机目录下
3.创建jvm目录
4.解压JDK压缩包
5.将解压包移动到自定义目录jvm下
6.配置JDK环境变量
7.使配置立即生效
8.验证配置是否正确
1.3.3 安装应用打包工具maven
1.解压Apache-maven-3.5.0-bin.tat.gz压缩包
2.移动到/opt目录下
3.配置环境变量
4.使配置立即生效
5. 查看配置信息
1.3.4 镜像准备
1.在apache-maven-3.5.0/conf/setting.xml中配置服务认证信息
2.将微服务项目microservice-mallmanagement复制到manager1服务主机中
3.打包
4.查看镜像列表验证
5.进入本地私有仓库目录验证查看
1.4 微服务的手动部署
1.4.1 非集群环境下的服务部署
1.登录本地私有仓库
2.进入到docker-compose.yml文件所在目录下,执行服务部署命令
3.部署完成后,验证查看
1.4.2 集群环境下的服务部署
1.查看集群网络详情
2.自定义一个以overlay为驱动的网络进行本地集群服务管理
3.在所有需要注册到Eureka注册中心的服务的application文件中,添加优选服务子网地址
spring:
cloud:
inetutils:
preferred-networks:
- 10.0 # 设置注册到Eureka中心的优选服务地址
server:
port: 7900 # 指定该Eureka实例的端口号
eureka:
instance:
prefer-ip-address: true #优选通过IP地址找到对应的服务名称
4.修改服务部署的编排文件docker-compose.yml,将所有服务启动的网络设置为前面预先定义的microservice_net网络来进行管理,将修改后的编排文件重命名为docker-compose-swarm.yml
version: "3"
services:
mysql:
image: mysql:5.6
restart: on-failure
ports:
- 3306:3306
volumes:
- microservice-mysql:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: microservice_mallmanagement
deploy:
replicas: 1
restart_policy:
condition: on-failure
placement:
constraints: [node.role == manager]
eureka-server:
image: 192.168.10.117:5000/microservice-eureka-server:0.0.1-SNAPSHOT
restart: on-failure
ports:
- 8761:8761
deploy:
replicas: 1
restart_policy:
condition: on-failure
gateway-zuul:
image: 192.168.10.117:5000/microservice-gateway-zuul:0.0.1-SNAPSHOT
restart: on-failure
ports:
- 8050:8050
depends_on:
- eureka-server
deploy:
replicas: 1
restart_policy:
condition: on-failure
placement:
constraints: [node.role == manager]
order-service:
image: 192.168.10.117:5000/microservice-orderservice:0.0.1-SNAPSHOT
restart: on-failure
ports:
- 7900:7900
depends_on:
- mysql
- eureka-server
deploy:
replicas: 2
restart_policy:
condition: on-failure
user-service:
image: 192.168.10.117:5000/microservice-userservice:0.0.1-SNAPSHOT
restart: on-failure
ports:
- 8030:8030
depends_on:
- mysql
- eureka-server
deploy:
replicas: 2
restart_policy:
condition: on-failure
visualizer:
image: dockersamples/visualizer:stable
ports:
- 8081:8080
volumes:
- /var/run/docker.sock:/var/run/docker.sock
deploy:
placement:
constraints: [node.role == manager]
networks:
default:
external:
name: microservice_net
volumes:
microservice-mysql:
5.登录私有仓库
6.部署服务
7.查看服务列表详情
8.查看整个微服务项目在集群节点的分配与启动情况
9.查看某个具体服务的日志
1.4.3 微服务测试
1.拉取集群服务可视化工具visualizer
2.通过地址http://192.168.10.117:8081地址查看集群服务可视化工具visualizer界面的显示情况
3.在Eurekka注册中心查看服务的启动情况
4.安装MySQL客户端
5.连接到数据库服务
6.创建数据库及表
7.测试微服务。分别连接用户管理微服务和订单管理微服务的访问地址进行测试
用户管理服务:
订单管理服务:
8.验证测试API网关服务
http://192.168.10.117:7900/order/findOrders/1
http://192.168.10.117:8030/user/findOrders/shitou
http://192.168.10.117:8050/order-service/order/findOrders/1
http://192.168.10.117:8050/user-service/user/findOrders/shitou
1.5 使用jenkins自动部署微服务
1.5.1 Jenkins安装
1.下载Jenkins安装包,并上传到Linux主机中
2.启动Jenkins
3.Jenkins初始化安装
初始化认证密码
4.插件初始化安装
5.初始化成功后效果
1.5.2 集成插件配置
1.安装maven插件
2.系统全局插件配置(安装JDK,Git,maven,docker)
1.5.3 服务自动化部署
1.构建新任务
2.创建项目名,项目类型
3.配置源码仓库地址
4.构建触发器
5.服务发布配置
6.构建任务完成后效果
7.自动化部署服务 单击主页面“立即构建”
8.查看构建历史信息
9.构建成功
10.成功后的项目
二、问题与解决
问题1
在安装JDK插件时,首先使用wget命令在线下载JDK安装包,但是解压时出现如下图问题:
解决方案:
根据报错信息,查看该tar.gz文件类型,查询结果为html文件,并非压缩包,如图:
重新下载安装包到本地,通过XFTP上传到虚拟机中,再次执行该命令,问题解决。
问题2
使用mvn install命令进行打包时,出现如下图报错:
解决方案:
根据报错信息,no basic auth credentials,没有进行认证。于是,进入maven安装目录下的conf/setting.xml配置文件,添加认证信息,如下图,修改完成后,再次打包,成功。
问题3
解决方案:
该问题是由于缺少Git插件,使.git文件无法识别执行导致。所以,安装Git插件,重新构建项目,问题解决。如图:
三、总结
本章主要讲解了有关微服务部署的相关知识,其中涉及Docker Compose编排工具、微服务与Docker的整合、微服务手动部署,以及使用Jenkins完成微服务自动化部署等。