Gateway2案例
1.新建moudle:zds-egg-gateway
2.pom 不要引入spring-boot-starter-web,gateway使用的是webflux
<dependencies><!--gateway--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency><!-- nacos --><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></dependencies>
3.yml 新增网关配置
nacos
#devspring:application:name: zds-egg-gatewayprofiles:active: devmain:allow-bean-definition-overriding: truecloud:nacos:config:server-addr: 192.168.34.126:8848file-extension: yamlnamespace: 5fa02c50-78b7-4c9d-a43c-8883874c7c56group: DEFAULT_GROUPdiscovery:server-addr: 192.168.34.126:8848file-extension: yamlnamespace: 5fa02c50-78b7-4c9d-a43c-8883874c7c56group: DEFAULT_GROUP
server:
port: 9527
spring:
application:
name: zds-egg-gateway
cloud:
nacos:
discovery:
server-addr: 192.168.34.126:8848
#############################新增网关配置###########################
gateway:
routes:
- id: biz_routh1 #路由的ID,没有固定规则但要求唯一,建议配合服务名
uri: http://localhost:8082 #匹配后提供服务的路由地址
predicates:
- Path=/timeout/** # 断言,路径相匹配的进行路由
- id: biz_routh2 #路由的ID,没有固定规则但要求唯一,建议配合服务名
uri: http://localhost:8082 #匹配后提供服务的路由地址
predicates:
- Path=/selectStuById # 断言,路径相匹配的进行路由 http://localhost:8082/selectStuById?id=1
4.启动类
@SpringBootApplication
@EnableDiscoveryClient
public class Gateway9527 {
public static void main(String[] args) {
SpringApplication.run(Gateway9527.class, args);
}
}
5.测试
原地址
http://localhost:8082/timeout/10
http://localhost:8082/selectStuById?id=1
新地址
http://localhost:9527/timeout/10
http://localhost:9527/selectStuById?id=1
两者返回结果一致
Gateway配置路由的两种方式
- 在配置文件yml中配置,见上
- 代码中注入RouteLocator的Bean
案例:百度国内新闻网址,需要外网 - http://news.baidu.com/guonei
自己写一个:业务需求 - 通过9527网关访问到外网的百度新闻网址
@Configuration
public class GatewayConfig {
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder){
RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
routes.route("path_route_biz3",
r-> r.path("/guonei")
.uri("http://news.baidu.com/guonei")).build();
return routes.build();
}
}
测试
浏览器输入http://localhost:9527/guonei,返回http://news.baidu.com/guonei相同的页面。
GateWay配置动态路由
默认情况下Gateway会根据注册中心注册的服务列表,以注册中心上微服务名为路径创建动态路由进行转发,从而实现动态路由的功能(不写死一个地址)。
启动
zds-egg-gateway 9527
zds-egg-biz 8082/8085
YML
需要注意的是uri的协议为lb,表示启用Gateway的负载均衡功能。
lb://serviceName是spring cloud gateway在微服务中自动为我们创建的负载均衡uri。
server:
port: 9527
spring:
application:
name: zds-egg-gateway
cloud:
nacos:
discovery:
server-addr: 192.168.34.126:8848
#############################新增网关配置###########################
gateway:
discovery:
locator:
enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由
routes:
- id: biz_routh1 #路由的ID,没有固定规则但要求唯一,建议配合服务名
# uri: http://localhost:8082 #匹配后提供服务的路由地址
uri: lb://zds-egg-biz
predicates:
- Path=/timeout/** # 断言,路径相匹配的进行路由
- id: biz_routh2 #路由的ID,没有固定规则但要求唯一,建议配合服务名
# uri: http://localhost:8082 #匹配后提供服务的路由地址
uri: lb://zds-egg-biz
predicates:
- Path=/selectStuById # 断言,路径相匹配的进行路由 http://localhost:8082/selectStuById?id=1
测试
浏览器输入 - http://localhost:9527/timeout/10
结果
不停刷新页面,8082/8085两个端口切换。
