Redis
虚拟机的常用命令
- hostnamectl set-hostname username //修改名字
- ls 查看文件
- vim filename(文件名)进入编辑
- :w 保存 :q未编辑的退出 :q! 强退,回滚 i编辑状态 esc退出编辑状态
- rm - rf 删除 不会有提示
- cd /filename/filename 进入指定文件
- cd… 上一级目录
Redis的安装
下载redis安装包
1 wget http://download.redis.io/releases/redis-3.0.4.tar.gz //连接为安装包
2 tar -xzvf redis-3.0.4.tar.gz //进行解压
3 cd redis-3.0.4 //进入redis
4 #执行make命令编译
5 make
6 make install 安装完成后,会在/usr/local/bin目录下生成下面几个可执行文件,它们的作用分别是:
7 redis-server 启动redis
8 redis-cli 客户操作页面 ,api文档中友很多命令
Redis的一些简单配置修改
1 vim redis.conf 进入修改页面
2 daemonize yes //Redis使用后台模式
3 bind 0.0.0.0 允许别人连接
4 port 6379 端口一般用6379
5 protected-mode no 保护模式关闭
RabbitMQ
没有中间件的消息队列的构建
接受方
ConnectionFactory factory = new ConnectionFactory(); //建立连接工厂
factory.setVirtualHost("/myhost");
factory.setHost(“192.168.184.13”);
factory.setUsername(“admin”);
factory.setPassword(“123456”);
factory.setPort(5672);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
// 幂等性创建队列… 是否持久 是否排他 是否自动删除
channel.queueDeclare(“myQueue”, true, false, false, null);
//设置负载均衡
channel.basicQos(1);
DefaultConsumer consumer = new DefaultConsumer(channel) {
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String message = new String(body, “utf-8”);
System.out.println(“收到消息队列中的消息内容为:” + message);
// 手动确认, true代表确认getDeliveryTag以前所有消息
channel.basicAck(envelope.getDeliveryTag(), false);
}
};
channel.basicConsume(“myQueue”, false, consumer);//回调
发送方
public class Publisher {
public static void main(String[] args) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setVirtualHost("/myhost");
factory.setHost(“192.168.184.13”);
factory.setUsername(“admin”);
factory.setPassword(“123456”);
factory.setPort(5672);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
// 幂等性创建队列... 是否持久 是否排他 是否自动删除
channel.queueDeclare("myQueue", true, false, false, null);
String message1 = "a";
channel.basicPublish("",“myQueue”,null,message1.getBytes());
channel.close();
connection.close();
}
}
有中间件的消息队列
将Connection连接封装
public class ConnectMQ {
protected ConnectionFactory factory = new ConnectionFactory();
protected Connection connection = null;
protected Channel channel = null;
@Before
public void init() {
factory.setVirtualHost("/myhost");
factory.setHost("192.168.184.13");
factory.setUsername("admin");
factory.setPassword("123456");
factory.setPort(5672);
try {
connection = factory.newConnection();
channel = connection.createChannel();
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
public Consumer myConsumer(){
DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
String message = new String(body, "utf-8");
System.out.println("收到消息:"+message);
channel.basicAck(envelope.getDeliveryTag(), false);
}
};
return defaultConsumer;
}
}
发送方
public class Publisher extends ConnectMQ {
@Test
public void send() throws Exception{
//幂等性交换机
channel.exchangeDeclare(“myTopic”,“topic”);
//发送消息
String message = “这是Direct的测试消息2”;
// 交换机名 口令
channel.basicPublish(“myTopic”,“com.seecen.aa”,null,message.getBytes());
channel.close();
connection.close();
}
}
接受方
public class Subscriber extends ConnectMQ {
@Test
public void receive() throws Exception{
channel.exchangeDeclare(“myTopic”,“topic”);
//创建一个临时队列(名字随机生成)
//返回值是队列的名字
String queue = channel.queueDeclare().getQueue();
String key = “com.seecen.*”;
//把该队列绑定到交换机上,并且设置口令
channel.queueBind(queue,“myTopic”,key);
channel.basicConsume(queue,myConsumer());
//让程序不结束
System.in.read();
}
}
中间件(交换机)的类型有4种
Direct exchange(直连交换机)
Fanout exchange(扇型交换机)
Topic exchange(主题交换机)
Headers exchange(头交换机)
Topic交换机 业务需求都能满足