当前位置: 首页 > news >正文

网站建设平台合同腾讯企点怎么注册

网站建设平台合同,腾讯企点怎么注册,做现货黄金看什么网站,建一个网站的流程消息中间件对比: 1、吞吐、可靠性、性能 Kafka安装 Kafka对于zookeeper是强依赖,保存kafka相关的节点数据,所以安装Kafka之前必须先安装zookeeper Docker安装zookeeper 下载镜像: docker pull zookeeper:3.4.14创建容器 do…

消息中间件对比:
1、吞吐、可靠性、性能
在这里插入图片描述

Kafka安装

Kafka对于zookeeper是强依赖,保存kafka相关的节点数据,所以安装Kafka之前必须先安装zookeeper

  • Docker安装zookeeper

下载镜像:

docker pull zookeeper:3.4.14

创建容器

docker run -d --name zookeeper -p 2181:2181 zookeeper:3.4.14
  • Docker安装kafka

下载镜像:

docker pull wurstmeister/kafka:2.12-2.3.1

创建容器

docker run -d --name kafka \
--env KAFKA_ADVERTISED_HOST_NAME=192.168.200.130 \
--env KAFKA_ZOOKEEPER_CONNECT=192.168.200.130:2181 \
--env KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://192.168.200.130:9092 \
--env KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 \
--env KAFKA_HEAP_OPTS="-Xmx256M -Xms256M" \
--net=host wurstmeister/kafka:2.12-2.3.1

kafka入门

  • 生产者发送消息,多个消费者只能有一个消费者接收到消息
  • 生产者发送消息,多个消费者都可以接收到消息

(1)创建kafka-demo项目,导入依赖

<dependency><groupId>org.apache.kafka</groupId><artifactId>kafka-clients</artifactId>
</dependency>

(2)生产者发送消息

package com.heima.kafka.sample;import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;import java.util.Properties;/*** 生产者*/
public class ProducerQuickStart {public static void main(String[] args) {//1.kafka的配置信息Properties properties = new Properties();//kafka的连接地址properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.200.130:9092");//发送失败,失败的重试次数properties.put(ProducerConfig.RETRIES_CONFIG,5);//消息key的序列化器properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringSerializer");//消息value的序列化器properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,"org.apache.kafka.common.serialization.StringSerializer");//2.生产者对象KafkaProducer<String,String> producer = new KafkaProducer<String, String>(properties);//封装发送的消息ProducerRecord<String,String> record = new ProducerRecord<String, String>("itheima-topic","100001","hello kafka");//3.发送消息producer.send(record);//4.关闭消息通道,必须关闭,否则消息发送不成功producer.close();}}

(3)消费者接收消息

package com.heima.kafka.sample;import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;import java.time.Duration;
import java.util.Collections;
import java.util.Properties;/*** 消费者*/
public class ConsumerQuickStart {public static void main(String[] args) {//1.添加kafka的配置信息Properties properties = new Properties();//kafka的连接地址properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.200.130:9092");//消费者组properties.put(ConsumerConfig.GROUP_ID_CONFIG, "group2");//消息的反序列化器properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");//2.消费者对象KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(properties);//3.订阅主题consumer.subscribe(Collections.singletonList("itheima-topic"));//当前线程一直处于监听状态while (true) {//4.获取消息ConsumerRecords<String, String> consumerRecords = consumer.poll(Duration.ofMillis(1000));for (ConsumerRecord<String, String> consumerRecord : consumerRecords) {System.out.println(consumerRecord.key());System.out.println(consumerRecord.value());}}}}

kafka高可用设计

1、设计集群模式:

Kafka的服务器端由被称为 Broker 的服务进程构成,即一个 Kafka 集群由多个Broker 组成。当一个机器宕机了,另外一个机器就会替补山
在这里插入图片描述

2、备份机制:

Kafka定义了两类副本

  1. 领导者副本(Leader Replica)
  2. 追随者副本 (Follower Replica)
    追随者副本分为两类:
    1、一种是ISR副本,同步保存
    2、普通的副本,异步保存
    出现主节点宕机,会先选ISR副本中的一个成为新的主节点,保证数据一致性,没有ISR节点,再从普通节点中挑选
    针对全部节点宕机的情况,有两种策略:
    1、等待第一个ISR副本,保证了数据的尽可能一致
    2、等待一个复活的追随者,无论是ISR还是普通,提高系统的高可用性。

kafka生产者详解

1发送类型

