这篇文章主要为大家展示了“服务网关spring Cloud Zuul的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“服务网关Spring Cloud&nbs
这篇文章主要为大家展示了“服务网关spring Cloud Zuul的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“服务网关Spring Cloud Zuul的示例分析”这篇文章吧。
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=/apiGatewayeureka.instance.statusPageUrlPath=${management.server.servlet.context-path}/actuator/infoeureka.instance.healthCheckUrlPath=${management.server.servlet.context-path}/actuator/health#通过服务连接配置中心#spring.cloud.config.discovery.enabled=true#spring.cloud.config.discovery.serviceId=config-serverspring.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@SpringCloudApplicationpublic class ApiGatewayMicroApplication { public static void main(String[] args) { SpringApplication.run(ApiGatewayMicroApplication.class, args); }}
以上是“服务网关Spring Cloud Zuul的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网精选频道!
--结束END--
本文标题: 服务网关Spring Cloud Zuul的示例分析
本文链接: https://lsjlt.com/news/324152.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0