首页 / 微服务 / 39、微服务GateWay 新一代网关
39、微服务GateWay 新一代网关
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了39、微服务GateWay 新一代网关,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3249字,纯文字阅读大概需要5分钟。
内容图文
1、SpringCloud gateway三个重要的概念:路由,断言 、过滤
2、创建cloud-gateway-gateway9527 项目编写pom.xml添加依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> <dependency> <groupId>com.atguigu.springcloud</groupId> <artifactId>cloud-api-commons</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies>
编写application.yml配置文件
server: port: 9527 spring: application: name: cloud-gateway cloud: gateway: routes: - id: payment-routh01 uri: http://localhost:8001 predicates: - Path=/payment/get/** - id: payment-routh02 uri: http://localhost:8001 predicates: - Path=/payment/lb/** eureka: instance: hostname: cloud-gateway-service client: register-with-eureka: true fetch-registry: true service-url: # defaultZone: http://localhost:7001/eureka defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
编写主启动类
@SpringBootApplication @EnableEurekaClient public class GateWayApplication9527 { public static void main(String[] args) { SpringApplication.run(GateWayApplication9527. class ,args); } }
测试cloud-provider-payment8001 服务提供者自测:http://localhost:8001/payment/get/1409365580926001153
测试网关gateway服务地址转发到服务提供者:http://localhost:9527/payment/get/1409365580926001153
2、application.yml配置动态路由
server: port: 9527 spring: application: name: cloud-gateway cloud: gateway: discovery: locator: enabled: true #开启动态路由 routes: - id: payment-routh01 uri: lb://cloud-payment-service predicates: - Path=/payment/get/** - id: payment-routh02 uri: lb://cloud-payment-service predicates: - Path=/payment/lb/** eureka: instance: hostname: cloud-gateway-service client: register-with-eureka: true fetch-registry: true service-url: # defaultZone: http://localhost:7001/eureka defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
3、GateWay 的 Filter 自定义过滤器
@Component @Slf4j public class MyLogGatewayFilter implements GlobalFilter, Ordered { @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { String username = exchange.getRequest().getQueryParams().getFirst("username"); if(username == null) { log.info("*******************非法的请求********************"); exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE); return exchange.getResponse().setComplete(); } return chain.filter(exchange); } @Override publicint getOrder() { return 0; } }
测试地址:http://localhost:9527/payment/lb?username=123
原文:https://www.cnblogs.com/shunWcs/p/14958293.html
内容总结
以上是互联网集市为您收集整理的39、微服务GateWay 新一代网关全部内容,希望文章能够帮你解决39、微服务GateWay 新一代网关所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。