一、项目介绍

本实验是利用Zookeeper分布式协调应用程序、Kafka分布式日志文件系统、Nginx服务器以及后端jar包部署商城应用服务。

二、组件介绍

Kafka
Kafka是最初由Linkedin公司开发,是一个分布式、分区的、多副本的、多订阅者,基于zookeeper协调的分布式日志系统(也可以当做MQ系统),常见可以用于web/nginx日志、访问日志,消息服务等等,Linkedin于2010年贡献给了Apache基金会并成为顶级开源项目。
消息系统
一个消息系统负责将数据从一个应用传递到另外一个应用,应用只需关注于数据,无需关注数据在两个或多个应用间是如何传递的。分布式消息传递基于可靠的消息队列,在客户端应用和消息系统之间异步传递消息。有两种主要的消息传递模式:点对点传递模式、发布-订阅模式。大部分的消息系统选用发布-订阅模式。Kafka就是一种发布-订阅模式。
点对点消息传播
在点对点消息系统中,消息持久化到一个队列中。此时,将有一个或多个消费者消费队列中的数据。但是一条消息只能被消费一次。当一个消费者消费了队列中的某条数据之后,该条数据则从消息队列中删除。该模式即使有多个消费者同时消费数据,也能保证数据处理的顺序。
image.png
与发布-订阅消息传递模式
在发布-订阅消息系统中,消息被持久化到一个topic中。与点对点消息系统不同的是,消费者可以订阅一个或多个topic,消费者可以消费该topic中所有的数据,同一条数据可以被多个消费者消费,数据被消费后不会立马删除。在发布-订阅消息系统中,消息的生产者称为发布者,消费者称为订阅者。
image.png

Zookeeper介绍
ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。ZooKeeper的目标就是封装好复杂易出错的关键服务,将简单易用的接口和性能高效、功能稳定的系统提供给用户。ZooKeeper包含一个简单的原语集,提供Java和C的接口。
ZooKeeper的基本运转流程:
1、选举Leader。
2、同步数据。
3、选举Leader过程中算法有很多,但要达到的选举标准是一致的。
4、Leader要具有最高的执行ID,类似root权限
5、集群中大多数的机器得到响应并接受选出的Leader。
Redis服务介绍
Redis(Remote Dictionary Server ),即远程字典服务,是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。
Elasticsearch介绍
Elasticsearch 是一个分布式、高扩展、高实时的搜索与数据分析引擎。它能很方便的使大量数据具有搜索、分析和探索的能力。充分利用Elasticsearch的水平伸缩性,能使数据在生产环境变得更有价值。Elasticsearch 的实现原理主要分为以下几个步骤,首先用户将数据提交到Elasticsearch 数据库中,再通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据,当用户搜索数据时候,再根据权重将结果排名,打分,再将返回结果呈现给用户。

三、案例部署

1、修改主机名

  1. [root@local ~]# hostnamectl set-hostname mall //修改主机名,修改完成使用ctrl+d退出登录

2、添加主机名映射文件

  1. [root@mall ~]# cat /etc/hosts
  2. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
  3. ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
  4. 192.168.100.11 mall
  5. 192.168.100.11 redis.mall
  6. 192.168.100.11 kafka.mall
  7. 192.168.100.11 zookeeper.mall
  8. 192.168.100.11 mysql.mall
  9. [root@mall ~]#

3、配置Yum源

  1. [root@mall ~]# cat /etc/yum.repos.d/local.repo
  2. [centos]
  3. name=centos
  4. baseurl=file:///opt/centos
  5. gpgcheck=0
  6. enabled=1
  7. [gpmall]
  8. name=gpmall
  9. baseurl=file:///root/gpmall-cloud/gpmall-repo
  10. gpgcheck=0
  11. enabled=1
  12. [root@mall ~]#

4、安装服务

  1. [root@mall ~]# yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel redis nginx mariadb mariadb-server //安装jdk、数据库、缓存、nginx服务

5、关闭防火墙以及selinux

  1. [root@mall ~]# systemctl stop firewalld && systemctl disable firewalld //关闭防火墙并设置开机不自启
  2. Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
  3. Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
  4. [root@mall ~]# setenforce 0 //临时关闭selinux

6、查看jdk版本

  1. [root@mall ~]# java -version
  2. openjdk version "1.8.0_222"
  3. OpenJDK Runtime Environment (build 1.8.0_222-b10)
  4. OpenJDK 64-Bit Server VM (build 25.222-b10, mixed mode)
  5. [root@mall ~]#

