我试图在网关上创建自定义路由,以控制网关能够分发哪些微服务,但它不起作用。
在此过程中,我使用了Registry Eureka、Gateway和微服务Seguranca。
春季启动3.1.5 爪哇17
GateWay 中的自定义路由:
server:
port: 8080
spring:
application:
name: gateway
cloud:
gateway:
routes:
- id: seguranca
uri: lb://seguranca
predicates:
- Path=/api/**
eureka:
client:
serviceUrl:
defaultzone: http://localhost:8761/eureka
微服务保障:
spring:
application:
name: seguranca
控制器
@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
public class AuthenticationController {
private final AuthenticationManager authenticationManager;
private final TokenService tokenService;
@PostMapping("/authenticate")
public ResponseEntity authenticate(@RequestBody @Valid LoginRecord loginRecord) {
var authenticationToken = new UsernamePasswordAuthenticationToken(loginRecord.username(), loginRecord.password());
var authentication = authenticationManager.authenticate(authenticationToken);
var tokenJWT = tokenService.createToken((UserDetailsImpl) authentication.getPrincipal());
return ResponseEntity.ok(new DataTokenJWT(tokenJWT));
}
}
注册表:
eureka:
client:
fetch-registry: false
register-with-eureka: false
instance:
hostname: localhost
server:
port: 8761
spring:
application:
name: service-registry
我添加了发现,它有效,但我意识到自定义旋转不起作用
discovery:
locator:
enabled: true
lower-case-service-id: true
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway-mvc</artifactId> </dependency>
查看 pom.xml 文件中的依赖项,我也遇到了这个问题,直到我将依赖项从上面更改为下面。
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency>