前言
如果你是初学者,那么我之前发布的KVM创建虚拟机一文对一些简单操作有详细讲解,你可以从中入手,本文对之前讲过的操作不会详细说明。
使用的虚拟机是考试提供的xnode1
操作过程
-
关闭防火墙和Selinux
#关闭防火墙并禁止其开机自启 [root@xnode1 ~]# systemctl stop firewalld && systemctl disable firewalld #临时关闭Selinux [root@xnode1 ~]# setenforce 0
-
修改主机名
[root@xnode1 ~]# hostnamectl set-hostname mall #使用bash命令启动一个新会话,生效主机名,你也可以Ctrl+D登出重新登入的方式刷新 [root@xnode1 ~]# bash [root@mall ~]#
-
添加主机映射
#通过echo命令结合重定向>>将字段追加到主机映射文件中 [root@mall ~]# echo "192.168.200.11 mall" >> /etc/hosts [root@mall ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.200.11 mall
-
配置YUM源
#创建挂载目录 [root@mall ~]# mkdir /opt/centos #挂载 [root@mall ~]# mount -o loop CentOS-7-x86_64-DVD-1511.iso /mnt/ mount: /dev/loop2 is write-protected, mounting read-only [root@mall ~]# cp -rvf /mnt/* /opt/centos [root@mall ~]# umount /mnt/ #删除系统自带的默认源 [root@mall ~]# rm -rf /etc/yum.repos.d/* #创建新的仓库文件 [root@mall ~]# vi /etc/yum.repos.d/local.repo [centos] name=centos baseurl=file:///opt/centos enabled=1 gpgcheck=0 [gpmall] name=gpmall baseurl=file:///root/gpmall-repo enabled=1 gpgcheck=0 #检查可用性,查出3888个包就没问题 [root@mall ~]# yum clean all && yum repolist
-
安装配置基础服务
#首先是Java环境 #我们需要安装1.8.0版本的Java,它在gpmall仓库里 [root@mall ~]# yum list | grep java-1.8 java-1.8.0-openjdk.x86_64 1:1.8.0.222.b10-1.el7_7 gpmall java-1.8.0-openjdk-devel.x86_64 1:1.8.0.222.b10-1.el7_7 gpmall java-1.8.0-openjdk-headless.x86_64 1:1.8.0.222.b10-1.el7_7 gpmall #用通配符全下了就行了 [root@mall ~]# yum -y install java-1.8* ... Complete! #安装好后可以看一下java的版本 [root@mall ~]# java -version openjdk version "1.8.0_222" OpenJDK Runtime Environment (build 1.8.0_222-b10) OpenJDK 64-Bit Server VM (build 25.222-b10, mixed mode) #然后是redis缓存服务、Elasticsearch搜索分析服务、Nginx(作为Web服务器)和MariaDB数据库 [root@mall ~]# yum -y install redis elasticsearch nginx mariadb mariadb-server #最后是zookeeper和kafka,它们的软件压缩包在主目录下 [root@mall ~]# ls [kz]* kafka_2.11-1.1.1.tgz zookeeper-3.4.14.tar.gz #一个一个来,首先解压zookeeper软件包 [root@mall ~]# tar -zxf zookeeper-3.4.14.tar.gz #进入其conf目录,将zoo_sample.cfg文件改名为zoo.cfg [root@mall ~]# cd zookeeper-3.4.14/conf/ [root@mall conf]# mv zoo_sample.cfg zoo.cfg #进入其bin目录,运行zkServer.sh脚本,启动服务 [root@mall conf]# cd ../bin/ [root@mall bin]# ./zkServer.sh start ZooKeeper JMX enabled by default Using config: /root/zookeeper-3.4.14/bin/../conf/zoo.cfg Starting zookeeper ... STARTED #再是kafka,我们先回到主目录 [root@mall bin]# cd ~ #解压kafka软件包 [root@mall ~]# tar -zxf kafka_2.11-1.1.1.tgz #进入其bin目录,运行kafka-server-start.sh脚本,启动服务 [root@mall ~]# cd kafka_2.11-1.1.1/bin/ [root@mall bin]# ./kafka-server-start.sh -daemon ../config/server.properties #可以使用jps命令查看服务是否正在运行 #QuorumPeerMain是Zookeeper的进程 [root@mall bin]# jps 3684 Kafka 3749 Jps 3401 QuorumPeerMain #也可以使用netstat -ntpl查看端口开放情况 [root@mall bin]# netstat -ntpl Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1115/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1557/master tcp6 0 0 :::52086 :::* LISTEN 3401/java tcp6 0 0 :::22 :::* LISTEN 1115/sshd tcp6 0 0 ::1:25 :::* LISTEN 1557/master tcp6 0 0 :::9092 :::* LISTEN 3684/java tcp6 0 0 :::2181 :::* LISTEN 3401/java tcp6 0 0 :::55047 :::* LISTEN 3684/java #看见kafka的9092端口和zookeeper的2181端口开放了就行。
-
配置数据库
#修改数据库配置文件,添加字段 [root@mall bin]# cd ~ [root@mall ~]# vi /etc/my.cnf # # This group is read both both by the client and the server # use it for options that affect everything # [client-server] # # include all files from the config directory # !includedir /etc/my.cnf.d [mysqld] init_connect='SET collation_connection = utf8_unicode_ci' #init_connect 连接参数 #collation_connection 指定连接到数据库时的字符集排序规则 init_connect='SET NAMES utf8' #设置编码格式 character-set-server=utf8 #设置服务端默认的内部操作字符集 collation-server=utf8_unicode_ci #设置服务器的默认字符集排序规则 skip-character-set-client-handshake #跳过mysql程序起动时的字符参数设置,使用服务器端字符集设置 #启动数据库服务 [root@mall ~]# systemctl start mariadb #初始化数据库 [root@mall ~]# mysql_secure_installation ... Enter current password for root (enter for none): #此处回车 ... Set root password? [Y/n] #默认选项是Yes,直接回车即可 New password: #输入密码,简便点就好,不要是0开头就行,我这里设置的123456 Re-enter new password: #重新输入密码,确认无误 ... Remove anonymous users? [Y/n] #是否删除匿名用户,直接回车确定 ... Disallow root login remotely? [Y/n] n #是否禁止root远程登录,no否定 ... Remove test database and access to it? [Y/n] #是否删除测试数据库并访问,直接回车确定 ... Reload privilege tables now? [Y/n] #是否立即重新加载权限表,直接回车确定 ... Thanks for using MariaDB! #登录数据库 [root@mall ~]# mysql -uroot -p123456 ... MariaDB [(none)]> #给root用户授权 MariaDB [(none)]> grant all privileges on *.* to root@localhost identified by '123456' with grant option; Query OK, 0 rows affected (0.000 sec) MariaDB [(none)]> grant all privileges on *.* to root@"%" identified by '123456' with grant option; Query OK, 0 rows affected (0.000 sec) #创建gpmall数据库并进入使用 MariaDB [(none)]> create database gpmall; Query OK, 1 row affected (0.001 sec) MariaDB [(none)]> use gpmall; Database changed MariaDB [gpmall]> #导入gpmall.sql数据库脚本文件 MariaDB [gpmall]> source /root/gpmall-single/gpmall.sql ... #可以查看一些是否导入成功了 MariaDB [gpmall]> show tables; +--------------------+ | Tables_in_gpmall | +--------------------+ | tb_address | | tb_base | | tb_comment | | tb_comment_picture | | tb_comment_reply | | tb_dict | | tb_express | | tb_item | | tb_item_cat | | tb_item_desc | | tb_log | | tb_member | | tb_order | | tb_order_item | | tb_order_shipping | | tb_panel | | tb_panel_content | | tb_payment | | tb_refund | | tb_stock | | tb_user_verify | +--------------------+ 21 rows in set (0.001 sec) #退出数据库,也可以直接Ctrl+C MariaDB [gpmall]> quit; Bye #设置数据库服务开机自启 [root@mall ~]# systemctl enable mariadb
-
配置redis
#修改redis配置文件,只修改第61行和第80行,可以在vi编辑器里通过61gg和80gg的快捷键快速跳转到对应行 [root@mall ~]# vi /etc/redis.conf #原先内容是这样的 [root@mall ~]# sed -n '61p;80p' /etc/redis.conf bind 127.0.0.1 protected-mode yes #我们将61行注释掉,将80行改为no,修改后如下 [root@mall ~]# sed -n '61p;80p' /etc/redis.conf #bind 127.0.0.1 protected-mode no #启动和开机自启redis服务 [root@mall ~]# systemctl start redis && systemctl enable redis ...
-
配置elasticsearch
#修改elasticsearch.yml文件 [root@mall ~]# vi /etc/elasticsearch/elasticsearch.yml #在文件最上方写入这三条 http.cors.enabled: true #启用CORS支持,允许Elasticsearch集群处理跨域请求 http.cors.allow-origin: "*" #允许来自任何来源的跨域请求 http.cors.allow-credentials: true #允许在跨域请求中发送身份验证信息 #去掉文件20行、26行、58行、62行(添加上方三条语句后)的注释号,并修改network.host的IP为本机IP #原先内容: [root@mall ~]# sed -n '20p;26p;58p;62p' /etc/elasticsearch/elasticsearch.yml #node.name: node-1 #network.host: 192.168.0.1 #http.port: 9200 #修改后: cluster.name: my-application node.name: node-1 network.host: 192.168.200.11 http.port: 9200 #都做好后,开启Elasticsearch服务并设置开机自启 [root@mall ~]# systemctl start elasticsearch && systemctl enable elasticsearch
-
再次修改主机映射文件
#在原先的基础上添加四行 [root@mall ~]# cat >> /etc/hosts << EOF > 192.168.200.11 kafka.mall > 127.0.0.1 mysql.mall #这里不用环回地址的话,后面数据库连接不上的 > 192.168.200.11 redis.mall > 192.168.200.11 zookeeper.mall > EOF #修改后内容如下 [root@mall ~]# tail -n 5 /etc/hosts 192.168.200.11 mall 192.168.200.11 kafka.mall 127.0.0.1 mysql.mall 192.168.200.11 redis.mall 192.168.200.11 zookeeper.mall
-
部署前端(配置nginx)
#删除nginx默认页面,将提供的dist目录拷贝过去 [root@mall ~]# rm -rf /usr/share/nginx/html/* [root@mall ~]# cp -rf gpmall-single/dist/* /usr/share/nginx/html/ #编辑nginx配置文件 [root@mall ~]# vi /etc/nginx/conf.d/default.conf #根据文件这段注释内容给的格式模板 # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} #我们添加以下三段 location /user { proxy_pass http://127.0.0.1:8082; } location /shopping { proxy_pass http://127.0.0.1:8081; } location /cashier { proxy_pass http://127.0.0.1:8083; } #添加好后启动nginx并设置开机自启 [root@mall ~]# systemctl start nginx && systemctl enable nginx ...
-
部署后端
#先将提供的jar包移动到主目录 [root@mall ~]# mv gpmall-single/*.jar /root/ #然后按顺序启动 [root@mall ~]# nohup java -jar shopping-provider-0.0.1-SNAPSHOT.jar & ... [root@mall ~]# nohup java -jar user-provider-0.0.1-SNAPSHOT.jar & ... [root@mall ~]# nohup java -jar gpmall-shopping-0.0.1-SNAPSHOT.jar & ... [root@mall ~]# nohup java -jar gpmall-user-0.0.1-SNAPSHOT.jar & ... #用jobs命令查看进程,确保4个进程都在运行,没有中途退出 [root@mall ~]# jobs [1] Running nohup java -jar shopping-provider-0.0.1-SNAPSHOT.jar & [2] Running nohup java -jar user-provider-0.0.1-SNAPSHOT.jar & [3]- Running nohup java -jar gpmall-shopping-0.0.1-SNAPSHOT.jar & [4]+ Running nohup java -jar gpmall-user-0.0.1-SNAPSHOT.jar &
-
验收成果
打开浏览器,在地址栏输入http://192.168.200.11/#/home,回车跳转
我们点击右上角的头像登录
输入测试用的账号密码
随便点击一个商品,点击现在购买,测试一下功能
结语
像这种微服务架构的优点与缺点:
优点:
① 每个服务都比较简单,只关注于一个业务功能。
② 微服务架构方式是松耦合的,可以提供更高的灵活性。
③ 微服务可通过最佳及最合适的不同的编程语言与工具进行开发,能够做到有的放矢地解决针对性问题。
④ 每个微服务可由不同团队独立开发,互不影响,加快推出市场的速度。
⑤ 微服务架构是持续交付(CD)的巨大推动力,允许在频繁发布不同服务的同时保持系统其他部分的可用性和稳定性。
缺点:
① 运维开销及成本增加。整体应用可能只需部署至一小片应用服务区集群,而微服务架构可能变成需要构建、测试、部署、运行数十个独立的服务,并可能需要支持多种语言和环境。这导致一个整体式系统如果由20个微服务组成,可能需要40~60个 进程。
② 必须有坚实的DevOps开发运维一体化技能。
③ 隐式接口及接口匹配问题。把系统分为多个协作组件后会产生新的接口,这意味着简单的交叉变化可能需要改变许多组件,并需协调一起发布。在实际环境中,一个新品发布可能被迫同时发布大量服务,由于集成点的大量增加,微服务架构会有更高的发布风险。
④ 代码重复。某些底层功能需要被多个服务所用,为了避免将“同步耦合引入到系统中”,有时需要向不同服务添加一些代码,这就会导致代码重复。
⑤ 分布式系统的复杂性。作为一种分布式系统,微服务引入了复杂性和其他若干问题,例如,网络延迟、容错性、消息序列化、不可靠的网络、异步机制、版本化、差异化的工作负载等。
⑥ 异步机制。微服务往往使用异步编程、消息与并行机制,如果应用存在跨微服务的事务性处理,其实现机制会变得复杂化。
⑦ 可测性的挑战。在动态环境下服务间的交互会产生非常微妙的行为,难以可视化及全面测试。经典微服务往往不太重视测试,更多的是通过监控发现生产环境的异常,进而快速回滚或采取其他必要的行动。但对于特别在意风险规避监管或投产环境 错误会产生显著影响的场景下需要特别注意。