关于服务网关SpringCloudZuul(Finchley版本)
短信预约 -IT技能 免费直播动态提醒
一、Zuul简介
Zuul作为微服务系统的网关组件,用于构建边界服务(Edge Service),致力于动态路由、过滤、监控、弹性伸缩和安全。其在微服务架构中有着重要的作用,主要体现在以下六个方面:
Zull、Ribbon以及Eureka相结合可以实现智能路由和负载均衡的功能,Zull可以按照某种策略将请求分发到不同的实例上;
网关作为边界服务,将内部服务的API接口进行聚合并统一对外暴露接口。保护内部服务的API接口,防止内部服务被外界调用泄露敏感信息;
网关可以对用户的身份权限进行认证,防止非法请求API接口;
网关可以实现监控功能,实时日志输出,对请求进行记录;
网关可以用来实现流量监控,在高流量的情况下,对服务进行降级;
API接口从内部服务分离出来,便于测试
二、请求路由
使用Spring Cloud Zuul实现路由的规则是十分简单的。路由方式包括两种:传统路由方式,面向服务的路由方式。
2.1 传统路由
下面我们看以下配置:
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);
}
}
六、实战
6.1 pom文件
<?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>
6.2 配置文件
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);
}
}
6.5 启动类
**
* 路由网关服务部署启动类
*
* @author simon
**/
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableZuulProxy
@EnableEurekaClient
@SpringCloudApplication
public class ApiGatewayMicroApplication {
public static void main(String[] args) {
SpringApplication.run(ApiGatewayMicroApplication.class, args);
}
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341