1.gateway-server模块
1.1.pom.xml文件
4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.12.RELEASE com.it gateway-server 0.0.1-SNAPSHOT gateway-server Demo project for Spring Boot 1.8 Hoxton.SR12 org.springframework.cloud spring-cloud-starter-gateway org.springframework.cloud spring-cloud-netflix-eureka-client org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
1.2.application.yml文件

server:port: 80
spring:application:name: gateway-servercloud:gateway:enabled: truediscovery:locator:enabled: true #开启动态路由 开启通过应用名称找到服务的功能lower-case-service-id: true #开启服务名称小写
eureka:client:service-url:defaultZone: http://192.168.174.133:8761/eurekaregistry-fetch-interval-seconds: 3instance:hostname: localhostinstance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}
1.3主函数类
package com.it;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication
@EnableEurekaClient
public class GatewayServerApplication {public static void main(String[] args) {SpringApplication.run(GatewayServerApplication.class, args);}}
2.login-service模块
2.1.pom.xml文件
4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.12.RELEASE com.it login-service 0.0.1-SNAPSHOT login-service Demo project for Spring Boot 1.8 Hoxton.SR12 org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-netflix-eureka-client org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
2.2.application.yml文件
server:port: 8081
spring:application:name: login-service
eureka:client:service-url:defaultZone: http://192.168.174.133:8761/eurekaregistry-fetch-interval-seconds: 3instance:hostname: localhostinstance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}
2.3.LoginController文件
package com.it.controller;import org.springframework.web.bind.annotation.GetMapping;import java.util.UUID;public class LoginController {@GetMapping("doLogin")public String doLogin(String name,String pwd){System.out.println(name);System.out.println(pwd);String s = UUID.randomUUID().toString();return s;}}
2.4主函数类
package com.it;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication
@EnableEurekaClient
public class LoginServiceApplication {public static void main(String[] args) {SpringApplication.run(LoginServiceApplication.class, args);}}
3.功能测试
启动动态路由后,访问路径时需要在localhost后面加上,要访问服务的名称,后面再跟上这个服务中的方法接口名

如果不加服务名称,还是按照以前的方法写,会导致访问报404