7、配置zookeeper服务

  1. [root@mall ~]# cd gpmall-cloud
  2. [root@mall gpmall-cloud]# tar -zxvf zookeeper-3.4.14.tar.gz //解压zookeeper压缩包
  3. [root@mall gpmall-cloud]# cd zookeeper-3.4.14/conf/ //进入zookeeper/conf目录下
  4. [root@mall conf]# mv zoo_sample.cfg zoo.cfg //给zoo_sample文件改名
  5. [root@mall conf]# cd ../bin/ //进入到zookeeper/bin目录下
  6. [root@mall bin]# ./zkServer.sh start //启动zookeeper服务
  7. ZooKeeper JMX enabled by default
  8. Using config: /root/zookeeper-3.4.14/bin/../conf/zoo.cfg
  9. Starting zookeeper ... STARTED
  10. [root@mall bin]# ./zkServer.sh status //查看zookeeper服务状态
  11. ZooKeeper JMX enabled by default
  12. Using config: /root/zookeeper-3.4.14/bin/../conf/zoo.cfg
  13. Mode: standalone

8、配置kafka

  1. [root@mall ~]# cd gpmall-cloud
  2. [root@mall gpmall-cloud]# tar -zxvf kafka_2.11-1.1.1.tgz //解压kafka压缩包
  3. [root@mall ~]# cd kafka_2.11-1.1.1/bin/ //进入bin目录
  4. [root@mall bin]# ./kafka-server-start.sh -daemon ../config/server.properties //启动kafka服务
  5. [root@mall bin]# cd //进入root目录
  6. [root@mall ~]# jps //使用命令查看进程,有kafka服务证明启动无误
  7. 3456 QuorumPeerMain
  8. 3771 Kafka
  9. 3836 Jps
  10. [root@mall ~]# yum -y install net-tools //安装net-tools(此工具用来查看端口号)
  11. [root@mall ~]# netstat -ntpl //查询到端口9092,对应的服务为kafka
  12. Active Internet connections (only servers)
  13. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  14. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1467/sshd
  15. tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1970/master
  16. tcp6 0 0 :::36141 :::* LISTEN 3456/java
  17. tcp6 0 0 :::22 :::* LISTEN 1467/sshd
  18. tcp6 0 0 ::1:25 :::* LISTEN 1970/master
  19. tcp6 0 0 :::47043 :::* LISTEN 3771/java
  20. tcp6 0 0 :::9092 :::* LISTEN 3771/java
  21. tcp6 0 0 :::2181 :::* LISTEN 3456/java

