1.新建Module
项目名称:cloud-gateway-gateway9527
鼠标点击父工程项目右击 —> New —> Module
具体创建步骤参考【搭建EurekaServer端服务注册中心】
2.添加pom.xml
注意:此处不能添加 spring-boot-starter-web 依赖因为 spring-cloud-starter-gateway 依赖中引入了 spring-boot-starter-webflux 依赖此时就不能引入 spring-boot-starter-web 依赖会有冲突
<dependencies><!--新增gateway--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</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-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>
3.添加application.yml
server:
port: 9527
spring:
application:
name: cloud-gateway
cloud:
gateway:
routes:
- id: payment_routh #路由的ID,没有固定规则但要求唯一,建议配合服务名
uri: http://localhost:8001 #匹配后提供服务的路由地址
predicates:
- Path=/payment/get/** #断言,路径相匹配的进行路由
- id: payment_routh2
uri: http://localhost:8001
predicates:
- Path=/payment/lb/** #断言,路径相匹配的进行路由
eureka:
client:
# 表示是否将自己注册进EurekaServer默认为true
register-with-eureka: true
# 是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
instance:
instance-id: payment8001
#访问路径可以显示IP地址
prefer-ip-address: true
#Eureka客户端向服务端发送心跳的时间间隔,单位为秒(默认是30秒)
#lease-renewal-interval-in-seconds: 1
#Eureka服务端在收到最后一次心跳后等待时间上限,单位为秒(默认是90秒),超时将剔除服务
#lease-expiration-duration-in-seconds: 2
4.添加主启动类
@SpringBootApplication
@EnableEurekaClient
public class GateWay9587Application {
public static void main(String[] args) {
SpringApplication.run(GateWay9587Application.class, args);
}
}
5.启动并测试
启动Eureka注册中心:cloud-eureka-server7001,cloud-eureka-server7002
启动服务提供者:cloud-provider-hystrix-payment8001
启动网关:cloud-gateway-gateway9527
浏览器输入:http://localhost:9527/payment/get/1
返回如下内容:
{"code":200,"message":"查询成功,serverPort: 8001","data":{"id":1,"serial":"哈哈哈"}}
6.配置说明
Gateway网关路由有两种配置方式
6.1.在配置文件yml中配置
spring:
application:
name: cloud-gateway
cloud:
gateway:
routes:
- id: payment_routh #路由的ID,没有固定规则但要求唯一,建议配合服务名
uri: http://localhost:8001 #匹配后提供服务的路由地址
predicates:
- Path=/payment/get/** #断言,路径相匹配的进行路由
- id: payment_routh2
uri: http://localhost:8001
predicates:
- Path=/payment/lb/** #断言,路径相匹配的进行路由
6.2.代码中注入RouteLocator的Bean
@Configuration
public class GateWayConfig {
@Bean
public RouteLocator routes(RouteLocatorBuilder builder) {
return builder.routes()
.route("circuitbreaker_route",r -> r.path("/guonei")
.uri("http://news.baidu.com/guonei"))
.route("circuitbreaker_route1",r -> r.path("/guoji")
.uri("http://news.baidu.com/guoji"))
.build();
}
}
