Python 官方文档:入门教程 => 点击学习
目录一、Zuul简介二、请求路由2.1 传统路由2.2 面向服务的路由三、路由规则3.1 默认路由规则3.2 自定义路由规则四、Zuul的过滤器五、设置熔断六、实战6.1 pom文件
Zuul作为微服务系统的网关组件,用于构建边界服务(Edge Service),致力于动态路由、过滤、监控、弹性伸缩和安全。其在微服务架构中有着重要的作用,主要体现在以下六个方面:
Zull、Ribbon以及Eureka相结合可以实现智能路由和负载均衡的功能,Zull可以按照某种策略将请求分发到不同的实例上;
网关作为边界服务,将内部服务的api接口进行聚合并统一对外暴露接口。保护内部服务的API接口,防止内部服务被外界调用泄露敏感信息;
网关可以对用户的身份权限进行认证,防止非法请求API接口;
网关可以实现监控功能,实时日志输出,对请求进行记录;
网关可以用来实现流量监控,在高流量的情况下,对服务进行降级;
API接口从内部服务分离出来,便于测试
使用spring cloud Zuul实现路由的规则是十分简单的。路由方式包括两种:传统路由方式,面向服务的路由方式。
下面我们看以下配置:
zuul.routes.holiday.path=/holiday
@Override
public String getRoute() {
return "*";
}
public ClientHttpResponse fallbackResponse(String route) {
return new ClientHttpResponse() {
@Override
public httpstatus getStatusCode() throws ioException {
return HttpStatus.OK;
}
@Override
public int getRawStatusCode() throws IOException {
return 200;
}
@Override
public String getStatusText() throws IOException {
return "OK";
}
@Override
public void close() {
}
@Override
public InputStream getBody() throws IOException {
return new ByteArrayInputStream((route+" is unavailable.").getBytes());
}
@Override
public HttpHeaders getHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return headers;
}
};
}
@Override
public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
if (cause != null) {
String reason = cause.getMessage();
logger.info("Excption {}",reason);
}
return fallbackResponse(route);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<groupId>com.southGIS.ibase.parent</groupId>
<artifactId>parentWEBService</artifactId>
<version>2.0.1-SNAPSHOT</version>
<relativePath>../../parent/parentWebService/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>api-gateway</artifactId>
<groupId>com.southgis.ibase.systemassistance</groupId>
<version>2.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<description>网关服务</description>
<dependencies>
<!--服务注册与发现-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!--配置中心-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<!--路由网关-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
<!--cas 客户端-->
<dependency>
<groupId>org.jasig.cas.client</groupId>
<artifactId>cas-client-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>apiGateway</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.southgis.ibase.systemassistance.ApiGatewayCustomApplication</mainClass>
</configuration>
<executions>
<execution>
<Goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
bootstrap.properties
#服务名 对应配置文件中的{application}部分
spring.application.name=apiGateway
#对应前配置文件中的{profile}部分
spring.cloud.config.profile=dev2
#配置访问路径
server.servlet.context-path=/eureka-server
#注册中心
eureka.client.serviceUrl.defaultZone=http://localhost:8080/eureka-server/eureka
#为监控端点 /info和/health端点也加上类似的前缀
management.server.servlet.context-path=/apiGateway
eureka.instance.statusPageUrlPath=${management.server.servlet.context-path}/actuator/info
eureka.instance.healthCheckUrlPath=${management.server.servlet.context-path}/actuator/health
#通过服务连接配置中心
#spring.cloud.config.discovery.enabled=true
#spring.cloud.config.discovery.serviceId=config-server
spring.cloud.config.uri = http://localhost:8080/config-server
#配置文件获取失败快速返回
spring.cloud.config.failFast=true
#日志配置
#logging.config=classpath:logback-spring.xml
#logging.path=D:/ibase/logs/holiday
#logging.pattern.console=[%d{yyyy-MM-dd HH:mm:ss}] -- [%-5p]: [%c] -- %m%n
#logging.pattern.file=[%d{yyyy-MM-dd HH:mm:ss}] -- [%-5p]: [%c] -- %m%n
apiGateway-dev2.properties
#访问端口
server.port=8080
#设置session超时时间为540分钟
server.servlet.session.timeout=PT540M
#zuul默认为所有服务开启默认的路由,为了服务安全,此处关闭
zuul.ignored-services=*
#代码字典服务路由
zuul.routes.codedict.path=/codedict
@Override
public String getRoute() {
return "*";
}
public ClientHttpResponse fallbackResponse(String route) {
return new ClientHttpResponse() {
@Override
public HttpStatus getStatusCode() throws IOException {
return HttpStatus.OK;
}
@Override
public int getRawStatusCode() throws IOException {
return 200;
}
@Override
public String getStatusText() throws IOException {
return "OK";
}
@Override
public void close() {
}
@Override
public InputStream getBody() throws IOException {
return new ByteArrayInputStream((route+" is unavailable.").getBytes());
}
@Override
public HttpHeaders getHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_jsON);
return headers;
}
};
}
@Override
public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
if (cause != null) {
String reason = cause.getMessage();
logger.info("Excption {}",reason);
}
return fallbackResponse(route);
}
}
**
* 路由网关服务部署启动类
*
* @author simon
**/
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableZuulProxy
@EnableEurekaClient
@SpringCloudApplication
public class ApiGatewayMicroApplication {
public static void main(String[] args) {
SpringApplication.run(ApiGatewayMicroApplication.class, args);
}
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: 关于服务网关SpringCloudZuul(Finchley版本)
本文链接: https://lsjlt.com/news/141530.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0