# 关闭防火墙
systemctl stop firewalld.service
# 禁止防火墙开机启动
systemctl disable firewalld.service
只要网络环境变化,防火墙的关闭,ip地址变化等都需要重启docker
# 启动docker
systemctl start docker
# 重启docker
systemctl restart docker
docker load < redis-docker-image.gz
docker run -d --name redis7000 -p 7000:6379 redis
docker run -d --name redis7000(容器的名字) -p 7000:6379(映射的端口容器的端口:redis端口) redis
#启动容器后,进入容器访问检测
docker exec -it redis7000(redis7000容器) redis-cli(连接redis客户端)
docker exec -it redis7000 redis-cli
#测试没有问题后加上70001和7002
docker run -d --name redis7001 -p 7001:6379 redis
docker run -d --name redis7002 -p 7002:6379 redis
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.tedu</groupId>
<artifactId>docker</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
编写测试类
package test;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
import java.util.ArrayList;
import java.util.List;
public class Test1 {
public static void main(String[] args) {
//配置对象
JedisPoolConfig config=new JedisPoolConfig();
//服务器列表
List<JedisShardInfo> list=new ArrayList<>();
list.add(new JedisShardInfo("192.168.64.150",7000));
list.add(new JedisShardInfo("192.168.64.150",7001));
list.add(new JedisShardInfo("192.168.64.150",7002));
//创建ShardJedisPool对象
ShardedJedisPool pool=new ShardedJedisPool(config,list);
ShardedJedis j=pool.getResource();
for (int i=0;i <100;i++) {
j.set("key"+i,"value"+i);
}
pool.close();
}
}
在容器中查看数据
分别进入三个redis容器,执行 keys *查看结果
docker exec -it redis7000 redis-cli
docker exec -it redis7001 redis-cli
docker exec -it redis7002 redis-cli
启动多个 redis 容器
启动三个redis容器,将端口分别映射到7000,7001和7002端口
docker rm -f redis7000 redis7001 redis7002
删除以前的虚拟网络
docker network rm my-net
准备虚拟网络:
docker network create dockernet --subnet=172.18.0.0/24
docker network ls
docker inspect dockernet
http://hub.docker.com
数据库数据挂载:
端口的映射
宿主机3306
root密码的设置
设置五个参数:
docker volume create mysql-data
docker volume ls
docker inspect mysql-data
docker run -d --name mysql --restart=always \
-v mysql-data:/var/lib/mysql \ 数据卷挂载
-p 3306:3306 \ 端口的连接
-e MYSQL_ROOT_PASSWORD=root \ 指定密码
--net dockernet \ 连接到网络
--ip 172.18.0.11 \ 指定固定ip
mariadb
docker run -d --name mysql --restart=always \
-v mysql-data:/var/lib/mysql \
-p 3306:3306 \
-e MYSQL_ROOT_PASSWORD=root \
--net dockernet \
--ip 172.18.0.11 \
mariadb
#进入容器登陆mysql
docker exec -it mysql mysql -uroot -p
#或者测试
docker exec -it mysql bash
mysql -uroot -p
/*
SQLyog Ultimate v8.32
MySQL - 5.0.22-community-nt : Database - easymall
*********************************************************************
*/
/*!40101 SET NAMES utf8 */;
/*!40101 SET SQL_MODE=''*/;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`easymall` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `easymall`;
/*Table structure for table `orderitem` */
DROP TABLE IF EXISTS `orderitem`;
CREATE TABLE `orderitem` (
`order_id` varchar(100) NOT NULL default '',
`product_id` varchar(100) NOT NULL default '',
`buynum` int(11) default NULL,
PRIMARY KEY (`order_id`,`product_id`),
KEY `product_id` (`product_id`),
CONSTRAINT `orderitem_ibfk_1` FOREIGN KEY (`order_id`) REFERENCES `orders` (`id`),
CONSTRAINT `orderitem_ibfk_2` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*Data for the table `orderitem` */
insert into `orderitem`(`order_id`,`product_id`,`buynum`) values ('1dc4b5c7-2ef3-41ec-b149-6101efb52c2e','a84fc7b3-944d-4b30-b67d-eba5cd2b326a',900),('2789c695-8f45-40fe-bba6-1bd399efddd2','103e5414-0da2-4fba-b92f-0ba876e08939',1),('2789c695-8f45-40fe-bba6-1bd399efddd2','258554f0-2cce-4cd1-9db3-139f6228b318',1),('294918ca-06f3-4209-b0b5-c0e2652f0a37','103e5414-0da2-4fba-b92f-0ba876e08939',1),('294918ca-06f3-4209-b0b5-c0e2652f0a37','2ad0d041-8c5f-4b70-a0ef-1ca2fd476dba',1),('31bdf06b-ca3e-4398-b966-595a4868a123','103e5414-0da2-4fba-b92f-0ba876e08939',100),('31bdf06b-ca3e-4398-b966-595a4868a123','258554f0-2cce-4cd1-9db3-139f6228b318',100),('3311b6ba-69d9-4563-b239-3bdf944aae3f','103e5414-0da2-4fba-b92f-0ba876e08939',1),('3311b6ba-69d9-4563-b239-3bdf944aae3f','26128d47-423b-4220-8047-544ff899db50',1),('36365b01-1b7d-400f-a778-de5b38aabb32','103e5414-0da2-4fba-b92f-0ba876e08939',1),('3f214bcd-3cb7-4dea-b205-15a72b77a13f','36b9407f-746a-4956-988e-557122bc74d0',1),('3f214bcd-3cb7-4dea-b205-15a72b77a13f','a84fc7b3-944d-4b30-b67d-eba5cd2b326a',50),('3f214bcd-3cb7-4dea-b205-15a72b77a13f','b1f9c947-4f72-4245-b09d-8c5a8c311ae1',1),('43d824e6-3a93-4c1b-948d-b16ad02804d1','09f47493-214d-44bc-927d-6ce0bf89a057',1),('43d824e6-3a93-4c1b-948d-b16ad02804d1','17c3f20e-ef86-4857-9293-f29e52954a95',3),('4745c002-5aa9-4d2b-acc6-2e2926ba82b0','103e5414-0da2-4fba-b92f-0ba876e08939',1),('4745c002-5aa9-4d2b-acc6-2e2926ba82b0','2ad0d041-8c5f-4b70-a0ef-1ca2fd476dba',1),('49e8dd1b-1ab3-42d3-b79f-788958fd71ef','103e5414-0da2-4fba-b92f-0ba876e08939',1),('49e8dd1b-1ab3-42d3-b79f-788958fd71ef','26128d47-423b-4220-8047-544ff899db50',1),('4cc513f4-c67c-4f95-a175-49858768233b','103e5414-0da2-4fba-b92f-0ba876e08939',1),('4cc513f4-c67c-4f95-a175-49858768233b','2ad0d041-8c5f-4b70-a0ef-1ca2fd476dba',1),('4d1a0d4e-672d-45b0-94a0-d2ec3ee24daa','103e5414-0da2-4fba-b92f-0ba876e08939',3),('4d1a0d4e-672d-45b0-94a0-d2ec3ee24daa','258554f0-2cce-4cd1-9db3-139f6228b318',20),('4d1a0d4e-672d-45b0-94a0-d2ec3ee24daa','34ff133c-8ff5-433b-86d9-ec86217d203d',30),('548d1455-5113-4e8e-8bb8-34e84d779cde','09f47493-214d-44bc-927d-6ce0bf89a057',1),('548d1455-5113-4e8e-8bb8-34e84d779cde','17c3f20e-ef86-4857-9293-f29e52954a95',23),('66def42f-1391-4ff4-97d7-ac93ed55f87f','103e5414-0da2-4fba-b92f-0ba876e08939',1),('66def42f-1391-4ff4-97d7-ac93ed55f87f','2ad0d041-8c5f-4b70-a0ef-1ca2fd476dba',1),('66e3231d-102c-4075-a14f-e18e97575e2e','05e20c1a-0401-4c0a-82ab-6fb0f37db397',1),('686b5794-3bd2-4fd6-bef1-7cb65a1dcd22','103e5414-0da2-4fba-b92f-0ba876e08939',1),('70baa50d-e105-4320-9792-5c558235c182','103e5414-0da2-4fba-b92f-0ba876e08939',1),('72b227da-1af0-4360-a1da-562469e51f88','103e5414-0da2-4fba-b92f-0ba876e08939',1),('72b227da-1af0-4360-a1da-562469e51f88','26128d47-423b-4220-8047-544ff899db50',1),('7ccd887f-9c46-4d0b-a7d3-707ea670bf1d','103e5414-0da2-4fba-b92f-0ba876e08939',1),('7ccd887f-9c46-4d0b-a7d3-707ea670bf1d','26128d47-423b-4220-8047-544ff899db50',1),('7f2872b2-7474-42e7-8a69-fcb54b1b316f','103e5414-0da2-4fba-b92f-0ba876e08939',2),('7f2872b2-7474-42e7-8a69-fcb54b1b316f','2ad0d041-8c5f-4b70-a0ef-1ca2fd476dba',1),('85f51b3c-a851-4be1-b5f2-a0b1acbfd9b8','103e5414-0da2-4fba-b92f-0ba876e08939',1),('85f51b3c-a851-4be1-b5f2-a0b1acbfd9b8','26128d47-423b-4220-8047-544ff899db50',1),('89b7ebb5-28a7-470b-bc31-c5dcd304b953','103e5414-0da2-4fba-b92f-0ba876e08939',1),('89b7ebb5-28a7-470b-bc31-c5dcd304b953','26128d47-423b-4220-8047-544ff899db50',1),('8af7af6c-5690-4857-92c1-d64a5d320131','103e5414-0da2-4fba-b92f-0ba876e08939',1),('8af7af6c-5690-4857-92c1-d64a5d320131','2ad0d041-8c5f-4b70-a0ef-1ca2fd476dba',1),('92a75c42-5254-4896-9c99-da3f8c5a52c9','103e5414-0da2-4fba-b92f-0ba876e08939',1),('9531164b-f2f0-4adb-9faf-6e96b3679dd0','09f47493-214d-44bc-927d-6ce0bf89a057',1),('96927957-0c71-403a-980f-d73553327aba','103e5414-0da2-4fba-b92f-0ba876e08939',1),('9ab2cca6-8951-48d8-8445-b59b45683245','a84fc7b3-944d-4b30-b67d-eba5cd2b326a',2),('9c35d974-dc8f-4e0f-ac94-d0debf3ed05d','103e5414-0da2-4fba-b92f-0ba876e08939',1),('9c35d974-dc8f-4e0f-ac94-d0debf3ed05d','26128d47-423b-4220-8047-544ff899db50',1),('a063606d-8da9-4ae3-a8f6-f34f4d0948e4','103e5414-0da2-4fba-b92f-0ba876e08939',2),('a063606d-8da9-4ae3-a8f6-f34f4d0948e4','2ad0d041-8c5f-4b70-a0ef-1ca2fd476dba',1),('ac5defdb-c193-4ff2-b2d8-d8bd8b36742d','103e5414-0da2-4fba-b92f-0ba876e08939',1),('adec3640-87a8-4897-888b-f505b1441746','103e5414-0da2-4fba-b92f-0ba876e08939',1),('adec3640-87a8-4897-888b-f505b1441746','2ad0d041-8c5f-4b70-a0ef-1ca2fd476dba',1),('b3e0f0f0-afac-464a-b6d4-3a00d7886736','103e5414-0da2-4fba-b92f-0ba876e08939',1),('b58e6909-4eee-442f-ba42-5eb6b50aa1f3','d73ab7ed-9f78-4775-a93b-4d355b2d5fc0',2),('b6454235-bba9-470c-948b-6d02c5d7bc25','103e5414-0da2-4fba-b92f-0ba876e08939',1),('b716f817-fb29-46b3-b5ea-93b519efa293','103e5414-0da2-4fba-b92f-0ba876e08939',1),('ba08aa4b-6335-45e3-a038-a7c0edec5bc1','103e5414-0da2-4fba-b92f-0ba876e08939',1),('ba08aa4b-6335-45e3-a038-a7c0edec5bc1','26128d47-423b-4220-8047-544ff899db50',1),('c2665c92-d6ee-4bda-9cd7-9cc377214d9d','09f47493-214d-44bc-927d-6ce0bf89a057',1),('c2665c92-d6ee-4bda-9cd7-9cc377214d9d','103e5414-0da2-4fba-b92f-0ba876e08939',1),('c32e1f5f-e188-4446-9ea2-4469c5f57492','103e5414-0da2-4fba-b92f-0ba876e08939',50),('c32e1f5f-e188-4446-9ea2-4469c5f57492','17c3f20e-ef86-4857-9293-f29e52954a95',100),('c32e1f5f-e188-4446-9ea2-4469c5f57492','258554f0-2cce-4cd1-9db3-139f6228b318',100),('c41524e9-53c3-4830-9c17-6d5d41c4ba7c','05e20c1a-0401-4c0a-82ab-6fb0f37db397',1),('caffb69e-5d0a-43de-9218-fb8b6d794f7a','103e5414-0da2-4fba-b92f-0ba876e08939',1),('caffb69e-5d0a-43de-9218-fb8b6d794f7a','26128d47-423b-4220-8047-544ff899db50',1),('ccd56972-dedf-4b52-b690-606de6ad1ced','21ec8c05-a91e-4993-9dd0-7a032ec1d824',10),('ccd56972-dedf-4b52-b690-606de6ad1ced','258554f0-2cce-4cd1-9db3-139f6228b318',779),('d1fcfff8-ccb6-4134-abe0-edc6bf1ee569','09f47493-214d-44bc-927d-6ce0bf89a057',1),('d1fcfff8-ccb6-4134-abe0-edc6bf1ee569','103e5414-0da2-4fba-b92f-0ba876e08939',1),('d1fcfff8-ccb6-4134-abe0-edc6bf1ee569','17c3f20e-ef86-4857-9293-f29e52954a95',1),('d4342f85-b046-4cd1-8a11-b841943b196d','09f47493-214d-44bc-927d-6ce0bf89a057',1),('d4342f85-b046-4cd1-8a11-b841943b196d','103e5414-0da2-4fba-b92f-0ba876e08939',1),('d4342f85-b046-4cd1-8a11-b841943b196d','17c3f20e-ef86-4857-9293-f29e52954a95',1),('d4954039-8fa4-4c9f-bd91-c5a919b6aa2d','2ad0d041-8c5f-4b70-a0ef-1ca2fd476dba',1),('d4a83695-3182-4555-8e61-a8c334027efa','103e5414-0da2-4fba-b92f-0ba876e08939',1),('d4a83695-3182-4555-8e61-a8c334027efa','2ad0d041-8c5f-4b70-a0ef-1ca2fd476dba',1),('d71dd880-bd78-48a7-8a1e-f158bf92e766','103e5414-0da2-4fba-b92f-0ba876e08939',1),('d71dd880-bd78-48a7-8a1e-f158bf92e766','2ad0d041-8c5f-4b70-a0ef-1ca2fd476dba',1),('d907990b-0286-45d4-b51e-77e1af6281ca','05e20c1a-0401-4c0a-82ab-6fb0f37db397',5),('dd372acf-f8ce-4185-9ba4-706565db635d','2ad0d041-8c5f-4b70-a0ef-1ca2fd476dba',23000),('dd6691c4-3872-4bcf-b168-eec9611db4b1','103e5414-0da2-4fba-b92f-0ba876e08939',1),('dd6691c4-3872-4bcf-b168-eec9611db4b1','26128d47-423b-4220-8047-544ff899db50',1),('de5f212e-b614-4061-aa86-c238e56e04ef','103e5414-0da2-4fba-b92f-0ba876e08939',1),('e5773553-fa93-4ab1-b75f-4eefeadc2280','103e5414-0da2-4fba-b92f-0ba876e08939',1),('e952b2f4-bc3e-4cf3-bccf-296cb72d3635','26128d47-423b-4220-8047-544ff899db50',1),('e952b2f4-bc3e-4cf3-bccf-296cb72d3635','2ad0d041-8c5f-4b70-a0ef-1ca2fd476dba',1),('ed5f7326-065d-41c1-a979-86fbe99932b1','103e5414-0da2-4fba-b92f-0ba876e08939',2),('ed5f7326-065d-41c1-a979-86fbe99932b1','2ad0d041-8c5f-4b70-a0ef-1ca2fd476dba',1),('ee6d591b-8f70-4afa-83df-18e26bc7933e','d8cb845e-37f6-4515-9fc1-dea07719ee06',100000),('fe47e4df-acd2-4e32-93e5-737116602c88','09f47493-214d-44bc-927d-6ce0bf89a057',1),('fe47e4df-acd2-4e32-93e5-737116602c88','103e5414-0da2-4fba-b92f-0ba876e08939',1);
/*Table structure for table `orders` */
DROP TABLE IF EXISTS `orders`;
CREATE TABLE `orders` (
`id` varchar(100) NOT NULL,
`money` double default NULL,
`receiverinfo` varchar(255) default NULL,
`paystate` int(11) default NULL,
`ordertime` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
`user_id` int(11) default NULL,
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
CONSTRAINT `orders_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*Data for the table `orders` */
insert into `orders`(`id`,`money`,`receiverinfo`,`paystate`,`ordertime`,`user_id`) values ('1dc4b5c7-2ef3-41ec-b149-6101efb52c2e',9000,'111906',0,'2016-11-19 16:32:24',39),('2789c695-8f45-40fe-bba6-1bd399efddd2',2500,'111605',0,'2016-11-21 16:58:21',35),('294918ca-06f3-4209-b0b5-c0e2652f0a37',7888,'2017011301测试',0,'2017-01-13 11:27:32',35),('31bdf06b-ca3e-4398-b966-595a4868a123',250000,'ASDFGFUIY ',1,'2016-11-19 16:27:50',38),('3311b6ba-69d9-4563-b239-3bdf944aae3f',2012,'连云港码头',0,'2016-12-19 11:35:59',33),('36365b01-1b7d-400f-a778-de5b38aabb32',2000,'20170114031测试\r\n }',0,'2017-01-14 16:55:53',35),('3f214bcd-3cb7-4dea-b205-15a72b77a13f',5505,'11111119',0,'2016-11-19 16:28:09',39),('43d824e6-3a93-4c1b-948d-b16ad02804d1',1540,'土豪\r\n河北省张家口河北建筑工程学院***\r\n13812345678',0,'2017-04-06 19:16:32',48),('4745c002-5aa9-4d2b-acc6-2e2926ba82b0',7888,'111602dfas ',0,'2016-11-21 16:58:23',35),('49e8dd1b-1ab3-42d3-b79f-788958fd71ef',2012,'20170113041测试',0,'2017-01-13 17:01:12',35),('4cc513f4-c67c-4f95-a175-49858768233b',7888,'bbbbbbbbbbbbbbb1214',1,'2016-12-16 16:54:28',17),('4d1a0d4e-672d-45b0-94a0-d2ec3ee24daa',23500,'111901',1,'2016-11-19 16:27:53',35),('548d1455-5113-4e8e-8bb8-34e84d779cde',5140,'测试20170406-001',0,'2017-04-06 17:25:01',22),('66def42f-1391-4ff4-97d7-ac93ed55f87f',7888,'天安门01',0,'2016-12-13 11:08:04',17),('66e3231d-102c-4075-a14f-e18e97575e2e',9000000,'sdasdsadsa',1,'2016-11-19 16:27:54',36),('686b5794-3bd2-4fd6-bef1-7cb65a1dcd22',2180,'0222-1-1-测试02',1,'2017-02-25 16:32:05',22),('70baa50d-e105-4320-9792-5c558235c182',2000,'地方撒发生地方v',0,'2016-12-08 10:11:59',25),('72b227da-1af0-4360-a1da-562469e51f88',2192,'北京市022-3-3-3-3-3-测试03',0,'2017-02-21 16:56:00',22),('7ccd887f-9c46-4d0b-a7d3-707ea670bf1d',2012,'北京天安门',0,'2016-12-19 11:10:17',35),('7f2872b2-7474-42e7-8a69-fcb54b1b316f',9888,'2017014final1',0,'2017-01-14 17:09:28',35),('81fa87db-666a-4a4e-878d-d1f296044967',0,'1211406_01',0,'2016-12-14 15:33:56',17),('85f51b3c-a851-4be1-b5f2-a0b1acbfd9b8',2012,'0222-3-3-3测试301',0,'2017-02-22 15:24:21',22),('89b7ebb5-28a7-470b-bc31-c5dcd304b953',2012,'1214_05',0,'2016-12-14 15:18:52',17),('8af7af6c-5690-4857-92c1-d64a5d320131',7888,'1211406_01',0,'2016-12-14 15:23:20',17),('92a75c42-5254-4896-9c99-da3f8c5a52c9',2000,'fdasfsa',0,'2016-12-13 16:54:53',17),('9531164b-f2f0-4adb-9faf-6e96b3679dd0',1000,'土豪02\r\n河北省张家口河北建筑工程学院***\r\n13812345678',1,'2017-04-06 19:23:18',48),('96927957-0c71-403a-980f-d73553327aba',2000,'aaaaaaaa',0,'2017-01-09 22:58:47',33),('9ab2cca6-8951-48d8-8445-b59b45683245',20,'北京市亦庄经济开发区21701',0,'2017-02-17 09:18:29',35),('9c35d974-dc8f-4e0f-ac94-d0debf3ed05d',2012,'北京市0221-2-2-2-2-测试02',0,'2017-02-21 16:52:55',22),('a063606d-8da9-4ae3-a8f6-f34f4d0948e4',9888,'fdsafas',0,'2016-12-14 15:10:58',17),('ac5defdb-c193-4ff2-b2d8-d8bd8b36742d',2000,'fds',1,'2016-11-21 22:56:26',41),('adec3640-87a8-4897-888b-f505b1441746',7888,'ddddddddd121411',0,'2016-12-14 16:18:06',17),('b3e0f0f0-afac-464a-b6d4-3a00d7886736',2180,'0222-1-1-测试01',0,'2017-02-22 14:19:00',22),('b58e6909-4eee-442f-ba42-5eb6b50aa1f3',8888,'北京市亦庄开发区。。。。',0,'2017-01-10 09:33:19',33),('b6454235-bba9-470c-948b-6d02c5d7bc25',2000,'fdsafsa',0,'2016-11-21 17:03:40',41),('b716f817-fb29-46b3-b5ea-93b519efa293',2000,'2017014051测试',0,'2017-01-14 17:05:06',35),('ba08aa4b-6335-45e3-a038-a7c0edec5bc1',2012,'20170114021测试',0,'2017-01-14 16:52:48',35),('c2665c92-d6ee-4bda-9cd7-9cc377214d9d',3000,'测试0322-0777',0,'2017-03-22 15:43:17',34),('c32e1f5f-e188-4446-9ea2-4469c5f57492',168000,'ASE LKIHDGK ',0,'2016-11-19 16:28:16',38),('c41524e9-53c3-4830-9c17-6d5d41c4ba7c',9000000,'121601',0,'2016-12-16 11:25:29',17),('caffb69e-5d0a-43de-9218-fb8b6d794f7a',2012,'1214_03',0,'2016-12-14 15:17:15',17),('ccd56972-dedf-4b52-b690-606de6ad1ced',390380,'',0,'2016-11-19 16:28:46',41),('d1fcfff8-ccb6-4134-abe0-edc6bf1ee569',3180,'测试0322-0666',1,'2017-03-24 16:02:59',34),('d4342f85-b046-4cd1-8a11-b841943b196d',3180,'测试0322-08888',0,'2017-03-22 15:45:28',34),('d4954039-8fa4-4c9f-bd91-c5a919b6aa2d',5888,'112201',1,'2016-11-22 09:23:40',41),('d4a83695-3182-4555-8e61-a8c334027efa',7888,'钓鱼岛',0,'2016-12-13 11:10:52',17),('d71dd880-bd78-48a7-8a1e-f158bf92e766',7888,'2017011401测试',0,'2017-01-14 09:35:35',35),('d907990b-0286-45d4-b51e-77e1af6281ca',45000000,'',0,'2016-12-19 11:44:09',45),('dd372acf-f8ce-4185-9ba4-706565db635d',135424000,'',0,'2016-11-19 16:32:40',38),('dd6691c4-3872-4bcf-b168-eec9611db4b1',2192,'0224-3-3-3-3测试301',0,'2017-02-24 15:56:16',22),('de5f212e-b614-4061-aa86-c238e56e04ef',2000,'121305',0,'2016-12-13 17:16:17',17),('e5773553-fa93-4ab1-b75f-4eefeadc2280',2000,'北京市亦庄经济开发区000101001-1',0,'2017-03-14 09:24:54',22),('e952b2f4-bc3e-4cf3-bccf-296cb72d3635',5900,'2017011601测试',1,'2017-01-16 17:07:38',35),('ed5f7326-065d-41c1-a979-86fbe99932b1',9888,'2017011302测试',0,'2017-01-13 11:31:41',35),('ee6d591b-8f70-4afa-83df-18e26bc7933e',0,'',0,'2016-11-19 16:38:50',41),('fe47e4df-acd2-4e32-93e5-737116602c88',3000,'0321-测试-077777',1,'2017-03-24 16:20:09',34);
/*Table structure for table `products` */
DROP TABLE IF EXISTS `products`;
CREATE TABLE `products` (
`id` varchar(100) NOT NULL,
`name` varchar(255) default NULL,
`price` double default NULL,
`category` varchar(255) default NULL,
`pnum` int(11) default NULL,
`imgurl` varchar(255) default NULL,
`description` varchar(255) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*Data for the table `products` */
insert into `products`(`id`,`name`,`price`,`category`,`pnum`,`imgurl`,`description`) values ('05e20c1a-0401-4c0a-82ab-6fb0f37db397','靳老师别墅',9000000,'电子数码',206,'/WEB-INF/upload/5/e/d/5/4/5/e/b/5f0d34dc-157f-49ba-ad39-1b28927ba6ae_1005714.jpg','贵族的生活,美女很多'),('09f47493-214d-44bc-927d-6ce0bf89a057','爱疯9S(0315-01)',1000,'电子数码',183,'/WEB-INF/upload/5/2/3/4/7/8/d/c/1838eaa6-6459-420f-b8e2-6ea9f43c4b5e_dfd259ab-bcc7-43f6-a9d5-62872ff5671e.jpg','爱疯9S(0315-01)'),('103e5414-0da2-4fba-b92f-0ba876e08939','滑雪套装',2000,'日用百货',676,'/WEB-INF/upload/4/d/2/a/2/3/1/8/b3c3fc7a-222c-49be-9491-f466553d2284_386718.jpg','这种运动值得你拥有'),('17c3f20e-ef86-4857-9293-f29e52954a95','打印机',180,'电子数码',0,'/WEB-INF/upload/7/c/b/f/7/d/2/9/5e229aef-063f-4d0d-91df-2d4aa7167670_6f84843a-1d1e-49c7-b4ce-c035d7790171.jpg','一个神奇的打印机?!'),('21ec8c05-a91e-4993-9dd0-7a032ec1d824','护手霜',88,'电子数码',1234,'/WEB-INF/upload/8/f/b/2/1/0/1/f/870afdb3-c754-4dea-85cf-59688007f2b9_c987f2c1-4123-4d87-83bd-fe2fb221e272.jpg','让你的手变得拜拜嫩嫩'),('258554f0-2cce-4cd1-9db3-139f6228b318','吃瓜群众',500,'日用百货',0,'/WEB-INF/upload/8/2/1/a/9/e/3/4/6e0b2162-b78a-42fc-ac04-323e506c66f4_girl.jpg','吃瓜群众!!!!!!!!!!!'),('26128d47-423b-4220-8047-544ff899db50','1208_01iphone7',12,'电子数码',986,'/WEB-INF/upload/2/1/0/3/f/0/3/5/2fb0b43b-4dbe-440b-899b-13c02a9f5475_22d124c9-df52-4cd4-88b3-691005f1cafe.jpg','测试商品'),('2ad0d041-8c5f-4b70-a0ef-1ca2fd476dba','战神主机',5888,'电子数码',200,'/WEB-INF/upload/b/4/1/b/d/9/8/8/e41cd642-2fc2-4cb2-b20a-f8a78405eee2_e9dd0d91-40c1-4db5-a888-244e825e9ce4.jpg',''),('34ff133c-8ff5-433b-86d9-ec86217d203d','愤怒的小蝌蚪',250,'图书杂志',20,'/WEB-INF/upload/8/3/7/9/0/a/2/2/63537720-202c-4862-bfcf-f5c90ca94669_Jellyfish.jpg','本书详细阐述了生命的真谛,数量有限,预购从速!!!!!'),('36b9407f-746a-4956-988e-557122bc74d0','banana',5,'日用百货',49999,'/WEB-INF/upload/e/5/0/9/c/7/6/0/60746822-144c-4e98-8aaa-fca07e142a63_banana.jpg','国产大banana,好吃又好用'),('38a4a0f0-7c33-4e78-aa9e-1a3f7f193683','打印机021701',1000,'电子数码',10,'/WEB-INF/upload/8/6/b/0/5/9/e/f/1d25320d-e1b2-42bc-b890-981d58391cf0_6f84843a-1d1e-49c7-b4ce-c035d7790171.jpg','希望好用'),('3da04a08-a570-4945-91b5-cd0d63ace7b4','2017011002',10000,'电子数码',100,'/WEB-INF/upload/9/f/f/a/1/6/d/0/49617712-4018-4c0e-9e7a-5ebc4ff79ad1_6f84843a-1d1e-49c7-b4ce-c035d7790171.jpg','打印机'),('3f36ac54-5da0-4cd8-9991-2ee86cc348c2','金士顿8G内存条',800,'电子数码',258,'/WEB-INF/upload/2/2/b/7/f/2/f/4/06402c91-aa25-45d5-b0c3-3ac276a7cd05_244c59c6-bf0a-451b-81e6-18f8bb257e5f.jpg','3级内存条,拿货220,数量有限!'),('59622587-958e-43cb-b657-49619f60713e','陈子龙的等身抱枕',88,'日用百货',100,'/WEB-INF/upload/6/5/a/4/2/9/e/8/cb68faa0-0033-4517-bff0-5fb2f1f1019a_671434fae6cd7b89a26ce25e072442a7d8330efa.jpg','陈子龙的等身抱枕'),('6746c459-b284-4256-bbc6-1df60ba4a0a2','跳吧',200,'电子数码',1000000,'/WEB-INF/upload/2/6/4/a/a/5/2/3/ee6c796a-6333-4cd5-a06e-271d876aac8c_589577.jpg','我可不捞你'),('6c28bc1a-9c9b-4be3-b1cf-0068565e64e4','兔子',59,'家用电器',885,'/WEB-INF/upload/1/f/b/5/3/4/e/4/61a8cdff-52f7-4fce-bdb5-570426022082_preview.jpg','我们的征途是星辰大海!'),('70ee3179-3e76-4a3d-bd30-55d740f022dc','极品水母',998,'日用百货',555,'/WEB-INF/upload/e/6/f/d/3/f/6/1/d2370fcb-dc8f-4405-9bf2-76e798a91567_Jellyfish.jpg','居家旅行,必备之选'),('77feb539-a575-487b-8500-df38520f3239','战地1 豪华版 Origin平台正版激活码',780,'图书杂志',85,'/WEB-INF/upload/5/e/e/6/c/4/9/b/67c1a752-9020-4372-bcda-3375ef01c878_preview.jpg','怎么样,想想办法,干他一炮?'),('a0390f80-bed7-4a92-9954-5e22e64cbe17','沙发',100,'电子数码',1,'/WEB-INF/upload/5/0/6/f/4/4/b/f/40ca42aa-8298-430a-9fa9-88d6156d7b18_c987f2c1-4123-4d87-83bd-fe2fb221e272.jpg','好用'),('a08b13e9-c16a-4657-94ee-3b9bee2bd9c6','华为荣耀8',5000,'电子数码',2000,'/WEB-INF/upload/4/a/d/8/8/c/4/0/236ac480-db3a-4e6b-bc7f-c379a30c2c2c_301fb535-938a-4103-a2f5-f3f9af9ba9c6.jpg','挺好的'),('a239a700-f130-42c1-92a7-ea0a8cf31fa4','NiuPhone',2000,'电子数码',20,'/WEB-INF/upload/f/4/2/8/f/3/c/d/c6d26ea1-3dc6-4495-9572-43b3497b5fe4_738f47e2-9605-46aa-b647-fc8dca814074.jpg','杆杆的'),('a7184417-5aa2-4de0-8237-a4c0f53972a1','无为手机8plus',5000,'电子数码',10,'/WEB-INF/upload/3/6/c/0/7/2/1/3/741c8c70-cdd1-43a9-8cde-aa6a787129ca_738f47e2-9605-46aa-b647-fc8dca814074.jpg','还可以。。。。老司机推荐使用'),('a84fc7b3-944d-4b30-b67d-eba5cd2b326a','薯条',10,'日用百货',48,'/WEB-INF/upload/8/3/c/c/7/e/1/6/40c52ff0-d851-4087-bf03-95f7023f3d7f_0.jpg','吃吧,吃吧,我做一个不容易'),('b1f9c947-4f72-4245-b09d-8c5a8c311ae1','地中海7日游',5000,'日用百货',199,'/WEB-INF/upload/6/5/5/c/5/4/1/9/d437c381-59af-49ee-80c6-2b01e0b06105_1017530.jpg','放松身心,寻找艳遇'),('bf45940e-ac72-454f-b67f-83dd288d11f9','反恐精英',10000,'服装服饰',99,'/WEB-INF/upload/1/4/2/e/7/3/6/4/09af74da-3829-45c5-9517-380d2cc74f6a_preview.jpg','专业反恐部队'),('c0e7b4f3-e1ad-47d6-8c0d-f1c58b820ca8','大金表',1000,'电子数码',100,'/WEB-INF/upload/c/4/b/7/b/5/8/f/5adda796-66af-4c6e-a9e5-49a52a3c44a5_371cee6d-d81b-42b7-a11f-3ad36dc0e537.jpg','好用'),('c2952779-e9e0-4eda-8e0a-41a61f1afc66','苍#null',998,'床上用品',50,'/WEB-INF/upload/e/d/b/1/f/0/6/7/bcff4ee1-cc7f-4b30-a29c-017f76a21bf8_1.jpg','苍老师同款'),('c2978733-5af8-473b-adbc-05073126164b','宠物猫',30000,'日用百货',1,'/WEB-INF/upload/3/5/5/4/c/3/a/b/943de853-0e1b-4d51-9524-991607024d3b_IMG_0928.JPG','只有一只哦,快来抢购吧'),('c766ec19-4645-4e6b-9ddf-73a0f4aa5f6c','愤怒的大胡子',999,'日用百货',1,'/WEB-INF/upload/f/e/f/8/2/e/3/c/82c1698f-38a2-4340-9df7-83fadaefff4b_howardmouth.jpg','限量款,全球唯一'),('d73ab7ed-9f78-4775-a93b-4d355b2d5fc0','死亡海',4444,'日用百货',42,'/WEB-INF/upload/c/8/1/0/4/6/3/b/28139e28-7390-45a7-82c8-03e673486e60_Desert.jpg','体验死亡的感觉,你值得拥有'),('d7f7cce4-b268-41a7-9429-21fa69b64159','好好的',20000,'家用电器',1000,'/WEB-INF/upload/1/8/e/9/b/2/7/2/95e01470-8e6f-40dc-a76b-087d804bb0cf_bae0a60a-521d-48ef-bea6-0854b89d7be0.jpg','好用啊'),('d8cb845e-37f6-4515-9fc1-dea07719ee06','糖炒栗子',0,'日用百货',0,'/WEB-INF/upload/6/b/8/3/a/4/9/8/3fb77001-cd6c-4e4f-94a1-21a9c0563778_Hydrangeas.jpg','好吃的栗子'),('ff838641-feb5-42a1-b061-042b9113a95c','烈性炸药',5899,'电子数码',88,'/WEB-INF/upload/c/7/4/1/4/2/3/2/a99e691b-88d4-43a2-ac12-82ec54db123d_738f47e2-9605-46aa-b647-fc8dca814074.jpg','充电会自动引爆');
/*Table structure for table `user` */
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(20) default NULL,
`password` varchar(40) default NULL,
`nickname` varchar(60) default NULL,
`email` varchar(60) default NULL,
`role` varchar(20) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*Data for the table `user` */
insert into `user`(`id`,`username`,`password`,`nickname`,`email`,`role`) values (16,'tom1','456','tom','[email protected]','user'),(17,'tom2','202cb962ac59075b964b07152d234b70','tom2','[email protected]','user'),(22,'guanyu','202cb962ac59075b964b07152d234b70','guanyu','[email protected]','user'),(23,'boss','ceb8447cc4ab78d2ec34cd9f11e4bed2','boss','[email protected]','user'),(24,'朴乾','202cb962ac59075b964b07152d234b70','朴朴','[email protected]','user'),(25,'guanyu1','202cb962ac59075b964b07152d234b70','guanyui1','[email protected]','user'),(26,'tuhao1','698d51a19d8a121ce581499d7b701668','tuhao','[email protected]','user'),(27,'tuhao2','698d51a19d8a121ce581499d7b701668','tuhao2','[email protected]','user'),(28,'tuhao3','202cb962ac59075b964b07152d234b70','tuhao3','[email protected]','admin'),(33,'jinxf','202cb962ac59075b964b07152d234b70','靳幸福','[email protected]','admin'),(34,'jinxf1','202cb962ac59075b964b07152d234b70','jinxf','[email protected]','admin'),(35,'jinxf898','202cb962ac59075b964b07152d234b70','jinxf898','[email protected]','admin'),(36,'张飞','202cb962ac59075b964b07152d234b70','123','[email protected]','admin'),(37,'qwer','962012d09b8170d912f0669f6d7d9d07','qwer','[email protected]','admin'),(38,'aa ','202cb962ac59075b964b07152d234b70','ASGIUI','[email protected]','admin'),(39,'高嘉星','202cb962ac59075b964b07152d234b70','星星','[email protected]','admin'),(40,'张三','202cb962ac59075b964b07152d234b70','小三','[email protected]','admin'),(41,'123','202cb962ac59075b964b07152d234b70','123','[email protected]','admin'),(42,'宝哥','202cb962ac59075b964b07152d234b70','123','[email protected]','admin'),(43,'dfd','202cb962ac59075b964b07152d234b70','123','[email protected]','admin'),(44,'jlkj','202cb962ac59075b964b07152d234b70','123','[email protected]','admin'),(45,'zm','e10adc3949ba59abbe56e057f20f883e','aaaa','[email protected]','admin'),(46,'lilei','202cb962ac59075b964b07152d234b70','李雷','[email protected]','admin'),(47,'spd','1e59132c5c434e25e01a39e0e1bbe9f3','spd','[email protected]','admin'),(48,'tuhao','202cb962ac59075b964b07152d234b70','tuhao','[email protected]','admin'),(49,' diaosi','123','doapso','[email protected]','admin'),(50,'baifumei','123','baifumei','baifumei.qq.com','admin'),(51,' diaosi01','123','123','123','admin'),(52,'车彬彬','123','123','[email protected]','admin'),(53,'th','th','th','th','admin'),(54,'sg','sg','sg','sg','admin'),(55,'gaoshuo','123','shabi','[email protected]','admin');
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
mkdir /opt/easymall
把ROOT.WAR 文件上传到/opt/easymall目录
#启动容器
docker run -d --name easymall --restart=always \
-v /opt/easymall:usr/tomcat/webapps \
-p 80:8080 \
--net dockernet \
--ip 172.18.0.12 \
tomcat:7
http://192.168.64.150
1.准备文件夹jdk
2.把jdk压缩文件上传到jdk文件夹
3.在jdk文件夹添加到Dockerfile文件夹
cat <<EOF >Dockerfile
FROM centos:7
ADD jdk-8u212-linux-x64.tar.gz /opt/
ENV JAVA_HOME=/OPT/JDK1.8.0_212 \
PATH=$PATH:/opt/jdk1.8.0_212/bin
ENTRYPOINT bash
EOF
cd jdk
docker build -t jdk:8 .
docker images
1.创建文件夹eureka
cd ~/
mkdir eureka
3.创建Dockerfile
# 启动命令:
# java -jar /opt/sp05-eureka-0.0.1-SNAPSHOT.jar
# --spring.profiles.active=eureka1 --server.port=2001
cd eureka
cat <<EOF >Dockerfile
FROM jdk:8
COPY sp05-eureka-0.0.1-SNAPSHOT.jar /opt/
ENTRYPOINT ["java", "-jar", "/opt/sp05-eureka-0.0.1-SNAPSHOT.jar"]
CMD ["--spring.profiles.active=eureka1", "--server.port=2001"]
EOF
4.构建eureka镜像
cd eureka
docker build -t eureka:v1 .
docker images
运行启动eureka容器
docker run -d --name eureka1 --restart=always \
-p 2001:2001 \
eureka:v1
浏览器访问
http://192.168.64.150:2001
查看eureka的启动日志
docker logs eureka1
全文检索服务器
数据在数据库中保存一份,在es中也保存一份,主要用于全文检索服务
关闭防火墙
后面我们要使用多个端口,为了避免繁琐的开放端口操作,我们关掉防火墙
# 关闭防火墙
systemctl stop firewalld.service
# 禁用防火墙
systemctl disable firewalld.service
安装Docker
下载 Elastic Search 镜像
docker pull elasticsearch:7.9.3
清空容器
关闭虚拟机
重新分配内存
重新运行此虚拟机
上传文件
导入镜像:
docker load -i es-img.gz
#导入后检查
docker images
集群部署结构
在一台服务器上,使用Docker部署三个ES容器组成的集群
准备虚拟网络和挂载目录
# 创建虚拟网络
docker network create es-net
# node1 的挂载目录
mkdir -p -m 777 /var/lib/es/node1/plugins
mkdir -p -m 777 /var/lib/es/node1/data
# node2 的挂载目录
mkdir -p -m 777 /var/lib/es/node2/plugins
mkdir -p -m 777 /var/lib/es/node2/data
# node3 的挂载目录
mkdir -p -m 777 /var/lib/es/node3/plugins
mkdir -p -m 777 /var/lib/es/node3/data
设置 max_map_count
必须修改系统参数 max_map_count,否则 Elasticsearch 无法启动:
在 /etc/sysctl.conf 文件中添加 vm.max_map_count=262144
echo 'vm.max_map_count=262144' >>/etc/sysctl.conf
启动 Elasticsearch 集群
node1
docker run -d \
--name=node1 \
--restart=always \
--net es-net \
-p 9200:9200 \ #和客户端连接端口
-p 9300:9300 \ #集群服务器之间的端口
-v /var/lib/es/node1/plugins:/usr/share/elasticsearch/plugins \
-v /var/lib/es/node1/data:/usr/share/elasticsearch/data \
-e node.name=node1 \
-e node.master=true \
-e network.host=node1 \ #容器名,也可以写ip
-e discovery.seed_hosts=node1,node2,node3 \ #集群中所有主机列表
-e cluster.initial_master_nodes=node1 \ #候选主节点,所有节点都可能被选为主节点
-e cluster.name=es-cluster \ #集群名
-e "ES_JAVA_OPTS=-Xms256m -Xmx256m" \ #java虚拟机的内存大小
elasticsearch:7.9.3
docker run -d \
--name=node2 \
--restart=always \
--net es-net \
-p 9201:9200 \
-p 9301:9300 \
-v /var/lib/es/node2/plugins:/usr/share/elasticsearch/plugins \
-v /var/lib/es/node2/data:/usr/share/elasticsearch/data \
-e node.name=node2 \
-e node.master=true \
-e network.host=node2 \
-e discovery.seed_hosts=node1,node2,node3 \
-e cluster.initial_master_nodes=node1 \
-e cluster.name=es-cluster \
-e "ES_JAVA_OPTS=-Xms256m -Xmx256m" \
elasticsearch:7.9.3
node3:
docker run -d \
--name=node3 \
--restart=always \
--net es-net \
-p 9202:9200 \
-p 9302:9300 \
-v /var/lib/es/node3/plugins:/usr/share/elasticsearch/plugins \
-v /var/lib/es/node3/data:/usr/share/elasticsearch/data \
-e node.name=node3 \
-e node.master=true \
-e network.host=node3 \
-e discovery.seed_hosts=node1,node2,node3 \
-e cluster.initial_master_nodes=node1 \
-e cluster.name=es-cluster \
-e "ES_JAVA_OPTS=-Xms256m -Xmx256m" \
elasticsearch:7.9.3
查看启动结果结果
http://192.168.64.181:9200
http://192.168.64.181:9200/_cat/nodes
elasticsearch-head 项目提供了一个直观的界面,可以很方便地查看集群、分片、数据等等。elasticsearch-head最简单的安装方式是作为 chrome 浏览器插件进行安装。
第一种安装方式:
第二种方式:
1 在 elasticsearch-head 项目仓库中下载 chrome 浏览器插件
https://github.com/mobz/elasticsearch-head/raw/master/crx/es-head.crx
2 将文件后缀改为 zip
3 解压缩
4 在 chrome 浏览器中选择“更多工具”–“扩展程序”
5 在“扩展程序”中确认开启了“开发者模式”
6 点击“加载已解压的扩展程序”
7 选择前面解压的插件目录
8 在浏览器中点击 elasticsearch-head 插件打开 head 界面,并连接 http://192.168.64.181:9200/
成功连接界面:
从 ik 分词器项目仓库中下载 ik 分词器安装包,下载的版本需要与 Elasticsearch 版本匹配:
https://github.com/medcl/elasticsearch-analysis-ik
或者可以访问 gitee 镜像仓库:
https://gitee.com/mirrors/elasticsearch-analysis-ik
下载 elasticsearch-analysis-ik-7.9.3.zip 复制到 /root/ 目录下
在三个节点上安装 ik 分词器
cd ~/
# 从宿主机复制 ik 分词器到三个 es 容器
docker cp elasticsearch-analysis-ik-7.9.3.zip node1:/root/
docker cp elasticsearch-analysis-ik-7.9.3.zip node2:/root/
docker cp elasticsearch-analysis-ik-7.9.3.zip node3:/root/
# 在 node1 中安装 ik 分词器
docker exec -it node1 elasticsearch-plugin install file:///root/elasticsearch-analysis-ik-7.9.3.zip
# 在 node2 中安装 ik 分词器
docker exec -it node2 elasticsearch-plugin install file:///root/elasticsearch-analysis-ik-7.9.3.zip
# 在 node2 中安装 ik 分词器
docker exec -it node3 elasticsearch-plugin install file:///root/elasticsearch-analysis-ik-7.9.3.zip
# 重启三个 es 容器
docker restart node1
docker restart node2
docker restart node3
查看安装结果
在浏览器中访问 http://192.168.64.150:9200/_cat/plugins
ik分词器提供两种分词器: ik_max_word 和 ik_smart
ik_max_word: 会将文本做最细粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,中华人民,中华,华人,人民共和国,人民,人,民,共和国,共和,和,国国,国歌”,会穷尽各种可能的组合,适合 Term Query;
ik_smart: 会做最粗粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,国歌”,适合 Phrase 查询。
使用 head 执行下面测试:
向 http://192.168.64.181:9200/_analyze 路径提交 POST 请求,并在协议体中提交 Json 数据:
{
"analyzer":"ik_max_word",
"text":"中华人民共和国国歌"
}
使用 head 执行下面测试:
向 http://192.168.64.181:9200/_analyze 路径提交 POST 请求,并在协议体中提交 Json 数据:
{
"analyzer":"ik_smart",
"text":"中华人民共和国国歌"
}
docker pull kibana:7.9.3
docker run \
-d \
--name kibana \
--net es-net \
-p 5601:5601 \
-e ELASTICSEARCH_HOSTS='["http://node1:9200","http://node2:9200","http://node3:9200"]' \
--restart=always \
kibana:7.9.3
启动后,浏览器访问 Kibana,进入 Dev Tools:
http://192.168.64.181:5601/
索引
Elasticsearch索引用来存储我们要搜索的数据,以倒排索引结构进行存储。
例如,要搜索商品数据,可以创建一个商品数据的索引,其中存储着所有商品的数据,供我们进行搜索:
当索引中存储了大量数据时,大量的磁盘io操作会降低整体搜索新能,这时需要对数据进行分片存储。
索引分片
在一个索引中存储大量数据会造成性能下降,这时可以对数据进行分片存储。
每个节点上都创建一个索引分片,把数据分散存放到多个节点的索引分片上,减少每个分片的数据量来提高io性能:
每个分片都是一个独立的索引,数据分散存放在多个分片中,也就是说,每个分片中存储的都是不同的数据。搜索时会同时搜索多个分片,并将搜索结果进行汇总。
如果一个节点宕机分片不可用,则会造成部分数据无法搜索:
为了解决这一问题,可以对分片创建多个副本来解决。
索引副本
对分片创建多个副本,那么即使一个节点宕机,其他节点中的副本分片还可以继续工作,不会造成数据不可用:
分片的工作机制:
主分片的数据会复制到副本分片
搜索时,以负载均衡的方式工作,提高处理能力
主分片宕机时,其中一个副本分片会自动提升为主分片
下面我们就以上图的结构来创建 products 索引
创建一个名为 products 的索引,用来存储商品数据。
分片和副本参数说明:
------number_of_shards:分片数量,默认值是 5
------number_of_replicas:副本数量,默认值是 1
我们有三个节点,在每个节点上都创建一个分片。每个分片在另两个节点上各创建一个副本。
# 创建索引,命名为 products
PUT /products
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 2
}
}
用索引名称过滤,查看 products 索引:
粗框为主分片,细框为副本分片
类似于数据库表结构,索引数据也被分为多个数据字段,并且需要设置数据类型和其他属性。
映射,是对索引中字段结构的定义和描述。
字段的数据类型
常用类型:
数字类型:
-----byte、short、integer、long
-----float、double
-----unsigned_long
字符串类型:
-------text : 会进行分词
--------keyword : 不会进行分词,适用于email、主机地址、邮编等
日期和时间类型:
-------date
在 products 索引中创建映射。
分词器设置:
------analyzer:在索引中添加文档时,text类型通过指定的分词器分词后,再插入倒排索引
------search_analyzer:使用关键词检索时,使用指定的分词器对关键词进行分词
查询时,关键词优先使用 search_analyzer 设置的分词器,如果 search_analyzer 不存在则使用 analyzer 分词器。
# 定义mapping,数据结构
PUT /products/_mapping
{
"properties": {
"id": {
"type": "long"
},
"title": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"category": {
"type": "text",
"analyzer": "ik_smart",
"search_analyzer": "ik_smart"
},
"price": {
"type": "float"
},
"city": {
"type": "text",
"analyzer": "ik_smart",
"search_analyzer": "ik_smart"
},
"barcode": {
"type": "keyword"
}
}
}
查看映射
GET /products/_mapping
添加的文档会有一个名为_id的文档id,这个文档id可以自动生成,也可以手动指定,通常可以使用数据的id作为文档id。
# 添加文档
PUT /products/_doc/10033
{
"id":"10033",
"title":"SONOS PLAY:5(gen2) 新一代PLAY:5无线智能音响系统 WiFi音箱家庭,潮酷数码会场",
"category":"潮酷数码会场",
"price":"3980.01",
"city":"上海",
"barcode":"527848718459"
}
PUT /products/_doc/10034
{
"id":"10034",
"title":"天猫魔盒 M13网络电视机顶盒 高清电视盒子wifi 64位硬盘播放器",
"category":"潮酷数码会场",
"price":"398.00",
"city":"浙江杭州",
"barcode":"522994634119"
}
PUT /products/_doc/10035
{
"id":"10035",
"title":"BOSE SoundSport耳塞式运动耳机 重低音入耳式防脱降噪音乐耳机",
"category":"潮酷数码会场",
"price":"860.00",
"city":"浙江杭州",
"barcode":"526558749068"
}
PUT /products/_doc/10036
{
"id":"10036",
"title":"【送支架】Beats studio Wireless 2.0无线蓝牙录音师头戴式耳机",
"category":"潮酷数码会场",
"price":"2889.00",
"city":"上海",
"barcode":"37147009748"
}
PUT /products/_doc/10037
{
"id":"10037",
"title":"SONOS PLAY:1无线智能音响系统 美国原创WiFi连接 家庭桌面音箱",
"category":"潮酷数码会场",
"price":"1580.01",
"city":"上海",
"barcode":"527783392239"
}
也可以自动生成 _id 值:
POST /products/_doc
{
"id":"10027",
"title":"vivo X9前置双摄全网通4G美颜自拍超薄智能手机大屏vivox9",
"category":"手机会场",
"price":"2798.00",
"city":"广东东莞",
"barcode":"541396973568"
}
查看文档:
GET /products/_doc/10037
底层索引数据无法修改,修改数据实际上是先删除再重新添加。
两种修改方式:
----PUT:对文档进行完整的替换
----POST:可以修改一部分字段
修改价格字段的值:
# 修改文档 - 替换
PUT /products/_doc/10037
{
"id":"10037",
"title":"SONOS PLAY:1无线智能音响系统 美国原创WiFi连接 家庭桌面音箱",
"category":"潮酷数码会场",
"price":"9999.99",
"city":"上海",
"barcode":"527783392239"
}
查看文档:
GET /products/_doc/10037
修改价格和城市字段的值:
# 修改文档 - 更新部分字段
POST /products/_update/10037
{
"doc": {
"price":"8888.88",
"city":"深圳"
}
}
查看文档
GET /products/_doc/10037
DELETE /products/_doc/10037
POST /products/_delete_by_query
{
"query": {
"match_all": {
}
}
}
# 删除 products 索引
DELETE /products
导入测试数据
为了测试搜索功能,我们首先导入测试数据,3160条商品数据,数据样例如下:
{
"index": {
"_index": "pditems", "_id": "536563"}}
{
"id":"536563","brand":"联想","title":"联想(Lenovo)小新Air13 Pro 13.3英寸14.8mm超轻薄笔记本电脑","sell_point":"清仓!仅北京,武汉仓有货!","price":"6688.0","barcode":"","image":"/images/server/images/portal/air13/little4.jpg","cid":"163","status":"1","created":"2015-03-08 21:33:18","updated":"2015-04-11 20:38:38"}
下载测试数据
https://download.csdn.net/download/weixin_38305440/13129113
将压缩文件中的 pditems.json 上传到服务器
PUT /pditems
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 2
},
"mappings": {
"properties": {
"id": {
"type": "long"
},
"brand": {
"type": "text",
"analyzer": "ik_smart"
},
"title": {
"type": "text",
"analyzer": "ik_max_word"
},
"sell_point": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"price": {
"type": "float"
},
"image": {
"type": "keyword"
},
"cid": {
"type": "long"
},
"status": {
"type": "byte"
},
"created": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
"updated": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
}
}
}
}
在服务器上,进入 pditems.json 所在的文件夹,执行批量数据导入:
curl -XPOST 'localhost:9200/pditems/_bulk' \
-H 'Content-Type:application/json' \
--data-binary @pditems.json
搜索 pditems 索引中全部 3160 条数据:
GET /pditems/_search
{
"query": {
"match_all": {
}
},
"size": 3160
}
搜索所有数据
# 搜索 pditems 索引中全部数据
POST /pditems/_search
{
"query": {
"match_all": {
}
}
}
关键词搜索
# 查询 pditems 索引中title中包含"电脑"的商品
POST /pditems/_search
{
"query": {
"match": {
"title": "电脑"
}
}
}
搜索结果过滤器
# 价格大于2000,并且title中包含"电脑"的商品
POST /pditems/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "电脑"
}
}
],
"filter": [
{
"range": {
"price": {
"gte": "2000"
}
}
}
]
}
}
}
搜索结果高亮显示
POST /pditems/_search
{
"query": {
"multi_match":{
"query": "手机",
"fields": ["title", "sell_point"]
}
},
"highlight" : {
"pre_tags" : [""],
"post_tags" : [""],
"fields" : {
"title" : {
},
"sell_point" : {
"pre_tags": "",
"post_tags": ""
}
}
}
}
Spring Data Elasticsearch
https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#reference
Spring Data Elasticsearch 是 Elasticsearch 搜索引擎开发的解决方案。它提供:
模板对象,用于存储、搜索、排序文档和构建聚合的高级API。
例如,Repository 使开发者能够通过定义具有自定义方法名称的接口来表达查询。
在 Elasticsearch 中存储学生数据,并对学生数据进行搜索测试。
1 创建 students 索引和映射
2 C - 创建学生数据
3 R - 访问学生数据
4 U - 修改学生数据
5 D - 删除学生数据
6 使用 Repository 和 Criteria 搜索学生数据
新建 springboot module,添加 spring data elasticsearch 依赖
项目的 pom.xml 文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.tedu</groupId>
<artifactId>es-springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>es-springboot</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml 配置
logging.level.tracer=TRACE 作用是在控制台中显示底层的查询日志
spring:
elasticsearch:
rest:
uris: http://192.168.64.181:9200
logging:
level:
tracer: TRACE
Student 实体类
package cn.tedu.esspringboot.es;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.DateFormat;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
@Document(indexName = "students",shards = 3,replicas = 2)
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
@Id
private Long id;
@Field(analyzer = "ngram",type = FieldType.Text)
private String name;
@Field(type = FieldType.Keyword)
private Character gender;
@Field(type= FieldType.Date,format = DateFormat.custom,pattern = "yyyy-M-d")
private String birthDate;
}
@Document 注解
@Documnet注解对索引的参数进行设置。
上面代码中,把 students 索引的分片数设置为3,副本数设置为2。
@Id 注解
在 Elasticsearch 中创建文档时,使用 @Id 注解的字段作为文档的 _id 值
@Field 注解
通过 @Field 注解设置字段的数据类型和其他属性。
文本类型 text 和 keyword
text 类型会进行分词。
keyword 不会分词。
analyzer 指定分词器
通过 analyzer 设置可以指定分词器,例如 ik_smart、ik_max_word 等。
我们这个例子中,对学生姓名字段使用的分词器是 ngram 分词器,其分词效果如下面例子所示:
通过 ElasticsearchRepository 实现 CRUD 操作
Spring Data 的 Repository 接口提供了一种声明式的数据操作规范,无序编写任何代码,只需遵循 Spring Data 的方法定义规范即可完成数据的 CRUD 操作。
ElasticsearchRepository 继承自 Repository,其中已经预定义了基本的 CURD 方法,我们可以通过继承 ElasticsearchRepository,添加自定义的数据操作方法。
Repository 方法命名规范
自定义数据操作方法需要遵循 Repository 规范,示例如下:
StudentRepository
package cn.tedu.esspringboot.es;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;
public interface StudentRepository extends ElasticsearchRepository<Student, Long> {
List<Student> findByName(String name);
List<Student> findByNameOrBirthDate(String name, String birthDate);
}
业务类 StudentService
package cn.tedu.esspringboot.es;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class StudentService {
@Autowired
private StudentRepository studentRepo;
public void save(Student student) {
studentRepo.save(student);
}
public void delete(Long id) {
studentRepo.deleteById(id);
}
public void update(Student student) {
save(student);
}
public List<Student> findByName(String name) {
return studentRepo.findByName(name);
}
public List<Student> findByNameOrBirthDate(String name, String birthDate) {
return studentRepo.findByNameOrBirthDate(name, birthDate);
}
}
在 Elasticsearch 中创建 students 索引
在开始运行测试之前,在 Elasticsearch 中先创建 students 索引:
PUT /students
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 2,
"index.max_ngram_diff":30,
"analysis": {
"analyzer": {
"ngram_analyzer": {
"tokenizer": "ngram_tokenizer"
}
},
"tokenizer": {
"ngram_tokenizer": {
"type": "ngram",
"min_gram": 1,
"max_gram": 30,
"token_chars": [
"letter",
"digit"
]
}
}
}
},
"mappings": {
"properties": {
"id": {
"type": "long"
},
"name": {
"type": "text",
"analyzer": "ngram_analyzer"
},
"gender": {
"type": "keyword"
},
"birthDate": {
"type": "date",
"format": "yyyy-MM-dd"
}
}
}
}
测试学生数据的 CRUD 操作
添加测试类,对学生数据进行 CRUD 测试
package cn.tedu.esspringboot;
import cn.tedu.esspringboot.es.Student;
import cn.tedu.esspringboot.es.StudentService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
public class Test1 {
@Autowired
private StudentService studentService;
@Test
public void test1() {
studentService.save(new Student(998L,"张三",'男',"2020-12-04"));
}
@Test
public void test2() {
studentService.update(new Student(1L,"李四",'女',"2020-12-04"));
}
@Test
public void test3() {
List<Student> stu = studentService.findByName("四");
System.out.println(stu);
}
@Test
public void test4() throws Exception {
List<Student> stu;
stu = studentService.findByNameOrBirthDate("四", "1999-09-09");
System.out.println(stu);
stu = studentService.findByNameOrBirthDate("SFSDFS", "2020-12-04");
System.out.println(stu);
}
}
Spring Data Elasticsearch 中,可以使用 SearchOperations 工具执行一些更复杂的查询,这些查询操作接收一个 Query 对象封装的查询操作。
Spring Data Elasticsearch 中的 Query 有三种:
----CriteriaQuery
----StringQuery
----NativeSearchQuery
多数情况下,CriteriaQuery 都可以满足我们的查询求。下面来看两个 Criteria 查询示例:
StudentSearcher
package cn.tedu.esspringboot.es;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.Criteria;
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.stream.Collectors;
@Component
public class StudentSearcher {
@Autowired
private ElasticsearchOperations searchOperations;
public List<Student> searchByBirthDate(String birthDate) {
Criteria c = new Criteria("birthDate").is(birthDate);
return criteriaSearch(c);
}
public List<Student> searchByBirthDate(String ge, String le) {
Criteria c = new Criteria("birthDate").between(ge, le);
return criteriaSearch(c);
}
private List<Student> criteriaSearch(Criteria c) {
CriteriaQuery q = new CriteriaQuery(c);
SearchHits<Student> hits = searchOperations.search(q, Student.class);
List<Student> list = hits.stream().map(SearchHit::getContent).collect(Collectors.toList());
return list;
}
}
修改 StudentService
在 StudentService 中,调用 StudentSearcher,执行查询:
package cn.tedu.esspringboot.es;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class StudentService {
@Autowired
private StudentRepository studentRepo;
@Autowired
private StudentSearcher studentSearcher;
public void save(Student student) {
studentRepo.save(student);
}
public void delete(Long id) {
studentRepo.deleteById(id);
}
public void update(Student student) {
save(student);
}
public List<Student> findByName(String name) {
return studentRepo.findByName(name);
}
public List<Student> findByNameOrBirthDate(String name, String birthDate) {
return studentRepo.findByNameOrBirthDate(name, birthDate);
}
public List<Student> findByBirthDate(String birthDate) {
return studentSearcher.searchByBirthDate(birthDate);
}
public List<Student> findByBirthDate(String ge, String le) {
return studentSearcher.searchByBirthDate(ge, le);
}
}
在测试类中添加测试方法
package cn.tedu.esspringboot;
import cn.tedu.esspringboot.es.Student;
import cn.tedu.esspringboot.es.StudentService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
public class Test1 {
@Autowired
private StudentService studentService;
@Test
public void test1() {
studentService.save(new Student(998L,"张三",'男',"2020-12-04"));
}
@Test
public void test2() {
studentService.update(new Student(1L,"李四",'女',"2020-12-04"));
}
@Test
public void test3() {
List<Student> stu = studentService.findByName("四");
System.out.println(stu);
}
@Test
public void test4() throws Exception {
List<Student> stu;
stu = studentService.findByNameOrBirthDate("四", "1999-09-09");
System.out.println(stu);
stu = studentService.findByNameOrBirthDate("SFSDFS", "2020-12-04");
System.out.println(stu);
}
@Test
public void test5() throws Exception {
List<Student> stu;
stu = studentService.findByBirthDate("2020-12-04");
System.out.println(stu);
}
@Test
public void test6() throws Exception {
List<Student> stu;
stu = studentService.findByBirthDate("2020-12-05", "2020-12-09");
System.out.println(stu);
}
}