9、配置数据库

  1. [root@mall gpmall-cloud]# vi /etc/my.cnf
  2. [mysqld]
  3. datadir=/var/lib/mysql
  4. socket=/var/lib/mysql/mysql.sock
  5. # Disabling symbolic-links is recommended to prevent assorted secur
  6. ity risks
  7. symbolic-links=0
  8. # Settings user and group are ignored when systemd is used.
  9. # If you need to run mysqld under a different user or group,
  10. # customize your systemd unit file for mariadb according to the
  11. # instructions in http://fedoraproject.org/wiki/Systemd
  12. [mysqld_safe]
  13. log-error=/var/log/mariadb/mariadb.log
  14. pid-file=/var/run/mariadb/mariadb.pid
  15. #
  16. # include all files from the config directory
  17. #
  18. !includedir /etc/my.cnf.d
  19. [mysqld] //添加这块
  20. init_connect='SET collation_connection = utf8_unicode_ci'
  21. init_connect='SET NAMES utf8'
  22. character-set-server=utf8
  23. collation-server=utf8_unicode_ci
  24. skip-character-set-client-handshake
  25. [root@mall ~]# systemctl restart mariadb //重启数据库服务
  26. [root@mall ~]# systemctl enable mariadb //设置数据库开机自启
  27. [root@mall ~]# mysql_secure_installation //初始化数据库服务,使用此命令必须保证数据库已经启动否则无效
  28. NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
  29. SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
  30. In order to log into MariaDB to secure it, we'll need the current
  31. password for the root user. If you've just installed MariaDB, and
  32. you haven't set the root password yet, the password will be blank,
  33. so you should just press enter here.
  34. Enter current password for root (enter for none):
  35. OK, successfully used password, moving on...
  36. Setting the root password ensures that nobody can log into the MariaDB
  37. root user without the proper authorisation.
  38. Set root password? [Y/n] y
  39. New password: //设置密码为123456,必须设置为123456,否则后面对接jar包不成功,造成商城页面不可弹出
  40. Re-enter new password: //再输入一遍123456
  41. Password updated successfully!
  42. Reloading privilege tables..
  43. ... Success!
  44. By default, a MariaDB installation has an anonymous user, allowing anyone
  45. to log into MariaDB without having to have a user account created for
  46. them. This is intended only for testing, and to make the installation
  47. go a bit smoother. You should remove them before moving into a
  48. production environment.
  49. Remove anonymous users? [Y/n] y //询问是否移除匿名用户--y
  50. ... Success!
  51. Normally, root should only be allowed to connect from 'localhost'. This
  52. ensures that someone cannot guess at the root password from the network.
  53. Disallow root login remotely? [Y/n] n //询问是否禁止远程登陆--n
  54. ... skipping.
  55. By default, MariaDB comes with a database named 'test' that anyone can
  56. access. This is also intended only for testing, and should be removed
  57. before moving into a production environment.
  58. Remove test database and access to it? [Y/n] y //询问是否移除匿名用户--y
  59. - Dropping test database...
  60. ... Success!
  61. - Removing privileges on test database...
  62. ... Success!
  63. Reloading the privilege tables will ensure that all changes made so far
  64. will take effect immediately.
  65. Reload privilege tables now? [Y/n] y //询问是否刷新权限--y
  66. ... Success!
  67. Cleaning up...
  68. All done! If you've completed all of the above steps, your MariaDB
  69. installation should now be secure.
  70. Thanks for using MariaDB!
  71. [root@mall ~]# mysql -uroot -p123456 //登录数据库,用户为root,密码为123456
  72. Welcome to the MariaDB monitor. Commands end with ; or \g.
  73. Your MariaDB connection id is 15
  74. Server version: 10.3.18-MariaDB MariaDB Server
  75. Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
  76. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  77. MariaDB [(none)]> grant all privileges on *.* to root@localhost identified by '123456' with grant option; //授权所有用户拥有本地数据库的所有权限
  78. Query OK, 0 rows affected (0.000 sec)
  79. MariaDB [(none)]> grant all privileges on *.* to root@"%" identified by '123456' with grant option; //赋予任何远程主机用root用户连接到mysql 所有库的权限,密码为123456
  80. Query OK, 0 rows affected (0.000 sec)
  81. MariaDB [(none)]> create database gpmall; //创建数据库,名为gpmall
  82. Query OK, 1 row affected (0.000 sec)
  83. MariaDB [(none)]> use gpmall; //进入数据库
  84. Database changed
  85. MariaDB [gpmall]> source /root/gpmall-cloud/gpmall-single/gpmall.sql //将本地数据导入到数据库中
  86. MariaDB [(none)]> Bye
  87. [root@mall ~]#

10、配置redis服务

  1. [root@mall ~]# vi /etc/redis.conf //编辑redis配置文件
  2. 进入文件后,不要做任何操作,直接输入:set nu显示行号,然后将文本改为插入模式,将第61行前加#进行注释,再将第80行中的yes改为no,最后保存退出
  3. [root@mall ~]# systemctl restart redis //更改文件后,重启redis服务
  4. [root@mall ~]# systemctl enable redis //设置redis服务开机自启
  5. [root@mall ~]# netstat -ntpl //查看端口号,找到6379端口,对应的服务为redis
  6. Active Internet connections (only servers)
  7. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  8. tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 10797/redis-server
  9. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 893/sshd
  10. tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1141/master
  11. tcp6 0 0 :::9092 :::* LISTEN 10192/java
  12. tcp6 0 0 :::2181 :::* LISTEN 9869/java
  13. tcp6 0 0 :::3306 :::* LISTEN 10688/mysqld
  14. tcp6 0 0 :::6379 :::* LISTEN 10797/redis-server
  15. tcp6 0 0 :::35279 :::* LISTEN 10192/java
  16. tcp6 0 0 :::41905 :::* LISTEN 9869/java
  17. tcp6 0 0 :::22 :::* LISTEN 893/sshd
  18. tcp6 0 0 ::1:25 :::* LISTEN 1141/master

