docker使用基础
安装:mac intel chip:https://docs.docker.com/desktop/mac/install/
命令:
docker run -dp 3000:3000 getting-started
ps start<container_id> stop<container_id> images rm <container_id>
概念:image container docker run -d -p 80:80 docker/getting-started
打包
docker build -t getting-started . (先编写Dockerfile 文件)
docker tag getting-started thinkqzl/getting-started
拉包 docker pull redis docker run -d redis:会自动下载包
docker serach docker/getting-started。 docker search thinkqzl/getting-started
docker login logout
自己的工程: thinkqzl/helloworld
有意思的是 docker基于曹组系统,也有操作系统的 images,并且只有 几十兆
docker pull ubuntu docker run -it -p 18888:18888 ubuntu /bin/bash 可以进入Ubuntu命令行操作。
实战
docker搭建rocketmq集群:部分参考: https://zhuanlan.zhihu.com/p/166204662
下载namesrv ,broker,监控台的 包
docker pull styletang/rocketmq-console-ng:latest docker pull rocketmqinc/rocketmq-namesrv:4.5.0-alpine docker pull rocketmqinc/rocketmq-broker:4.5.0-alpine
可以新建一个rocketmq目录,把docker里的log等目录映射出来,如果不需要日志等文件映射,也可以不建文件目录
安装
docker run -name="namesrv-1" -p 6538:9876 -e "JAVA_OPT_EXT=-server -XMS128m -Xms128m -Xmn128m" -d rocketmqinc/rocketmq-namesrv:4.5.0-alpine
docker run -name="namesrv-2" -p 6539:9876 -e "JAVA_OPT_EXT=-server -XMS128m -Xms128m -Xmn128m" -d rocketmqinc/rocketmq-namesrv:4.5.0-alpine
docker run -it --name="broker-m1" -p 10919:10919 -p 10922:10922 -p 10921:10921 -e "JAVA_OPT_EXT=-server -XMS128m -Xms128m -Xmn128m" -e "BROKE_IP=192.168.57.211" -e "BROKER_NAME=qin1" -e "BROKER_CLUSTER_NAME=qinc" -e "NAMESRV_ADDR=192.168.57.211:6358;192.168.57.211:6359" -e "BROKER_ID=0" -d rocketmqinc/rocketmq-broker:4.5.0-alpine
docker run -it --name="broker-s1" -p 10929:10929 -p 10932:10932 -p 10931:10931 -e "JAVA_OPT_EXT=-server -XMS128m -Xms128m -Xmn128m" -e "BROKE_IP=192.168.57.211" -e "BROKER_NAME=qin1" -e "BROKER_CLUSTER_NAME=qinc" -e "NAMESRV_ADDR=192.168.57.211:6358;192.168.57.211:6359" -e "BROKER_ID=1" -d rocketmqinc/rocketmq-broker:4.5.0-alpine
docker run -it --name="broker-m2" -p 10939:10939 -p 10942:10942 -p 10941:10941 -e "JAVA_OPT_EXT=-server -XMS128m -Xms128m -Xmn128m" -e "BROKE_IP=192.168.57.211" -e "BROKER_NAME=qin2" -e "BROKER_CLUSTER_NAME=qinc" -e "NAMESRV_ADDR=192.168.57.211:6358;192.168.57.211:6359" -e "BROKER_ID=0" -d rocketmqinc/rocketmq-broker:4.5.0-alpine
docker run -it --name="broker-m2" -p 10949:10949 -p 10952:10952 -p 10951:10951 -e "JAVA_OPT_EXT=-server -XMS128m -Xms128m -Xmn128m" -e "BROKE_IP=192.168.57.211" -e "BROKER_NAME=qin2" -e "BROKER_CLUSTER_NAME=qinc" -e "NAMESRV_ADDR=192.168.57.211:6358;192.168.57.211:6359" -e "BROKER_ID=1" -d rocketmqinc/rocketmq-broker:4.5.0-alpine
docker run --name="rocket-mq-console" -p 8081:8080 -e "NAMESRV_ADDR=188.*.*.151:6358;49.*.*.253:6358" -e "JAVA_OPTS=-Xms128m -Xmx128m -Xmn128m" -d styletang/rocketmq-console-ng:latest
登录到容器里,此时broker是启动失败的,需要在 加上
echo "brokerIP1=192.168.57.211">>$BROKER_CONFIG_FILE
echo "listenPort=10921" >>$BROKER_CONFIG_FILE
容器启动失败,想要进容器看错误可以加 /bin/bash
docker logs -f -t --tail 100 broker-m1 看报错
http://192.168.57.211:8081/
就可以看到控制台信息了
consumer:
public static void consumeMsg(String topic){
DefaultMQPushConsumer c = new DefaultMQPushConsumer("testGroupC");
c.setNamesrvAddr(addr);
try {
c.subscribe(topic,"");
} catch (MQClientException e) {
e.printStackTrace();
}
c.registerMessageListener(new MessageListenerConcurrently() {
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext ctx) {
for (MessageExt msg : list) {
System.out.println(new String(msg.getBody()) + " - " + msg);
}
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
});
try {
c.start();
} catch (MQClientException e) {
e.printStackTrace();
}
System.out.println("开始消费数据");
}
producer:
public static void sendMq(String topic ,String tag,String msg){
DefaultMQProducer defaultMQProducer = new DefaultMQProducer("testGroupP");
defaultMQProducer.setNamesrvAddr(addr);
//defaultMQProducer.setVipChannelEnabled(false);
try {
defaultMQProducer.start();
while (true){
int num = -1;
try {
num = System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
if (num == 48){
System.out.println("exit");
break;
}
if (num == 10){
continue;
}
System.out.println(num+msg);
Message message = new Message(topic, tag, (num+msg).getBytes(RemotingHelper.DEFAULT_CHARSET));
defaultMQProducer.send(message);
}
defaultMQProducer.shutdown();
} catch (MQClientException e) {
e.printStackTrace();
}catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (RemotingException e) {
e.printStackTrace();
} catch (MQBrokerException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
容器化:k8s
多活方案: