1、Eureka注册中心
2、spring cloud gateway网关
Eureka不解释了。
1、启动Eureka注册中心。
2、new module
然后next,选择spring web、eureka discovery client、gateway
然后next,finish。
pom.xml
4.0.0 org.springframework.boot spring-boot-starter-parent 2.5.5 com.jw demo 0.0.1-SNAPSHOT demo Demo project for Spring Boot 1.8 2020.0.4 org.springframework.boot spring-boot-starter-webflux org.springframework.cloud spring-cloud-starter-gateway org.springframework.cloud spring-cloud-starter-netflix-eureka-client io.projectreactor reactor-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
application.yml
server:
port: 5000 # 服务端口
spring:
application:
name: gateway # 服务名称
cloud:
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true
routes:
- id: ums
uri: lb://UMS #eureka注册中心存在的服务名称
predicates:
- Path=/ums/** #路径配置
# filters:
# - StripPrefix=1 #忽略Path配置的个数,此处为1代表访问/api/customer/**时,会将api忽略,真实的访问地址为lb://admin-api/customer/**,如果为2,则为lb://admin-api/**
eureka:
client:
service-url:
defaultZone: http://localhost:8080/eureka # 指定服务注册中心
启动类
@SpringBootApplication
@EnableEurekaClient
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
启动Eureka、gateway、ums(我的子服务的名称)
访问:localhost:5000/ums/role/hello
ums controller
@RestController
@RequestMapping("role")
public class RoleController {
@Value("${server.port}")
String port;
@GetMapping("hello")
public String hello(){
return "hello,port:"+port;
}
}
可以看到已经通过网关成功调用。



