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

网站建设行业动态免费网站在线客服软件

网站建设行业动态,免费网站在线客服软件,互联网行业都有哪些工作赚钱,什么网站可以自己做房子设计目录一、网关基本概念1、API网关介绍2、Spring Cloud Gateway3、Spring Cloud Gateway核心概念二、创建service_gateway模块(网关服务)1、创建service_gateway模块2、在pom.xml引入依赖3、编写application.properties配置文件4、编写启动类5、前端端口号…

目录

  • 一、网关基本概念
    • 1、API网关介绍
    • 2、Spring Cloud Gateway
    • 3、Spring Cloud Gateway核心概念
  • 二、创建service_gateway模块(网关服务)
    • 1、创建service_gateway模块
    • 2、在pom.xml引入依赖
    • 3、编写application.properties配置文件
    • 4、编写启动类
    • 5、前端端口号修改
  • 三、网关相关配置
    • 1、网关解决跨域问题
    • 2、gateway做统一的权限认证
    • 3、自定义异常处理

一、网关基本概念

1、API网关介绍

API 网关出现的原因是微服务架构的出现,不同的微服务一般会有不同的网络地址,而外部客户端可能需要调用多个服务的接口才能完成一个业务需求,如果让客户端直接与各个微服务通信,会有以下的问题:
(1)客户端会多次请求不同的微服务,增加了客户端的复杂性。
(2)存在跨域请求,在一定场景下处理相对复杂。
(3)认证复杂,每个服务都需要独立认证。
(4)难以重构,随着项目的迭代,可能需要重新划分微服务。例如,可能将多个服务合并成一个或者将一个服务拆分成多个。如果客户端直接与微服务通信,那么重构将会很难实施。
(5)某些微服务可能使用了防火墙 / 浏览器不友好的协议,直接访问会有一定的困难。
以上这些问题可以借助 API 网关解决。API 网关是介于客户端和服务器端之间的中间层,所有的外部请求都会先经过 API 网关这一层。也就是说,API 的实现方面更多的考虑业务逻辑,而安全、性能、监控可以交由 API 网关来做,这样既提高业务灵活性又不缺安全性

2、Spring Cloud Gateway

Spring cloud gateway是spring官方基于Spring 5.0、Spring Boot2.0和Project Reactor等技术开发的网关,Spring Cloud Gateway旨在为微服务架构提供简单、有效和统一的API路由管理方式,Spring Cloud Gateway作为Spring Cloud生态系统中的网关,目标是替代Netflix Zuul,其不仅提供统一的路由方式,并且还基于Filter链的方式提供了网关基本的功能,例如:安全、监控/埋点、限流等。

在这里插入图片描述

3、Spring Cloud Gateway核心概念

网关提供API全托管服务,丰富的API管理功能,辅助企业管理大规模的API,以降低管理成本和安全风险,包括协议适配、协议转发、安全策略、防刷、流量、监控日志等贡呢。一般来说网关对外暴露的URL或者接口信息,我们统称为路由信息。如果研发过网关中间件或者使用过Zuul的人,会知道网关的核心是Filter以及Filter Chain(Filter责任链)。Sprig Cloud Gateway也具有路由和Filter的概念。下面介绍一下Spring Cloud Gateway中几个重要的概念。
(1)路由。路由是网关最基础的部分,路由信息有一个ID、一个目的URL、一组断言和一组Filter组成。如果断言路由为真,则说明请求的URL和配置匹配
(2)断言。Java8中的断言函数。Spring Cloud Gateway中的断言函数输入类型是Spring5.0框架中的ServerWebExchange。Spring Cloud Gateway中的断言函数允许开发者去定义匹配来自于http request中的任何信息,比如请求头和参数等。
(3)过滤器。一个标准的Spring webFilter。Spring cloud gateway中的filter分为两种类型的Filter,分别是Gateway Filter和Global Filter。过滤器Filter将会对请求和响应进行修改处理
在这里插入图片描述
如图所示,Spring cloud Gateway发出请求。然后再由Gateway Handler Mapping中找到与请求相匹配的路由,将其发送到Gateway web handler。Handler再通过指定的过滤器链将请求发送到实际的服务执行业务逻辑,然后返回。

二、创建service_gateway模块(网关服务)

1、创建service_gateway模块

在这里插入图片描述

2、在pom.xml引入依赖

<dependencies><dependency><groupId>com.donglin</groupId><artifactId>service_utils</artifactId><version>0.0.1-SNAPSHOT</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency><!-- 服务注册 --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency>
</dependencies>

3、编写application.properties配置文件

# 服务端口
server.port=8222
# 服务名
spring.application.name=service-gateway# nacos服务地址
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848#使用服务发现路由
spring.cloud.gateway.discovery.locator.enabled=true#设置路由id
spring.cloud.gateway.routes[0].id=service-hosp
#设置路由的uri
spring.cloud.gateway.routes[0].uri=lb://service-hosp
#设置路由断言,代理servicerId为auth-service的/auth/路径
spring.cloud.gateway.routes[0].predicates= Path=/*/hosp/**#设置路由id
spring.cloud.gateway.routes[1].id=service-cmn
#设置路由的uri
spring.cloud.gateway.routes[1].uri=lb://service-cmn
#设置路由断言,代理servicerId为auth-service的/auth/路径
spring.cloud.gateway.routes[1].predicates= Path=/*/cmn/**

yml文件:

server:port: 8222spring:application:name: service-gatewaycloud:gateway:discovery:locator:enabled: trueroutes:- id:service-hosp1uri: lb://service-hosppredicates:- Path=/*/hosp/** # 路径匹配- id: service-hosp2uri: lb://service-hosppredicates:- Path=/*/user/** # 路径匹配- id: service-cmnuri: lb://service-cmnpredicates:- Path=/*/cmn/** # 路径匹配nacos:discovery:server-addr: 127.0.0.1:8848

4、编写启动类

@SpringBootApplication
public class ApiGatewayApplication {public static void main(String[] args) {SpringApplication.run(ApiGatewayApplication.class, args);}
}

5、前端端口号修改

env.development
在这里插入图片描述

三、网关相关配置

1、网关解决跨域问题

跨域不一定都会有跨域问题。因为跨域问题是浏览器对于ajax请求的一种安全限制:一个页面发起的ajax请求,只能是与当前页域名相同的路径,这能有效的阻止跨站攻击。因此:跨域问题 是针对ajax的一种限制。
但是这却给我们的开发带来了不便,而且在实际生产环境中,肯定会有很多台服务器之间交互,地址和端口都可能不同
(1)创建配置类
在这里插入图片描述

@Configuration
public class CorsConfig {@Beanpublic CorsWebFilter corsFilter() {CorsConfiguration config = new CorsConfiguration();config.addAllowedMethod("*");config.addAllowedOrigin("*");config.addAllowedHeader("*");UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());source.registerCorsConfiguration("/**", config);return new CorsWebFilter(source);}
}

目前我们已经在网关做了跨域处理,那么service服务就不需要再做跨域处理了,将之前在controller类上添加过@CrossOrigin标签的去掉,防止程序异常

2、gateway做统一的权限认证

添加依赖

<dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId><version>2.9.0</version>
</dependency>

统一处理会员登录与外部不允许访问的服务

@Component
public class AuthGlobalFilter implements GlobalFilter, Ordered {private AntPathMatcher antPathMatcher = new AntPathMatcher();@Overridepublic Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {//ServerHttpRequest:webflux//HttpServletRequest:servletServerHttpRequest request = exchange.getRequest();String path = request.getURI().getPath();//对于登录接口的请求就不要拦截了if (antPathMatcher.match("/admin/user/**",path)){return chain.filter(exchange);}else {  //对于非登录接口,验证:必须登录之后才能通过List<String> strings = request.getHeaders().get("X-Token");if (strings.size() == 0){//拦截ServerHttpResponse response = exchange.getResponse();response.setStatusCode(HttpStatus.FORBIDDEN);JsonObject message = new JsonObject();message.addProperty("success", false);message.addProperty("code", 28004);message.addProperty("data", "鉴权失败");byte[] bits = message.toString().getBytes(StandardCharsets.UTF_8);DataBuffer buffer = response.bufferFactory().wrap(bits);//response.setStatusCode(HttpStatus.UNAUTHORIZED);//指定编码,否则在浏览器中会中文乱码response.getHeaders().add("Content-Type", "application/json;charset=UTF-8");response.writeWith(Mono.just(buffer));return response.setComplete();}else {return chain.filter(exchange);}}}//影响的是全局过滤器的执行顺序,值越小优先级越高@Overridepublic int getOrder() {return 0;}
}

改进

package com.donglin.yygh.filter;import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import java.util.List;@Component
public class AuthGlobalFilter implements GlobalFilter, Ordered {private AntPathMatcher antPathMatcher = new AntPathMatcher();@Overridepublic Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {//ServerHttpRequest:webflux//HttpServletRequest:servletServerHttpRequest request = exchange.getRequest();String path = request.getURI().getPath();//对于登录接口的请求就不要拦截了if (antPathMatcher.match("/admin/user/**",path)){return chain.filter(exchange);}else {  //对于非登录接口,验证:必须登录之后才能通过List<String> strings = request.getHeaders().get("X-Token");if (strings == null){//拦截ServerHttpResponse response = exchange.getResponse();response.setStatusCode(HttpStatus.SEE_OTHER);//路由跳转response.getHeaders().set(HttpHeaders.LOCATION,"http://localhost:9528");return response.setComplete();}else {return chain.filter(exchange);}}}//影响的是全局过滤器的执行顺序,值越小优先级越高@Overridepublic int getOrder() {return 0;}
}

3、自定义异常处理

服务网关调用服务时可能会有一些异常或服务不可用,它返回错误信息不友好,需要我们覆盖处理
ErrorHandlerConfig:

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.web.ResourceProperties;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.reactive.error.ErrorAttributes;
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.web.reactive.result.view.ViewResolver;import java.util.Collections;
import java.util.List;/*** 覆盖默认的异常处理**/
@Configuration
@EnableConfigurationProperties({ServerProperties.class, ResourceProperties.class})
public class ErrorHandlerConfig {private final ServerProperties serverProperties;private final ApplicationContext applicationContext;private final ResourceProperties resourceProperties;private final List<ViewResolver> viewResolvers;private final ServerCodecConfigurer serverCodecConfigurer;public ErrorHandlerConfig(ServerProperties serverProperties,ResourceProperties resourceProperties,ObjectProvider<List<ViewResolver>> viewResolversProvider,ServerCodecConfigurer serverCodecConfigurer,ApplicationContext applicationContext) {this.serverProperties = serverProperties;this.applicationContext = applicationContext;this.resourceProperties = resourceProperties;this.viewResolvers = viewResolversProvider.getIfAvailable(Collections::emptyList);this.serverCodecConfigurer = serverCodecConfigurer;}@Bean@Order(Ordered.HIGHEST_PRECEDENCE)public ErrorWebExceptionHandler errorWebExceptionHandler(ErrorAttributes errorAttributes) {JsonExceptionHandler exceptionHandler = new JsonExceptionHandler(errorAttributes,this.resourceProperties,this.serverProperties.getError(),this.applicationContext);exceptionHandler.setViewResolvers(this.viewResolvers);exceptionHandler.setMessageWriters(this.serverCodecConfigurer.getWriters());exceptionHandler.setMessageReaders(this.serverCodecConfigurer.getReaders());return exceptionHandler;}
}

