JAVA全系列 教程
3762个小节阅读:7090.4k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
授权规则可以对调用方的来源做控制,有白名单和黑名单两种方式。
点击左侧菜单的授权,可以看到授权规则:
资源名:就是受保护的资源,例如/payment/{query}
流控应用:是来源者的名单,
注意:
我们允许请求从gateway到payment服务,不允许浏览器访问payment,那么白名单中就要填写网关的来源名称(origin)。
xxxxxxxxxx
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.itbaizhan</groupId>
<artifactId>itbaizhan_cloud2</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>itbaizhan-payment-sentinel</artifactId>
<packaging>jar</packaging>
<name>itbaizhan-payment-sentinel</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- sentinel依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 监控开放端口-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
xxxxxxxxxx
# 启动higress
./startup.sh
添加naocs连接配置。
xxxxxxxxxx
server:
# 端口号
port: 8787
spring:
application:
# 应用名字
name: payment-sentinel
cloud:
nacos:
discovery:
# Nacos注册中心的地址
server-addr: 192.168.47.100:8848
sentinel:
transport:
# sentinel 控制台得地址
dashboard: 192.168.47.100:8480
# 关闭context整合
web-context-unify: false
测试网关服务
发送请求http://localhost:9090/payment/query
Sentinel是通过RequestOriginParser这个接口的parseOrigin来获取请求的来源的。
xxxxxxxxxx
public interface RequestOriginParser {
/**
* 从请求request对象中获取origin,获取方式自定义
*/
String parseOrigin(HttpServletRequest request);
}
注意:
这个方法的作用就是从request对象中,获取请求者的origin值并返回。默认情况下,sentinel不管请求者从哪里来,返回值永远是default,也就是说一切请求的来源都被认为是一样的值default。因此,我们需要自定义这个接口的实现,让不同的请求,返回不同的origin。
xxxxxxxxxx
package cn.itcast.order.sentinel;
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import javax.servlet.http.HttpServletRequest;
@Component
public class HeaderOriginParser implements RequestOriginParser {
@Override
public String parseOrigin(HttpServletRequest request) {
// 1.获取请求头
String origin = request.getHeader("origin");
// 2.非空判断
if (StringUtils.isEmpty(origin)) {
origin = "blank";
}
return origin;
}
}
既然获取请求origin的方式是从reques-header中获取origin值,我们必须让所有从gateway路由到微服务的请求都带上origin头。
头,值为gateway。而从其它地方到达微服务的请求则没有这个头。
接下来,我们添加一个授权规则,放行origin值为gateway的请求。
请求localhost:9090/payment/query
实时效果反馈
1.Sentinel是通过_______这个接口的parseOrigin来获取请求的来源的。
A Origin
B RequestOrigin
C RequestOriginParser
D 以上都是错误
答案
1=>C