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

零售网站模板新手可以自己学做网站设计吗

零售网站模板,新手可以自己学做网站设计吗,网站建设维护费,最新的域名网站文章目录 业务场景拦截器实现Spring Cloud Gateway介绍 业务场景 我们服务使用Spring Cloud微服务架构#xff0c;使用Spring Cloud Gateway 作为网关#xff0c;使用 Spring Cloud OpenFeign 作为服务间通信方式作为网关#xff0c;主要作用是鉴权与路由转发。大多数应用场… 文章目录 业务场景拦截器实现Spring Cloud Gateway介绍 业务场景 我们服务使用Spring Cloud微服务架构使用Spring Cloud Gateway 作为网关使用 Spring Cloud OpenFeign 作为服务间通信方式作为网关主要作用是鉴权与路由转发。大多数应用场景网关主要是针对前端的请求前端调用接口网关鉴权和转发。对于微服务间的调用一般都不经过网关直接根据注册服务列表路由到对应服务对于一般的微服务路由转发一般按照默认设置根据服务名转发到对应服务即可对于一些有转发规则的请求也可以根据规则如url前缀、某个参数的值、请求header里某个属性的匹配转发到对应服务简单的直接在配置文件里编写例如某个值到某个服务是已知的这些值是确定的和少量的可以根据不通值到对应的服务也可以自定义过滤器实现现在有一个需求需要根据参数的值路由到对应的服务但是参数的值不是枚举值数量不确定可能有很多值也不确定但是这个值对应哪个服务ip 端口是已知的需要实现根据这个业务规则动态路由到对应服务 拦截器实现 Spring Cloud Gateway的默认配置写法将服务名转化为小写根据名字匹配 spring:cloud:gateway:discovery:locator:enabled: truelower-case-service-id: truepredicates:- name: Pathargs:pattern: /services/serviceId.toLowerCase()/**filters:- name: RewritePathargs:regexp: /services/ serviceId.toLowerCase() /(?remaining.*)replacement: /${remaining}默认写法只根据服务名字匹配进行路由转发同服务名的多个微服务轮询转发一开始试着重写RouteToRequestUrlFilter发现没有作用根本走不到这个类后面在Component注解里指定了名称value org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter完全覆盖生效了具体代码如下核心是路由转发重新指定exchange.getAttributes().put(ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR, mergedUrl) package com.newatc.com.authorization;import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.newatc.com.SpringContextHolderUtil; import com.newatc.com.authorization.util.HttpClientUtil; import com.newatc.com.authorization.vo.SignalVO; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.cloud.gateway.route.Route; import org.springframework.cloud.gateway.support.ServerWebExchangeUtils; import org.springframework.context.annotation.Lazy; import org.springframework.core.Ordered; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.util.UriComponentsBuilder; import reactor.core.publisher.Mono;import java.net.URI; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; import java.util.regex.Pattern;/*** 重写Spring的RouteToRequestUrlFilter自定义unit服务路由** author yanyulin* date 2023-12-6 19:44:52*/ Component(value org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter) Lazy public class RouteToRequestUrlFilter implements GlobalFilter, Ordered {public static final int ROUTE_TO_URL_FILTER_ORDER 10000;private static final Log log LogFactory.getLog(RouteToRequestUrlFilter.class);private static final String SCHEME_REGEX [a-zA-Z]([a-zA-Z]|\\d|\\|\\.|-)*:.*;static final Pattern schemePattern Pattern.compile(SCHEME_REGEX);private static final String UNIT_API_PREFIX unit;private static final String UNIT_ADAPTER UNIT_ADAPTER;public static final String GET_UNIT_URL /services/core/api/signalcontrol/getSignalList;public static final String GET_UNIT_HOSTS /services/core/api/signalcontrol/getUnitHostList;private static final RedisTemplateString, String redisTemplate SpringContextHolderUtil.getBean(StringRedisTemplate.class);Value(${server.port})private Integer SERVER_PORT;public RouteToRequestUrlFilter() {}static boolean hasAnotherScheme(URI uri) {return schemePattern.matcher(uri.getSchemeSpecificPart()).matches() uri.getHost() null uri.getRawPath() null;}public int getOrder() {return ROUTE_TO_URL_FILTER_ORDER;}public MonoVoid filter(ServerWebExchange exchange, GatewayFilterChain chain) {Route route exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR);if (route null) {return chain.filter(exchange);} else {log.trace(RouteToRequestUrlFilter start);URI uri exchange.getRequest().getURI();boolean encoded ServerWebExchangeUtils.containsEncodedParts(uri);URI routeUri route.getUri();if (hasAnotherScheme(routeUri)) {exchange.getAttributes().put(ServerWebExchangeUtils.GATEWAY_SCHEME_PREFIX_ATTR, routeUri.getScheme());routeUri URI.create(routeUri.getSchemeSpecificPart());}log.debug(routeUri: JSON.toJSONString(routeUri));if (lb.equalsIgnoreCase(routeUri.getScheme()) routeUri.getHost() null) {throw new IllegalStateException(Invalid host: routeUri);} else {// 原先的UriURI mergedUrl UriComponentsBuilder.fromUri(uri).scheme(routeUri.getScheme()).host(routeUri.getHost()).port(routeUri.getPort()).build(encoded).toUri();// 如果是需要自定义指定路由的服务根据业务重写if (UNIT_API_PREFIX.equalsIgnoreCase(routeUri.getHost())) {try {log.info(mergedUrl前: JSON.toJSONString(mergedUrl));String[] pathArr uri.getPath().split(/);String unitId pathArr[pathArr.length - 1];String host ;if (StringUtils.hasText(unitId)) {host getUnitHost(unitId);}if (StringUtils.hasText(host)) {String newurl host mergedUrl.getPath();if (StringUtils.hasText(exchange.getRequest().getURI().getQuery())) {newurl newurl ? exchange.getRequest().getURI().getQuery();}URI newURI new URI(newurl);mergedUrl UriComponentsBuilder.fromUri(uri).scheme(newURI.getScheme()).host(newURI.getHost()).port(newURI.getPort()).build(encoded).toUri();log.debug(mergedUrl后: JSON.toJSONString(mergedUrl));}} catch (Exception e) {log.error(uri error, e);}}exchange.getAttributes().put(ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR, mergedUrl);return chain.filter(exchange);}}}/*** 根据信号机编号获取对应的服务适配器** param unitId* return*/private String getUnitHost(String unitId) {MapString, String unitIdHostMap new HashMap();ListSignalVO signalList;if (Boolean.TRUE.equals(redisTemplate.hasKey(UNIT_ADAPTER))) {signalList JSONArray.parseArray(redisTemplate.opsForValue().get(UNIT_ADAPTER), SignalVO.class);} else {String url http://localhost: SERVER_PORT GET_UNIT_URL;String info HttpClientUtil.doGet(url, null);signalList JSONArray.parseArray(info, SignalVO.class);if (null ! signalList !signalList.isEmpty()) {redisTemplate.opsForValue().set(UNIT_ADAPTER, JSON.toJSONString(signalList), 1, TimeUnit.MINUTES);}}if (null ! signalList !signalList.isEmpty()) {signalList.forEach(e - unitIdHostMap.put(e.getUnitId(), http:// e.getServerIp() : e.getServerPort()));}return unitIdHostMap.get(unitId);} }核心就是重新指定uri再设置进请求里 String newurl host mergedUrl.getPath(); if (StringUtils.hasText(exchange.getRequest().getURI().getQuery())) {newurl newurl ? exchange.getRequest().getURI().getQuery(); } URI newURI new URI(newurl); mergedUrl UriComponentsBuilder.fromUri(uri).scheme(newURI.getScheme()).host(newURI.getHost()).port(newURI.getPort()).build(encoded).toUri(); exchange.getAttributes().put(ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR, mergedUrl);这个是前期的做法后面考虑到服务间调用不需要走网关Gateway服务又废弃掉了改为在服务调用的地方使用拦截器过滤详情参考拦截器配置FeignClient根据业务规则实现微服务动态路由 Spring Cloud Gateway介绍 Spring Cloud Gateway是Spring Cloud生态系统中的一个组件用于构建基于Spring Boot的API网关服务。Spring Cloud Gateway基于Reactive编程模型使用WebFlux框架实现可以快速、可靠地构建和部署高性能的微服务应用程序。 Spring Cloud Gateway具有以下特点 基于WebFluxSpring Cloud Gateway基于Reactive编程模型利用WebFlux框架实现非阻塞、事件驱动的异步处理可以提供更好的性能和并发处理能力。 灵活的路由规则Spring Cloud Gateway支持基于URI、请求方法、请求头等多种维度的路由规则配置使得对请求进行灵活的路由转发变得简单易用。 过滤器Spring Cloud Gateway的过滤器功能可以实现对请求和响应的预处理和后处理包括请求日志记录、鉴权、路由转发、重定向等功能可以满足各种需求的定制化处理。 集成性Spring Cloud Gateway可以与其他Spring Cloud组件和微服务框架无缝集成例如Eureka、Consul、Ribbon等能够灵活地进行微服务的注册、发现和负载均衡。 总之Spring Cloud Gateway是一个灵活、高性能的API网关服务能够帮助开发者快速构建和部署微服务应用实现请求路由、负载均衡、安全验证等功能。
http://www.dnsts.com.cn/news/166000.html

