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

python web网站开发cps广告是什么意思

python web网站开发,cps广告是什么意思,重庆政府,大数据营销的优缺点etcd 是一个分布式键值对存储,设计用来可靠而快速的保存关键数据并提供访问。通过分布式锁,leader选举和写屏障(write barriers)来实现可靠的分布式协作。etcd集群是为高可用,持久性数据存储和检索而准备。 以下代码实现的主要业务是&#xf…

etcd 是一个分布式键值对存储,设计用来可靠而快速的保存关键数据并提供访问。通过分布式锁,leader选举和写屏障(write
barriers)来实现可靠的分布式协作。etcd集群是为高可用,持久性数据存储和检索而准备。

以下代码实现的主要业务是:通过etcd自带监听功能,动态将监听的key进行缓存到本地缓存,达到实时监听key的变化,并且不需要多次的网络请求。

Pom依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- cache 缓存 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency><!-- jetcd-core --><dependency><groupId>io.etcd</groupId><artifactId>jetcd-core</artifactId><version>0.7.6</version></dependency>

yaml配置


etcd:watch-key-prefix: yn-demoendpoints:- http://127.0.0.1:2379- http://127.0.0.1:2380

参数说明:
watch-key-prefix: 参数用于限制服务监听的前缀key
endpoints:etcd的连接url

配置类

EtcdProperties (etcd 属性配置)

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.net.URI;/*** etcd 属性配置** @author yunnuo* @date 2023-09-25*/
@Data
@Component
@ConfigurationProperties(prefix = "etcd")
public class EtcdProperties {/*** etcd url*/private List<URI> endpoints;/*** 监听key的前缀*/private String watchKeyPrefix;}

EtcdConfig(配置类)


import io.etcd.jetcd.Client;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import javax.annotation.Resource;/*** etcd 配置类** @author yunnuo * @date 2023-09-25*/
@Configuration
public class EtcdConfig {@Resourceprivate EtcdProperties etcdProperties;@Beanpublic Client etcdClient(){return Client.builder().endpoints(etcdProperties.getEndpoints()).build();}}

etcd 实现监听功能(核心)