11、配置nginx服务

  1. [root@mall ~]# rm -rf /usr/share/nginx/html/* //删除Nginx服务站点目录下所有内容
  2. [root@mall ~]# cp -rvf gpmall-single/dist/* /usr/share/nginx/html/ //将本地web包拷贝到站点目录下
  3. [root@mall ~]# vi /etc/nginx/conf.d/default.conf //编辑nginx的配置文件
  4. 进入配置文件后找到第一个location,将下面内容添加进去
  5. location /user {
  6. proxy_pass http://127.0.0.1:8082;
  7. }
  8. location /shopping {
  9. proxy_pass http://127.0.0.1:8081;
  10. }
  11. location /cashier {
  12. proxy_pass http://127.0.0.1:8083;
  13. }
  14. [root@mall ~]# systemctl restart nginx //添加完成后重启nginx服务,如果遇到服务报错,则检查Nginx配置文件是否正确
  15. [root@mall ~]# netstat -ntpl //查看端口号,有无80端口暴露,对应服务为nginx
  16. Active Internet connections (only servers)
  17. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  18. tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 10797/redis-server
  19. tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 10829/nginx: master
  20. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 893/sshd
  21. tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1141/master
  22. tcp6 0 0 :::9092 :::* LISTEN 10192/java
  23. tcp6 0 0 :::2181 :::* LISTEN 9869/java
  24. tcp6 0 0 :::3306 :::* LISTEN 10688/mysqld
  25. tcp6 0 0 :::6379 :::* LISTEN 10797/redis-server
  26. tcp6 0 0 :::35279 :::* LISTEN 10192/java
  27. tcp6 0 0 :::41905 :::* LISTEN 9869/java
  28. tcp6 0 0 :::22 :::* LISTEN 893/sshd
  29. tcp6 0 0 ::1:25 :::* LISTEN 1141/master

12、部署后端jar包

  1. [root@mall ~]# cd gpmall-cloud/gpmall-single/ //进入存放后端jar包文件目录
  2. [root@mall gpmall-single]# ls
  3. dist
  4. gpmall-shopping-0.0.1-SNAPSHOT.jar
  5. gpmall.sql
  6. gpmall-user-0.0.1-SNAPSHOT.jar
  7. nohup.out
  8. shopping-provider-0.0.1-SNAPSHOT.jar
  9. user-provider-0.0.1-SNAPSHOT.jar
  10. [root@mall gpmall-single]#
  11. [root@mall ~]# gpmall-single]# nohup java -jar shopping-provider-0.0.1-SNAPSHOT.jar & //分别在后台,启动下面四个jar包
  12. [root@mall ~]# gpmall-single]# nohup java -jar user-provider-0.0.1-SNAPSHOT.jar &
  13. [root@mall ~]# gpmall-single]# nohup java -jar gpmall-shopping-0.0.1-SNAPSHOT.jar &
  14. [root@mall ~]# gpmall-single]# nohup java -jar gpmall-user-0.0.1-SNAPSHOT.jar &
  15. [root@mall gpmall-single]# jobs
  16. [1] Running nohup java -jar shopping-provider-0.0.1-SNAPSHOT.jar &
  17. [2] Running nohup java -jar user-provider-0.0.1-SNAPSHOT.jar &
  18. [3]- Running nohup java -jar gpmall-shopping-0.0.1-SNAPSHOT.jar &
  19. [4]+ Running nohup java -jar gpmall-user-0.0.1-SNAPSHOT.jar &
  20. [root@mall gpmall-single]# jps
  21. 10192 Kafka
  22. 10835 jar
  23. 10932 jar
  24. 10869 jar
  25. 10902 jar
  26. 10969 Jps
  27. 9869 QuorumPeerMain
  28. 注意:如果在命令启动过程中遇到EXIT字样,则为jar包出现闪退,检查前面所部署的服务是否运行正常,以及主机名映射文件是否书写正确

13、浏览器登录验证

如果前面每个服务都运行正常,且jar包没有出现闪退,那么使用浏览器输入192.168.100.11进行验证,如果发现有图片并且有文字出现,则gpmall商城部署正常。
image.png

14、将gpmall前端页面使用命令获取

  1. [root@mall ~]# curl http://192.168.100.11/#/home
  2. <!DOCTYPE html><html><head><meta charset=utf-8><title>1+x-示例项目</title><meta name=keywords content=""><meta name=description content=""><meta http-equiv=X-UA-Compatible content="IE=Edge"><meta name=wap-font-scale content=no><link rel="shortcut icon " type=images/x-icon href=/static/images/favicon.ico><link href=/static/css/app.8d4edd335a61c46bf5b6a63444cd855a.css rel=stylesheet></head><body><div id=app></div><script type=text/javascript src=/static/js/manifest.2d17a82764acff8145be.js></script><script type=text/javascript src=/static/js/vendor.4f07d3a235c8a7cd4efe.js></script><script type=text/javascript src=/static/js/app.81180cbb92541cdf912f.js></script></body></html><style>body{
  3. min-width:1250px;
  4. }</style>[root@mall ~]#