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

做网站开发考什么研企业品牌推广策划方案

做网站开发考什么研,企业品牌推广策划方案,校园网站开发的目的,做分析图很好用的网站文章目录 业务场景拦截器实现Spring Cloud Gateway介绍 业务场景 我们服务使用Spring Cloud微服务架构,使用Spring Cloud Gateway 作为网关,使用 Spring Cloud OpenFeign 作为服务间通信方式作为网关,主要作用是鉴权与路由转发。大多数应用场…

文章目录

    • 业务场景
    • 拦截器实现
    • 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 RedisTemplate<String, 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 Mono<Void> 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) {Map<String, String> unitIdHostMap = new HashMap<>();List<SignalVO> 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具有以下特点:

  1. 基于WebFlux:Spring Cloud Gateway基于Reactive编程模型,利用WebFlux框架实现非阻塞、事件驱动的异步处理,可以提供更好的性能和并发处理能力。

  2. 灵活的路由规则:Spring Cloud Gateway支持基于URI、请求方法、请求头等多种维度的路由规则配置,使得对请求进行灵活的路由转发变得简单易用。

  3. 过滤器:Spring Cloud Gateway的过滤器功能可以实现对请求和响应的预处理和后处理,包括请求日志记录、鉴权、路由转发、重定向等功能,可以满足各种需求的定制化处理。

  4. 集成性:Spring Cloud Gateway可以与其他Spring Cloud组件和微服务框架无缝集成,例如Eureka、Consul、Ribbon等,能够灵活地进行微服务的注册、发现和负载均衡。

总之,Spring Cloud Gateway是一个灵活、高性能的API网关服务,能够帮助开发者快速构建和部署微服务应用,实现请求路由、负载均衡、安全验证等功能。

http://www.yidumall.com/news/55274.html

相关文章:

  • 邢台企业网站建设服务网站推广属于哪些
  • 大连网站建设那家好东莞关键词优化实力乐云seo
  • 张家港手机网站建一个网站需要多少钱?
  • etc网站开发票什么是优化设计
  • 网站开发服务转包合同黑科技引流推广神器怎么下载
  • 广西建设厅网站首页安卓优化大师清理
  • 佛山网站建设维护互联网整合营销推广
  • 通河新村街道网站建设1688自然排名怎么做好
  • 建站工具 wordpress低价刷赞网站推广
  • 广州建站商城网站首页快速收录
  • 深圳做分销网站公司微信搜一搜排名优化
  • 阿里云虚拟主机做2个网站做网站优化哪家公司好
  • 做网站用什么网名好微信小程序官网
  • 做内衣模特接广告网站海外推广服务
  • 黄冈商城网站建设线上平台推广方案
  • 东莞哪家网站营销公司好友情连接出售
  • php和asp做网站哪个好上海seo推广方法
  • 做威士忌的网站什么是长尾关键词举例
  • 云商城是合法的吗软文优化
  • 美图秀秀可以做网站吗营销的主要目的有哪些
  • php连接wordpress天津seo诊断
  • 临朐网站做的好的免费站推广网站在线
  • 比较好的做网站的公司app 推广
  • 广州做手机网站咨询线上销售如何找到精准客户
  • 做本地网站应该选什么内容seo技术建站
  • 做网站需要字体切换自媒体营销代理
  • 网页设计毕业论文目录济南搜索引擎优化网站
  • 做p2p网站 预算多少快速优化关键词排名
  • 公安局网站不备案吗百度关键字搜索排名
  • wordpress 文字底色专业seo网络营销公司