创建一个新module,引入gateway依赖:
然后需要引入nacos的discovery和config两个依赖(版本和其他模块对应即可,一般直接引入common模块即可):
<!-- 服务注册/发现--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!-- 配置中心来做配置管理--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency>
然后配置下注册中心地址和配置中心地址:
application.properties:
# 应用名称 spring.application.name=gulimall-gateway spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
创建个bootstrap.properties:
spring.application.name=gulimall-gateway spring.cloud.nacos.config.server-addr=127.0.0.1:8848
然后启动项目:
因为我是引入的common模块,那个模块引入了mybatisplus相关依赖,需要配置数据库信息,解决方法直接在启动类上:
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
再次启动即可成功。
使用示例(创建个application.yml):
spring: cloud: gateway: routes: - id: to_baidu uri: https://www.baidu.com predicates: - Query=to,baidu #如果携带参数to=baidu则匹配成功,跳转至uri - id: to_qq uri: https://www.qq.com predicates: - Query=to,qq
访问即可看到效果:
http://localhost:88/?to=baidu http://localhost:88/?to=qq
也可以用编码方式:
package com.atguigu.springcloude.config; import org.springframework.cloud.gateway.route.RouteLocator; import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @author baikunlong * @date 2020/12/9 9:37 * 配置路由的第二种方式:采用编码 */ @Configuration public class GatewayConfig { @Bean public RouteLocator myRouteLocator(RouteLocatorBuilder builder){ RouteLocatorBuilder.Builder routes = builder.routes(); // 如果路径包含/guonei则跳转至uri routes.route("baidu",r->r.path("/guonei").uri("http://news.baidu.com/guonei")); return routes.build(); } @Bean public RouteLocator myRouteLocator2(RouteLocatorBuilder builder){ RouteLocatorBuilder.Builder routes = builder.routes(); routes.route("baidu2",r->r.path("/guoji").uri("http://news.baidu.com/guoji")); return routes.build(); } }
可以自定义过滤器:
package com.atguigu.springcloude.filter; import lombok.extern.slf4j.Slf4j; import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.core.Ordered; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Mono; import java.time.LocalDateTime; /** * @author baikunlong * @date 2020/12/9 11:07 */ @Component @Slf4j public class MyLogGatewayFilter implements GlobalFilter, Ordered { @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { log.info("进入我的过滤器:"+ LocalDateTime.now()); if(exchange.getRequest().getQueryParams().getFirst("uname")==null){ log.info("用户名为空,禁止访问"); exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE); return exchange.getResponse().setComplete(); } return chain.filter(exchange); } @Override public int getOrder() { //过滤器的顺序,越小等级越高 return 0; } }
其他示例:
spring: application: name: cloud-gateway cloud: gateway: discovery: locator: enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由 # 配置路由的第一种方式:采用yml配置(这种简单) routes: - id: payment_routh #payment_route #路由的ID,没有固定规则但要求唯一,建议配合服务名 # uri: http://localhost:8001 #匹配后提供服务的路由地址 uri: lb://cloud-payment-service #匹配后提供服务的路由地址 predicates: - Path=/payment/get/** # 断言,路径相匹配的进行路由 - id: payment_routh2 #payment_route #路由的ID,没有固定规则但要求唯一,建议配合服务名 # uri: http://localhost:8001 #匹配后提供服务的路由地址 uri: lb://cloud-payment-service #匹配后提供服务的路由地址 # 断言的所有规则些在该项目启动时会打印 predicates: - Path=/payment/lb/** # 断言,路径相匹配的进行路由 # - After=2020-12-09T10:51:37.485+08:00[Asia/Shanghai] #在多久之后才能访问系统 # Between就可以做秒杀活动,只有该时间段内才能访问系统,否则都是404 # - Between=2020-12-09T10:51:37.485+08:00[Asia/Shanghai],2020-12-09T11:51:37.485+08:00[Asia/Shanghai] # - Cookie=username,zzyy #必须携带username=zzyy的cookie,像这样:curl http://localhost:9527/payment/lb --cookie "username=zzyy" # - Header=X-Request-Id, \d+ # 请求头要有X-Request-Id属性并且值为整数的正则表达式,像这样curl http://localhost:9527/payment/lb -H "X-Request-Id:111" # 还有一些有用到再看