如何使用动态路由让 zuul 和 eureka 协同工作?

时间:2021-03-20 07:10:30

标签: spring netflix-eureka netflix-zuul jave

我正在尝试让 zuul 和 eureka 一起工作,但是当我尝试通过微服务名称将微服务的绝对(物理)路径更改为相对路径时,我被卡住了。

我有下面显示的代码。

祖尔

应用程序.yml

server:
  port: 8080
management:
  endpoints:
    web:
      exposure:
        include: '*'
spring:
  application:
    name: gateway
zuul:
  prefix: /api
  routes:
    lesson-service:
      path: /lesson-service/**
      serviceId: lesson-service
eureka:
  instance:
    preferIpAddress: true
  client:
    registerWithEureka: true
    fetchRegistry: true
    serviceUrl:
      defaultZone: ${EUREKA_URI:http://localhost:8761/eureka}

主要

@SpringBootApplication
@EnableZuulProxy
@EnableDiscoveryClient
public class TeachmeGatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(TeachmeGatewayApplication.class, args);
    }
}

尤里卡

应用程序.yml

server:
  port: 8761
management:
  endpoints:
    web:
      exposure:
        include: '*'
eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false
    server:
      waitTimeInMsWhenSyncEmpty: 0

主要

@SpringBootApplication
@EnableEurekaServer
public class TechmeEurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(TechmeEurekaApplication.class, args);
    }
}

课程服务

应用程序.yml

server:
  port: 8081
spring:
  application:
    name: lesson-service

主要

@SpringBootApplication
@EnableDiscoveryClient
public class TechmeLessonApplication {

    public static void main(String[] args) {
        SpringApplication.run(TechmeLessonApplication.class, args);
    }

}

控制器

@RestController
public class LessonController {

    @GetMapping("/info")
    public String getInfo() {
        return "All is ok!";
    }
}

当我尝试从 http://localhost:8080/api/lesson-service/info 网址获得响应时,我收到了 500 http 错误。

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sat Mar 20 12:38:40 OMST 2021
There was an unexpected error (type=Internal Server Error, status=500).

0 个答案:

没有答案