Python Flask高级编程之从0到1开发《鱼书》精品项目
作者:互联网
download:Python Flask高级编程之从0到1开发《鱼书》精品项目
通过一个项目的实践,深入浅出讲解Flask核心原理、剖析Flask源码,在解读Flask的同时,学习Python高级编程、培养面向对象思维,是市面上稀缺的优质课程。
适合人群及技术储备要求
如果你想更深入的学习Python,掌握优秀的框架设计思想,那本课程非常适合你
技术储备要求
具备Python语法基础、 Web后端开发基础
解壓下载的zip文件
windows
直接緊缩软件解壓即可
linux
unzip rocketmq-all-4.8.0-source-release.zip
cd rocketmq-all-4.8.0/
mvn -Prelease-all -DskipTests clean install -U
cd distribution/target/rocketmq-4.8.0/rocketmq-4.8.0
設置環境變量
這里windows需求設置,Linux並不需求,當然我也沒有在Linux環境下測試,有興味的小同伴本人去實驗。在windows添加如下環境變量:
ROCKETMQ_HOME="D:\workspace\tools\rocketmq-all-4.8.0-bin-release"
NAMESRV_ADDR="localhost:9876"
![]()
![image-20210310230651594]()
或者在啟動前的powershell窗口里設置,這里是暫時,每次都要設置,嫌费事的直接設置永世的:
$Env:ROCKETMQ_HOME="D:\workspace\tools\rocketmq-all-4.8.0-bin-release"
$Env:NAMESRV_ADDR="localhost:9876"
啟動
啟動Name效勞
linux
nohup sh bin/mqnamesrv &
tail -f ~/logs/rocketmqlogs/namesrv.log
The Name Server boot success...
windows
翻開powershell,假如沒有設置環境環境變量需求先執行下面的操作,停止環境變量設置
$Env:ROCKETMQ_HOME="D:\workspace\tools\rocketmq-all-4.8.0-bin-release"
$Env:NAMESRV_ADDR="localhost:9876"
然後進入rocketMQ裝置目錄,執行如下操作
cd D:\workspace\tools\rocketmq-all-4.8.0-bin-release
.\bin\mqnamesrv.cmd
![]()
![]()
啟動代理效勞
Linux
nohup sh bin/mqbroker -n localhost:9876 &
tail -f ~/logs/rocketmqlogs/broker.log
The broker[%s, 172.30.30.233:10911] boot success...
windows
和上面啟動Name效勞一樣,沒設置環境需求先執行前面兩行環境變量設置的操作
$Env:ROCKETMQ_HOME="D:\workspace\tools\rocketmq-all-4.8.0-bin-release"
$Env:NAMESRV_ADDR="localhost:9876"
然後執行啟動操作
cd D:\workspace\tools\rocketmq-all-4.8.0-bin-release
.\bin\mqbroker.cmd -n localhost:9876 autoCreateTopicEnable=true
操作之前,一定要進入rocketMQ裝置目錄,否則報答如下红色錯誤
![]()
接纳&發送音訊
發送音訊
Linux
export NAMESRV_ADDR=localhost:9876
sh bin/tools.sh org.apache.rocketmq.example.quickstart.Producer
SendResult [sendStatus=SEND_OK, msgId= ...
windows
同樣的,沒設置環境變量的記得先設置,嫌费事就直接設置永世的環境變量,參照設置環境變量
cd D:\workspace\tools\rocketmq-all-4.8.0-bin-release
.\bin\tools.cmd org.apache.rocketmq.example.quickstart.Producer
執行命令後,會看到我們向音訊隊列中發送了很多音訊
![]()
接纳音訊
Linux
sh bin/tools.sh org.apache.rocketmq.example.quickstart.Consumer
ConsumeMessageThread_%d Receive New Messages: [MessageExt...
windows
同樣的,沒設置環境變量的記得先設置
cd D:\workspace\tools\rocketmq-all-4.8.0-bin-release
.\bin\tools.cmd org.apache.rocketmq.example.quickstart.Consumer
執行上面命令後,能夠看到控製台接纳到方才發送的音訊
![]()
Java简單demo
這里的demo在官網都能夠看到,也都很简單,需求補充阐明的,我會停下來解释。開端項目之前,先引入如下依賴:
org.apache.rocketmq
rocketmq-client
4.3.0
發送同步音訊
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
/**
- @program: rocketmq-demo
- @description: 發送同步音訊
- @author: syske
- @create: 2021-03-09 20:24
/
public class SyncProducer {
public static void main(String[] args) throws Exception {
// 實例化音訊消费者Producer
DefaultMQProducer producer = new DefaultMQProducer("please_rename_unique_group_name");
// 設置NameServer的地址
producer.setNamesrvAddr("localhost:9876");
// 啟動Producer實例
producer.start();
for (int i = 0; i < 100; i++) {
// 創立音訊,並指定Topic,Tag和音訊體
Message msg = new Message("TopicTest" / Topic /,
"TagA" / Tag /,
("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET) / Message body */
);
// 發送音訊到一個Broker
SendResult sendResult = producer.send(msg);
// 經過sendResult返回音訊能否勝利送達
System.out.printf("%s%n", sendResult);
}
// 假如不再發送音訊,關閉Producer實例。
producer.shutdown();
}
}
發送異步音訊
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.CountDownLatch2;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import java.util.concurrent.TimeUnit;
/** - @program: rocketmq-demo
- @description: 異步音訊消费者
- @author: syske
- @create: 2021-03-09 20:28
*/
public class AsyncProducer {
public static void main(String[] args) throws Exception {
// 實例化音訊消费者Producer
DefaultMQProducer producer = new DefaultMQProducer("please_rename_unique_group_name");
// 設置NameServer的地址
producer.setNamesrvAddr("localhost:9876");
// 啟動Producer實例
producer.start();
producer.setRetryTimesWhenSendAsyncFailed(0);
int messageCount = 100;
// 依據音訊數量實例化倒計時計算器
final CountDownLatch2 countDownLatch = new CountDownLatch2(messageCount);
for (int i = 0; i < messageCount; i++) {
final int index = i;
// 創立音訊,並指定Topic,Tag和音訊體
Message msg = new Message("TopicTest",
"TagA",
"OrderID188",
"Hello world".getBytes(RemotingHelper.DEFAULT_CHARSET));
// SendCallback接纳異步返回結果的回調
producer.send(msg, new SendCallback() {br/>@Override
public void onSuccess(SendResult sendResult) {
System.out.printf("%-10d OK %s %n", index,br/>sendResult.getMsgId());
}
@Override
public void onException(Throwable e) {
System.out.printf("%-10d Exception %s %n", index, e);
e.printStackTrace();
}
});
}
// 等候5s
countDownLatch.await(5, TimeUnit.SECONDS);
// 假如不再發送音訊,關閉Producer實例。
producer.shutdown();
}
}
發送單向音訊
單向音訊就是沒有返回值的音訊
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
/**
- @program: rocketmq-demo
- @description: 單向音訊消费者
- @author: syske
- @create: 2021-03-09 20:30
/
public class OnewayProducer {
public static void main(String[] args) throws Exception{
// 實例化音訊消费者Producer
DefaultMQProducer producer = new DefaultMQProducer("please_rename_unique_group_name");
// 設置NameServer的地址
producer.setNamesrvAddr("localhost:9876");
// 啟動Producer實例
producer.start();
for (int i = 0; i < 100; i++) {
// 創立音訊,並指定Topic,Tag和音訊體
Message msg = new Message("TopicTest" / Topic /,
"TagA" / Tag /,
("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET) / Message body */
);
// 發送單向音訊,沒有任何返回結果
producer.sendOneway(msg);
}
// 假如不再發送音訊,關閉Producer實例。
producer.shutdown();
}
}
音訊消费者
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;
import java.util.List;
/** - @program: rocketmq-demo
- @description: 音訊消费者
- @author: syske
- @create: 2021-03-09 20:26
/
public class Consumer {
public static void main(String[] args) throws InterruptedException, MQClientException {
// 實例化消费者
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name");
// 設置NameServer的地址
consumer.setNamesrvAddr("localhost:9876");
// 订阅一個或者多個Topic,以及Tag來過濾需求消费的音訊
consumer.subscribe("TopicTest", "");
// 注册回調完成類來處置從broker拉取回來的音訊
consumer.registerMessageListener(new MessageListenerConcurrently() {br/>@Override
public ConsumeConcurrentlyStatus consumeMessage(List msgs, ConsumeConcurrentlyContext context) {
System.out.printf("%s Receive New Messages: %s %n", Thread.currentThread().getName(), msgs);
// 標誌該音訊曾經被勝利消费
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
});
// 啟動消费者實例
consumer.start();
System.out.printf("Consumer Started.%n");
}
}
發送次第音訊
次第音訊简單來說,就是消费者的消费次第和消费者消费次第是分歧的,比方對下面代码中的創立订單,消费的時分肯定是先消费創立1,然後是創立2,再是創立3,這里的是辨別能否是同一類音訊,是經過Message的tag屬性的值來判別的。關於次第音訊藉用網上的一個圖來阐明吧:
![]()
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/*
标签:producer,音訊,Python,鱼书,Flask,org,apache,import,rocketmq 来源: https://blog.51cto.com/15101022/2660742