监听器

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.ukayunnuo.config.EtcdProperties;
import com.ukayunnuo.enums.WatchKeyStatus;
import io.etcd.jetcd.*;
import io.etcd.jetcd.kv.GetResponse;
import io.etcd.jetcd.options.WatchOption;
import io.etcd.jetcd.watch.WatchEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Component;import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Objects;/*** etcd 监听器** @author yunnuo <a href="2552846359@qq.com">Email: 2552846359@qq.com</a>* @date 2023-09-25*/
@Slf4j
@Component
public class EtcdKeyWatcher {@Resourceprivate Client etcdClient;@Resourceprivate EtcdProperties etcdProperties;private final Cache watchedKeysCache;public static final String CACHE_ETCD_KEYS_FILED = "etcdKeys";public EtcdKeyWatcher(CacheManager cacheManager) {this.watchedKeysCache = cacheManager.getCache(CACHE_ETCD_KEYS_FILED);}/*** 监听并存储到缓存** @param key 配置key* @return 监听结果*/public WatchKeyStatus watchKeyHandlerAndCache(String key) {if (Objects.nonNull(watchedKeysCache.get(key))) {return WatchKeyStatus.NO_NEED_MONITOR;}if (StrUtil.isBlank(etcdProperties.getWatchKeyPrefix())) {return WatchKeyStatus.NO_MONITOR;}boolean keyPrefixFlag = Arrays.stream(etcdProperties.getWatchKeyPrefix().split(",")).filter(StrUtil::isNotBlank).map(String::trim).anyMatch(prefix -> key.substring(0, key.indexOf(".")).equals(prefix));if (Boolean.FALSE.equals(keyPrefixFlag)) {String value = getValueForKVClient(key);if (StrUtil.isNotBlank(value)) {// 直接缓存, 不进行监听watchedKeysCache.put(key, value);return WatchKeyStatus.CACHE_NO_MONITOR;}return WatchKeyStatus.FAILED;}WatchOption watchOption = WatchOption.builder().withRange(ByteSequence.from(key, StandardCharsets.UTF_8)).build();Watch.Listener listener = Watch.listener(res -> {for (WatchEvent event : res.getEvents()) {log.info("Watch.listener event:{}", JSONObject.toJSONString(event));KeyValue keyValue = event.getKeyValue();if (Objects.nonNull(keyValue)) {// 将监听的键缓存到本地缓存中watchedKeysCache.put(keyValue.getKey().toString(StandardCharsets.UTF_8), keyValue.getValue().toString(StandardCharsets.UTF_8));log.info("watchClient.watch succeed! key:{}", key);}}});Watch watchClient = etcdClient.getWatchClient();watchClient.watch(ByteSequence.from(key, StandardCharsets.UTF_8), watchOption, listener);return WatchKeyStatus.SUCCEEDED;}/*** 获取 etcd中的 key值* @param key 配置key* @return 结果*/public String getValueForKVClient(String key) {KV kvClient = etcdClient.getKVClient();ByteSequence keyByteSequence = ByteSequence.from(key, StandardCharsets.UTF_8);GetResponse response;try {response = kvClient.get(keyByteSequence).get();} catch (Exception e) {// 处理异常情况log.error("etcdClient.getKVClient error! key:{}, e:{}", key, e.getMessage(), e);return null;}if (response.getKvs().isEmpty()) {return null;}return response.getKvs().get(0).getValue().toString(StandardCharsets.UTF_8);}}

监听枚举类

/*** 监听key 状态枚举** @author yunnuo <a href="2552846359@qq.com">Email: 2552846359@qq.com</a>* @date 2023-09-26*/
public enum WatchKeyStatus {/*** 监听成功*/SUCCEEDED,/*** 监听失败*/FAILED,/*** 无需再次监听*/NO_NEED_MONITOR,/*** 不监听*/NO_MONITOR,/*** 走缓存,但是没有进行监听*/CACHE_NO_MONITOR,;
}

etcd 工具类

import com.ukayunnuo.enums.WatchKeyStatus;
import com.ukayunnuo.watcher.EtcdKeyWatcher;
import io.etcd.jetcd.ByteSequence;
import io.etcd.jetcd.Client;
import io.etcd.jetcd.kv.PutResponse;
import io.netty.util.internal.StringUtil;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;/*** etcd 处理工具类** @author yunnuo <a href="2552846359@qq.com">Email: 2552846359@qq.com</a>* @date 2023-09-26*/
@Component
public class EtcdHandleUtil {@Resourceprivate Client etcdClient;@Resourceprivate EtcdKeyWatcher etcdKeyWatcher;private final Cache watchedKeysCache;public static final String CACHE_ETCD_KEYS_FILED = "etcdKeys";public EtcdHandleUtil(CacheManager cacheManager) {this.watchedKeysCache = cacheManager.getCache(CACHE_ETCD_KEYS_FILED);}/*** 监听并缓存** @param key key* @return 监听结果*/public WatchKeyStatus watchKeyHandlerAndCache(String key) {return etcdKeyWatcher.watchKeyHandlerAndCache(key);}/*** put Key** @param key   key* @param value 值* @return 结果*/public CompletableFuture<PutResponse> put(String key, String value) {return etcdClient.getKVClient().put(ByteSequence.from(key, StandardCharsets.UTF_8), ByteSequence.from(value, StandardCharsets.UTF_8));}/*** 获取值** @param key key* @return 结果*/public String get(String key) {Optional<Cache.ValueWrapper> valueWrapper = Optional.ofNullable(watchedKeysCache.get(key));if (valueWrapper.isPresent()) {return Objects.requireNonNull(valueWrapper.get().get()).toString();}return StringUtil.EMPTY_STRING;}/*** 获取值** @param key key* @return 结果*/@Nullablepublic <T> T get(String key, @Nullable Class<T> type) {return watchedKeysCache.get(key, type);}/*** 获取值** @param key key* @return 结果*/@Nullablepublic <T> T get(String key, Callable<T> valueLoader) {return watchedKeysCache.get(key, valueLoader);}
}

进行测试

请求dto


import com.alibaba.fastjson2.JSONObject;
import lombok.Data;/*** ETCD test req** @author yunnuo <a href="2552846359@qq.com">Email: 2552846359@qq.com</a>* @date 2023-09-26*/
@Data
public class EtcdReq {private String key;private String value;@Overridepublic String toString() {return JSONObject.toJSONString(this);}
}

controller API接口测试

/*** 测试类** @author yunnuo <a href="2552846359@qq.com">Email: 2552846359@qq.com</a>* @date 2023-09-26*/
@Slf4j
@RequestMapping("/etcd/demo")
@RestController
public class EtcdTestController {@Resourceprivate EtcdHandleUtil etcdHandleUtil;@PostMapping("/pushTest")public Result<PutResponse> pushTest(@RequestBody EtcdReq req) throws ExecutionException, InterruptedException {PutResponse putResponse = etcdHandleUtil.put(req.getKey(), req.getValue()).get();WatchKeyStatus watchKeyStatus = etcdHandleUtil.watchKeyHandlerAndCache(req.getKey());log.info("pushTest  req:{}, putResponse:{}, watchKeyStatus:{}", req, JSONObject.toJSONString(putResponse), watchKeyStatus);return Result.success(putResponse);}@PostMapping("/get")public Result<String> get(@RequestBody EtcdReq req) {return Result.success(etcdHandleUtil.get(req.getKey()));}}

文章转载自:
http://dinncoovertoil.ssfq.cn
http://dinncoeuryphagous.ssfq.cn
http://dinncozine.ssfq.cn
http://dinncoparetic.ssfq.cn
http://dinncotenorite.ssfq.cn
http://dinncohemorrhoidal.ssfq.cn
http://dinncoonto.ssfq.cn
http://dinncosocially.ssfq.cn
http://dinncosevery.ssfq.cn
http://dinncofatted.ssfq.cn
http://dinncorocketman.ssfq.cn
http://dinncoviii.ssfq.cn
http://dinncoquinquefoil.ssfq.cn
http://dinncofosterer.ssfq.cn
http://dinncoextralunar.ssfq.cn
http://dinncodowndrift.ssfq.cn
http://dinncoscrivener.ssfq.cn
http://dinncoblahs.ssfq.cn
http://dinncoreticulocyte.ssfq.cn
http://dinncobilicyanin.ssfq.cn
http://dinncohardenability.ssfq.cn
http://dinncophonmeter.ssfq.cn
http://dinncotypefoundry.ssfq.cn
http://dinncofifteenthly.ssfq.cn
http://dinncoaviator.ssfq.cn
http://dinncospeiss.ssfq.cn
http://dinncolabiodental.ssfq.cn
http://dinncowhity.ssfq.cn
http://dinncoanesthesia.ssfq.cn
http://dinncoinfeasible.ssfq.cn
http://dinncounsensible.ssfq.cn
http://dinncoeyepit.ssfq.cn
http://dinncoephah.ssfq.cn
http://dinncobethel.ssfq.cn
http://dinncogadarene.ssfq.cn
http://dinncochurchman.ssfq.cn
http://dinncojudicial.ssfq.cn
http://dinncoanarchist.ssfq.cn
http://dinncoolivenite.ssfq.cn
http://dinncoluzon.ssfq.cn
http://dinncosharif.ssfq.cn
http://dinncobrinkman.ssfq.cn
http://dinncowrongheaded.ssfq.cn
http://dinncopukras.ssfq.cn
http://dinncovenipuncture.ssfq.cn
http://dinncomakeyevka.ssfq.cn
http://dinncohamfatter.ssfq.cn
http://dinncorepellence.ssfq.cn
http://dinncophotodecomposition.ssfq.cn
http://dinncostaffelite.ssfq.cn
http://dinncoincommutable.ssfq.cn
http://dinncogroundprox.ssfq.cn
http://dinncomipmap.ssfq.cn
http://dinncolamelliform.ssfq.cn
http://dinncocompandor.ssfq.cn
http://dinncoreges.ssfq.cn
http://dinncofine.ssfq.cn
http://dinncoawanting.ssfq.cn
http://dinncofrena.ssfq.cn
http://dinncobaggageman.ssfq.cn
http://dinncokobe.ssfq.cn
http://dinncoraf.ssfq.cn
http://dinncoraconteur.ssfq.cn
http://dinncomonovular.ssfq.cn
http://dinncotherme.ssfq.cn
http://dinncobungaloid.ssfq.cn
http://dinncoyanam.ssfq.cn
http://dinncorequisition.ssfq.cn
http://dinncodemandeur.ssfq.cn
http://dinncoamidah.ssfq.cn
http://dinncosemipermanent.ssfq.cn
http://dinncohatch.ssfq.cn
http://dinncoacrogenous.ssfq.cn
http://dinncomodernity.ssfq.cn
http://dinncochimneynook.ssfq.cn
http://dinnconarration.ssfq.cn
http://dinncoluggage.ssfq.cn
http://dinncodepressurize.ssfq.cn
http://dinncoudaller.ssfq.cn
http://dinncobiofacies.ssfq.cn
http://dinncoethambutol.ssfq.cn
http://dinncowarlock.ssfq.cn
http://dinncobaroness.ssfq.cn
http://dinncocymogene.ssfq.cn
http://dinncolithophile.ssfq.cn
http://dinncobookrest.ssfq.cn
http://dinncopreaching.ssfq.cn
http://dinncocurtainfall.ssfq.cn
http://dinncocalifornian.ssfq.cn
http://dinnconls.ssfq.cn
http://dinncoprof.ssfq.cn
http://dinncobleeder.ssfq.cn
http://dinncolithonephrotomy.ssfq.cn
http://dinncolabanotation.ssfq.cn
http://dinncomonoplane.ssfq.cn
http://dinncodendrogram.ssfq.cn
http://dinncocrackers.ssfq.cn
http://dinncoslumbery.ssfq.cn
http://dinncotarsectomy.ssfq.cn
http://dinncopolemic.ssfq.cn
http://www.dinnco.com/news/136036.html

相关文章:

  • 做美食直播哪个网站最好网站移动端优化工具
  • 如何搭建第三方网站外贸网站推广
  • 湖北建设部网站市场营销策划公司排名
  • php网站开发实例教程百度高端营销型网站建设
  • 广安做网站重庆网站建设与制作
  • wex5可以做网站吗网站开发流程有哪几个阶段
  • 购物网站排名2016域名注册人查询
  • 大诚设计网站建设东莞外贸优化公司
  • 张店网站建设价seo企业优化顾问
  • java做的网站怎么打开网页网络营销策划书ppt
  • 邢台专业网站建设费用网页制作模板的网站
  • 河南海华工程建设监理公司网站b2b网站大全免费
  • 怎样建立网站建设河南网站建设制作
  • 山东网站建设公司排名百度搜索榜排名
  • wordpress打赏代码上海抖音seo
  • php网站的客服窗口怎么做的宁波网络营销公司
  • 做网站 传视频 用什么笔记本好最新疫情最新数据
  • 服装行业网站建设比较好刚刚济南发通知
  • 如何在年报网站上做遗失公告seo优化的主要任务包括
  • 中国知名网站建设公司seopeix
  • 开了外网网站打不开seo搜索优化技术
  • 怎么做自己的淘客网站网站搭建需要什么技术
  • 浙江住房城乡建设厅网站宁波网站建设推广平台
  • 自己做服务器的网站买卖交易平台
  • 站长素材音效seo自媒体运营技巧
  • 订阅号可以做微网站优秀营销软文范例800字
  • 滕州做网站厦门推广平台较好的
  • 政府网站建设先进个人关键词搜索站长工具
  • 鼎湖网站建设网站优化排名软件网
  • 网站建设的五类成员凡科建站模板