欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 时评 > Spring Cloud Alibaba之网关组件Gateway

Spring Cloud Alibaba之网关组件Gateway

2024/10/25 23:22:48 来源:https://blog.csdn.net/ysy1119/article/details/139998336  浏览:    关键词:Spring Cloud Alibaba之网关组件Gateway

实例演示1:在微服务体系中引入GateWay组件

  • 创建一个ServiceForGateway的SpringBoot项目,通过在控制类编写方法对外提供服务
@RestController
public class Controller {@RequestMapping("/getAccount/{id}")public String getAccount(@PathVariable String id){return "Account Info, id is:"+id;}
}
  • 创建GatewayDemo项目,添加依赖到pom文件
 <dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency></dependencies>

ps:本次实验演练仅用到Gateway组件,所以在pom文件里面,就不需要通过dependencyManagement引入spring-cloud-alibaba-dependencies,在引入了gateway的依赖后也不需要再引入spring-boot-starter-web了。

  • 配置application.yml文件相关参数,实现简单路由转发
server:port: 8080
spring:cloud:gateway:routes:# 路由id,可随便命名,但要确保唯一- id: account_route# 匹配后的地址uri: http://localhost:8090/getAccount/{id}#断言predicates:# 包含/getAccount即需转发- Path=/getAccount/{id}
  • 修改application.yml文件,实现网关过滤
server:port: 8080
spring:cloud:gateway:routes:- id: StripPrefix_route# 匹配后的地址uri: http://localhost:8090predicates:- Path=/needRemoved/**filters:- StripPrefix=1- id: PrefixPath_route# 匹配后的地址uri: http://localhost:8090predicates:- Method=GETfilters:- PrefixPath=/getAccount
  • 自定义全局过滤器,设置过滤器动作:
//自定义的全局性过滤器
public class MyGlobalFilter implements GlobalFilter, Ordered {//处理请求@Overridepublic Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {String urlPath = exchange.getRequest().getURI().getPath();System.out.println(urlPath);if(urlPath.indexOf("hacker") == -1){return chain.filter(exchange);}else{ServerHttpResponse res = exchange.getResponse();String msg = "fail for hacker";byte[] bits = msg.getBytes();DataBuffer buf = res.bufferFactory().wrap(bits);res.setStatusCode(HttpStatus.BAD_REQUEST);res.getHeaders().add("Content-Type", "text/plain");return res.writeWith(Mono.just(buf));}}@Overridepublic int getOrder() {//Order值越小,优先级越高return 0;}
}
  • 编写代码配置过滤器:
@Configuration
public class ConfigMyGlobalFilter {@Beanpublic GlobalFilter configFilter() {return new MyGlobalFilter();}
}

实例演示2:GateWay整合Nacos,实现负载均衡

  • 创建 GateWithNacos项目,添加pom依赖,引入nacos和GateWay依赖包
<dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency>
</dependencies>
  • 编写启动类,添加注解,说明项目与nacos组件交互
@EnableDiscoveryClient
@SpringBootApplication
public class SpringBootApp {public static void main(String[] args) {SpringApplication.run(SpringBootApp.class, args);}}
  • 配置application.yml文件,实现GateWay整合Nacos效果
server:port: 8080
spring:cloud:nacos:discovery:server-addr: 127.0.0.1:8848gateway:routes:- id: loadbalance_routeuri: lb://ServiceProvider/predicates:- Path=/callServiceByRibbon

实例演示3:通过GateWay实现灰度发布

  • 创建GrayRelease项目,添加pom依赖,启动类同上一致
    <dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency></dependencies>
  • 配置application.yml文件,实现灰度发布效果
server:port: 8080
spring:cloud:gateway:routes:- id: oldVersion_Routeuri: http://localhost:3333/getAccount/{id}predicates:- Path=/getAccount/{id}- Weight=accountGroup, 9- id: newVersion_Routeuri: http://localhost:5555/getAccount/{id}predicates:- Path=/getAccount/{id}- Weight=accountGroup, 1

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com