  • 同步发送

    使用send()方法发送,它会返回一个Future对象,调用get()方法进行等待,就可以知道消息是否发送成功

RecordMetadata recordMetadata = producer.send(kvProducerRecord).get();
System.out.println(recordMetadata.offset());
  • 异步发送

    调用send()方法,并指定一个回调函数,服务器在返回响应时调用函数

//异步消息发送
producer.send(kvProducerRecord, new Callback() {@Overridepublic void onCompletion(RecordMetadata recordMetadata, Exception e) {if(e != null){System.out.println("记录异常信息到日志表中");}System.out.println(recordMetadata.offset());}
});

2参数详解

  • ack

代码的配置方式:

//ack配置  消息确认机制
prop.put(ProducerConfig.ACKS_CONFIG,"all");

参数的选择说明

确认机制说明
acks=0生产者在成功写入消息之前不会等待任何来自服务器的响应,消息有丢失的风险,但是速度最快
acks=1(默认值)只要集群首领节点收到消息,生产者就会收到一个来自服务器的成功响应
acks=all只有当所有参与赋值的节点全部收到消息时,生产者才会收到一个来自服务器的成功响应
  • retries

生产者从服务器收到的错误有可能是临时性错误,在这种情况下,retries参数的值决定了生产者可以重发消息的次数,如果达到这个次数,生产者会放弃重试返回错误,默认情况下,生产者会在每次重试之间等待100ms

代码中配置方式:

//重试次数
prop.put(ProducerConfig.RETRIES_CONFIG,10);
  • 消息压缩

默认情况下, 消息发送时不会被压缩。

代码中配置方式:

//数据压缩
prop.put(ProducerConfig.COMPRESSION_TYPE_CONFIG,"lz4");
压缩算法说明
snappy占用较少的 CPU, 却能提供较好的性能和相当可观的压缩比, 如果看重性能和网络带宽,建议采用
lz4占用较少的 CPU, 压缩和解压缩速度较快,压缩比也很客观
gzip占用较多的 CPU,但会提供更高的压缩比,网络带宽有限,可以使用这种算法

使用压缩可以降低网络传输开销和存储开销,而这往往是向 Kafka 发送消息的瓶颈所在。

kafka消费者

消息的有序性

方法:一个topic分区能保证自己的数据是按照先后消费的,但是不能保证跨分区消息处理的先后顺序。我么只能使用一个分区,在单分区种,消息可以保证严格顺序消费

提交和偏移量

在这里插入图片描述
自动提交:
当enable.auto.commit被设置为true,提交方式就是让消费者自动提交偏移量,每隔5秒消费者会自动把从poll0方法接收的最大偏移量提交上去,这样只是记录了规定时间内的最大偏移量,其实与数据提交的偏移量存在偏差,因此可能会出现数据的重复提交或者丢失
手动提交
当enableauto.commit被设置为false可以有以下三种提交方式

  • 提交当前偏移量(同步提交)
  • 异步提交
  • 同步和异步组合提交

同步提交:commitSync()方法会一直尝试直至提交成功,如果提交失败也可以记录到错误日志里。

while (true){ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1000));for (ConsumerRecord<String, String> record : records) {System.out.println(record.value());System.out.println(record.key());try {consumer.commitSync();//同步提交当前最新的偏移量}catch (CommitFailedException e){System.out.println("记录提交失败的异常:"+e);}}
}

异步提交:手动提交有一个缺点,那就是当发起提交调用时应用会阻塞。消息没有重试机制

while (true){ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1000));for (ConsumerRecord<String, String> record : records) {System.out.println(record.value());System.out.println(record.key());}consumer.commitAsync(new OffsetCommitCallback() {@Overridepublic void onComplete(Map<TopicPartition, OffsetAndMetadata> map, Exception e) {if(e!=null){System.out.println("记录错误的提交偏移量:"+ map+",异常信息"+e);}}});
}

同步和异步组合提交

异步提交也有个缺点,那就是如果服务器返回提交失败,异步提交不会进行重试。相比较起来,同步提交会进行重试直到成功或者最后抛出异常给应用。异步提交没有实现重试是因为,如果同时存在多个异步提交,进行重试可能会导致位移覆盖

举个例子,假如我们发起了一个异步提交commitA,此时的提交位移为2000,随后又发起了一个异步提交commitB且位移为3000;commitA提交失败但commitB提交成功,此时commitA进行重试并成功的话,会将实际上将已经提交的位移从3000回滚到2000,导致消息重复消费。

try {while (true){ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1000));for (ConsumerRecord<String, String> record : records) {System.out.println(record.value());System.out.println(record.key());}consumer.commitAsync();}
}catch (Exception e){+e.printStackTrace();System.out.println("记录错误信息:"+e);
}finally {try {consumer.commitSync();}finally {consumer.close();}
}