JsonExceptionHandler:

import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.boot.autoconfigure.web.ResourceProperties;
import org.springframework.boot.autoconfigure.web.reactive.error.DefaultErrorWebExceptionHandler;
import org.springframework.boot.web.reactive.error.ErrorAttributes;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.web.reactive.function.server.*;import java.util.HashMap;
import java.util.Map;/*** 自定义异常处理** <p>异常时用JSON代替HTML异常信息<p>**/
public class JsonExceptionHandler extends DefaultErrorWebExceptionHandler {public JsonExceptionHandler(ErrorAttributes errorAttributes, ResourceProperties resourceProperties,ErrorProperties errorProperties, ApplicationContext applicationContext) {super(errorAttributes, resourceProperties, errorProperties, applicationContext);}/*** 获取异常属性*/@Overrideprotected Map<String, Object> getErrorAttributes(ServerRequest request, boolean includeStackTrace) {Map<String, Object> map = new HashMap<>();map.put("success", false);map.put("code", 20005);map.put("message", "网关失败");map.put("data", null);return map;}/*** 指定响应处理方法为JSON处理的方法* @param errorAttributes*/@Overrideprotected RouterFunction<ServerResponse> getRoutingFunction(ErrorAttributes errorAttributes) {return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse);}/*** 根据code获取对应的HttpStatus* @param errorAttributes*/@Overrideprotected HttpStatus getHttpStatus(Map<String, Object> errorAttributes) {return HttpStatus.OK;}
}
http://www.ritt.cn/news/22263.html

相关文章:

  • 互联网网站建设情况统计表网络营销软文范例500
  • 网站建设发展状况优化大师电脑版下载
  • github建网站中国营销策划第一人
  • 我的世界做壁纸的网站链接交易网
  • 深圳网站设计首选柚米最近热点新闻事件
  • 南阳百度网站推广百度新闻搜索
  • 护肤品网站建设的意义北京网站快速排名优化
  • 企业网站推广的方法有?公司网页制作
  • 网站开发需要有什么证书网址收录入口
  • 响应式网站检测工具网络整合营销理论
  • html导航网站源码百度指数趋势
  • 四川平台网站建设方案软件发布网
  • 典型的电子商务网站有哪些知乎软文推广
  • 杭州做微信网站软件公司百度seo优化方案
  • 个人网站怎么做银行卡支付谷歌浏览器下载手机版安卓
  • 网站建设的会计科目网络推广哪个平台效果最好
  • 企业自适应网站制作域名
  • 厦门php网站建设社群营销平台有哪些
  • 新增网站推广教程网站制作软件
  • 做视频网站用哪个模板太原优化排名推广
  • 建个网站大概需要多久谷歌浏览器下载手机版官网中文
  • 烟台建站模板源码长沙seo招聘
  • 达州网站开发qinsanw如何建立网上销售平台
  • 淄博网赢网站建设培训课程设计
  • wordpress 添加插件南宁关键词优化服务
  • 云加速应用于html网站建立网站步骤
  • php网站开发实用技术答案打开百度搜索引擎
  • html5 国内网站建设免费的网页制作软件
  • 成都市住房和城乡建设局官方网站宠物美容师宠物美容培训学校
  • 建设网站需要提前准备的条件软文推广收费