相关文章:

  • phpcms 怎么做视频网站wordpress更换网址
  • 福州建设公司网站网站图片宽度
  • 门户网站建设和推广wordpress简单投稿
  • 长春网站建设新格石家庄今天又发现一例
  • 怎样查网站用什么程序做的如何建立一家公司网站
  • asp.net 网站开发项目化教程互联网最好的公司
  • 做网站怎么申请百度推广公司logo设计免费生成图片
  • 如何查看网站是否开启gzip寻找网络公司做公司网站升级改版
  • 国内做轮胎网站哪家好wordpress 酒业模板
  • dede网站制作教程seo课程心得体会
  • 门户网站 cms做网站基础教程
  • 东营优化网站搭建一个网站的具体步骤
  • 做的网站怎么上传到网上运行沈阳营销型网站建设
  • 成都专业网站推广个人推广app的妙招
  • 佛山网站制作哪家好哈尔滨网站优化公司
  • 南京明辉建设集团网站seo快排
  • 新网站的建设方案微信里我的微站是怎么弄的
  • 单页面网站制作教程专业网页设计费用
  • 网站建设方案推销淮安市工程造价信息网
  • 湖南做网站 磐石网络引领seo是搜索引擎吗
  • 世界网站设计公司推荐
  • 正定县建设局 网站wordpress怎么禁止评论
  • 平台网站怎么推广wordpress可折叠菜单
  • 网站被屏蔽怎么访问如何开一个网站
  • 网站开发网页设计北京师范大学出版社网站建设360元起全包
  • 哪个网站做五金冲压的建设电子商务网站市场分析
  • dedecms的网站系统设计结论关键词搜索趋势
  • 网页广告设计培训百度搜索关键词排名优化技术
  • 建设银行网站怎么先无贷款呢wordpress php5.4
  • 企业建站网站建站系统百度公司图片