spring-boot - 如何在spring cloud Netflix eureka上注册spring boot微服务?

标签 spring-boot microservices netflix-eureka spring-cloud-netflix

我们计划使用 spring cloud Netflix oss 组件。所以我在做一个小样本项目。 我开发了 2 个 spring 微服务,这些服务运行良好 http://localhost:9000/microsvc-one http://localhost:9001/microsvc-two

并且还写了一个示例 spring cloud etflix eureka maven 项目,运行良好 http://localhost:8761

我在两个 spring boot 微服务主类上都使用了注解 @EurekaDiscoveryClient 和 @SpringBootApplication

我使用了注解@EnableEurekaServer 和@SpringBootApplication

现在我在 eureka 服务器中注册这些服务时遇到了问题。我引用了一些 sample 。我不明白那些。 我对 microsvc-one 和 microsvc-two 的 application.yml 文件以及 eureka 服务器的 application.yml 文件做了一些更改。 但它仍然显示为空。

为了在 eureka 上注册我的服务,需要或缺少或正确配置的所有更改是什么。

我还有其他问题,比如除了上述 2 个微服务和 eureka 服务器项目模块之外,我是否需要创建一个具有 @EnableConfigServer 和 @SpringBootApplication 注释的单独项目来在 eureka 上注册服务。 我在大多数示例中都看到了这些。

如果是..我们如何将所有这些联系起来?

最佳答案

如果您使用的是 springBoot 应用程序,您将需要注解 @SpringBootApplication 这就是为什么该注解出现在您所看到的项目中的原因。 @EnableConfigServer 是当您使用 spring-cloud 配置服务器时,它用于外部化配置属性,但由于您在项目中有 application.yml,所以您也不需要它。

我认为您有一个用于微服务和 Eureka 服务器的 spring boot 应用程序。你需要用

注释 Eureka 主类
@SpringBootApplication
@EnableEurekaServer
@EnableDiscoveryClient

public class EurekaServerApplication {

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

此外,您需要使用...注释微服务的主类

@SpringBootApplication
@EnableDiscoveryClient
public class MicroApplication {

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

由于您没有 application.yml 文件,所以这里的问题就是您需要的。

您需要在微服务的application.yml中进行如下配置。

eureka:
  client:
    serviceUrl:
      defaultZone: ${eurekaurl:http://localhost:8761/eureka/} 

在我的 Eureka Server application.yml 文件中有这个。您可能需要根据需要对其进行调整。

info:
  component: Registry Server

server: 
  port: ${port:8761}


eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false
  server:
    enable-self-preservation: false
    waitTimeInMsWhenSyncEmpty: 0
  instance:
    hostname: localhost
    lease-expiration-duration-in-seconds: 15
    lease-renewal-interval-in-seconds: 5

关于spring-boot - 如何在spring cloud Netflix eureka上注册spring boot微服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42311050/

相关文章:

spring-boot - Feign/Eureka 客户端不传递 X-Forwarded-For header

spring - 在请求头验证错误的情况下,让 Spring 响应 400(而不是 500)

kubernetes - 应用程序之间的 ArgoCD 同步波

architecture - 微服务中的 grpc 组织

java - Spring Cloud Streams 功能的条件路由

java - Spring Eureka 服务器中的 datacenter 属性是什么意思?

java - Spring Boot 在 web.xml 中启用 <async-supported>

spring-boot - “Application Failed to Start”

java - Spring Boot/Spring LDAP 获取用户的成员列表

spring-boot - Grails 2.4作为Spring Cloud微服务的应用