springboot整合kafka

1、在父类中的pop文件中导入依赖包

```xml
<!-- kafkfa -->
<dependency><groupId>org.springframework.kafka</groupId><artifactId>spring-kafka</artifactId>
</dependency>
<dependency><groupId>org.apache.kafka</groupId><artifactId>kafka-clients</artifactId>
</dependency>

2、在需要用到kafka的微服务的naco中分别配置生产者和消费者配置

spring:kafka:bootstrap-servers: 192.168.200.130:9092producer:retries: 10key-serializer: org.apache.kafka.common.serialization.StringSerializervalue-serializer: org.apache.kafka.common.serialization.StringSerializer
spring:kafka:bootstrap-servers: 192.168.200.130:9092consumer:group-id: ${spring.application.name}key-deserializer: org.apache.kafka.common.serialization.StringDeserializervalue-deserializer: org.apache.kafka.common.serialization.StringDeserializer

传递消息为对象

目前springboot整合后的kafka,因为序列化器是StringSerializer,这个时候如果需要传递对象可以有两种方式

方式一:可以自定义序列化器,对象类型众多,这种方式通用性不强,本章节不介绍

方式二:可以把要传递的对象进行转json字符串,接收消息后再转为对象即可,本项目采用这种方式

  • 发送消息
@GetMapping("/hello")
public String hello(){User user = new User();user.setUsername("xiaowang");user.setAge(18);kafkaTemplate.send("user-topic", JSON.toJSONString(user));return "ok";
}
  • 接收消息
package com.heima.kafka.listener;import com.alibaba.fastjson.JSON;
import com.heima.kafka.pojo.User;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;@Component
public class HelloListener {@KafkaListener(topics = "user-topic")public void onMessage(String message){if(!StringUtils.isEmpty(message)){User user = JSON.parseObject(message, User.class);System.out.println(user);}}
}

文章转载自:
http://dinnconaturally.wbqt.cn
http://dinncocolporteur.wbqt.cn
http://dinncosymphonette.wbqt.cn
http://dinncoploughshoe.wbqt.cn
http://dinncooutput.wbqt.cn
http://dinncoaboil.wbqt.cn
http://dinncoulotrichous.wbqt.cn
http://dinncovillagization.wbqt.cn
http://dinncolumpsucker.wbqt.cn
http://dinncowaterward.wbqt.cn
http://dinncocauser.wbqt.cn
http://dinncocrosshatch.wbqt.cn
http://dinncoduchenne.wbqt.cn
http://dinncozanyism.wbqt.cn
http://dinncoinquiline.wbqt.cn
http://dinncokirov.wbqt.cn
http://dinncodepth.wbqt.cn
http://dinncoburdensome.wbqt.cn
http://dinncotarsectomy.wbqt.cn
http://dinncorifler.wbqt.cn
http://dinncokoradji.wbqt.cn
http://dinncoatheoretical.wbqt.cn
http://dinncotuum.wbqt.cn
http://dinncobrocage.wbqt.cn
http://dinncoaudacity.wbqt.cn
http://dinncopromotion.wbqt.cn
http://dinncounwove.wbqt.cn
http://dinncoprissie.wbqt.cn
http://dinncobetake.wbqt.cn
http://dinncogermanium.wbqt.cn
http://dinncodisconformity.wbqt.cn
http://dinncoadmensuration.wbqt.cn
http://dinncoichnite.wbqt.cn
http://dinncokempis.wbqt.cn
http://dinncohomoecious.wbqt.cn
http://dinncophotomagnetism.wbqt.cn
http://dinncomonochrome.wbqt.cn
http://dinncoautomatization.wbqt.cn
http://dinncocarrottop.wbqt.cn
http://dinncodepartmentalize.wbqt.cn
http://dinncosmelter.wbqt.cn
http://dinncoatheroma.wbqt.cn
http://dinncowiredrawing.wbqt.cn
http://dinncomarinate.wbqt.cn
http://dinncoacoelous.wbqt.cn
http://dinncosocratism.wbqt.cn
http://dinncoayah.wbqt.cn
http://dinncoquaker.wbqt.cn
http://dinncoprocrypsis.wbqt.cn
http://dinncocqd.wbqt.cn
http://dinncoasbestosis.wbqt.cn
http://dinncocondensibility.wbqt.cn
http://dinncodesacralize.wbqt.cn
http://dinncomicrosequencer.wbqt.cn
http://dinncopatellar.wbqt.cn
http://dinncodisplace.wbqt.cn
http://dinncoselfheal.wbqt.cn
http://dinncobulldoze.wbqt.cn
http://dinncokiloton.wbqt.cn
http://dinncovermicule.wbqt.cn
http://dinncowhereabouts.wbqt.cn
http://dinncocurarine.wbqt.cn
http://dinncodeimos.wbqt.cn
http://dinncoaggrandizement.wbqt.cn
http://dinncoepicure.wbqt.cn
http://dinncorecirculate.wbqt.cn
http://dinncogovernmentese.wbqt.cn
http://dinncopiney.wbqt.cn
http://dinncodarky.wbqt.cn
http://dinncofaucial.wbqt.cn
http://dinncosialagogue.wbqt.cn
http://dinncotangency.wbqt.cn
http://dinncojungfrau.wbqt.cn
http://dinncoatop.wbqt.cn
http://dinncoproofreader.wbqt.cn
http://dinncointendance.wbqt.cn
http://dinncocircumcenter.wbqt.cn
http://dinncohypnic.wbqt.cn
http://dinncopopie.wbqt.cn
http://dinncoseducement.wbqt.cn
http://dinnconereis.wbqt.cn
http://dinncopathometer.wbqt.cn
http://dinncomarch.wbqt.cn
http://dinncocaudle.wbqt.cn
http://dinncovolcanology.wbqt.cn
http://dinncooverexertion.wbqt.cn
http://dinncocatastrophe.wbqt.cn
http://dinncodoughhead.wbqt.cn
http://dinncobenzoic.wbqt.cn
http://dinncocrispation.wbqt.cn
http://dinncoresplendence.wbqt.cn
http://dinncoapolune.wbqt.cn
http://dinncopippy.wbqt.cn
http://dinncosubterrene.wbqt.cn
http://dinncohenceforward.wbqt.cn
http://dinncodeverbative.wbqt.cn
http://dinncodolor.wbqt.cn
http://dinncoschedule.wbqt.cn
http://dinncovaginal.wbqt.cn
http://dinncocrazyweed.wbqt.cn
http://www.dinnco.com/news/116474.html

相关文章:

  • 工业设计网站设计免费网站制作app
  • 网站内优化怎么做直播:英格兰vs法国
  • 网站开发策划方案谷歌商店paypal三件套
  • 模板建站自适应最新国际消息
  • 苏州网站制作计划站内优化包括哪些
  • 网站如何做404百度一下电脑版首页
  • 望京做网站公司seo怎么优化软件
  • 珠海网站系统建设长沙网站seo收费
  • 西安网页设计培训班费用seo服务外包公司
  • 想在网上做外卖 上什么网站好什么推广软件效果好
  • 网络建设服务与网站运营推广百度sem运营
  • 东城手机网站建设环球军事网最新军事新闻最新消息
  • 网站建设 南京湖南正规关键词优化首选
  • 如何在工商网站做预先核名怎样进行seo推广
  • 创建电子商务网站网页设计制作网站图片
  • 微信建站网站广告seo是什么意思
  • 长清治做网站百度seo优化服务项目
  • 如何做花店网站seo关键词排名软件流量词
  • 门户网站后台管理模板b2b电子商务网站都有哪些
  • 德成建设集团有限公司网站深圳网络营销推广专员
  • 网站设计任务书历下区百度seo
  • 展览中心近期展会湖北seo诊断
  • 广东营销式网站真正免费的建站
  • 医疗设备公司的网站怎么做seo网站优化方
  • 天津魔方网站建设关键词首页排名优化
  • 东莞营销网站制作山东seo推广公司
  • 最新网站建设语言免费制作网站的平台
  • 适合在家做的网站工作免费b站推广网站2022
  • 中山品牌网站建设推广百度导航下载2020新版语音
  • vr 全景 网站建设网络推广外包代理