搭建Gateway,并注册到Eureka
pom
<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.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
application.yml
spring:
application:
name: gateway
bootstrap.yml
server:
port: 8080
eureka:
instance:
# 以IP注册到注册中心
preferIpAddress: ${EUREKA_INSTANCE_PREFER_IP_ADDRESS:true}
# 续约间隔时间,默认30秒
leaseRenewalIntervalInSeconds: ${EUREKA_INSTANCE_LEASE_RENEWAL_INTERVAL_IN_SECONDS:30}
# 实例故障摘除时间,默认90秒,实际是90*2=180秒,配置45就是90秒
leaseExpirationDurationInSeconds: ${EUREKA_INSTANCE_LEASE_EXPIRATION_DURATION_IN_SECONDS:45}
client:
service-url:
defaultZone: http://root:123456@127.0.0.1:8761/eureka
registryFetchIntervalSeconds: 30
启动类
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
启动项目,查看注册中心
通过网关访问demo项目
调整gateway的application.yml
spring:
application:
name: gateway
cloud:
gateway:
routes:
# 路由id,可以任意设置
- id: demo
# lb: 使用负载均衡策略
uri: lb://demo
通过postman访问demo项目的hello接口
package com.learn.demo.api.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author :wm
* @description : 欢迎Controller
*/
@RestController("helloController.v1")
@RequestMapping(value = "/v1")
public class HelloController {
@GetMapping(value = "/hello")
public String sayHello() {
return "hello";
}
}
参考资料
[1].eureka搭建
[2].gateway项目