繁体   English   中英

如何在Spring Cloud Netflix Eureka上注册Spring Boot微服务?

[英]How to register spring boot microservices on spring cloud Netflix eureka?

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

并且还编写了一个示例春天云etflix eureka maven项目,该项目在http:// localhost:8761上运行良好

我在两个Spring Boot Microservices主类上都使用了注解@EurekaDiscoveryClient和@SpringBootApplication

我使用了注释@EnableEurekaServer和@SpringBootApplication

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

需要进行所有更改,缺少什么或进行正确的配置,以便在eureka上注册我的服务。

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

如果是,我们如何将所有这些链接起来?

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

我认为您有一个针对Microservices和Eureka服务器的spring boot应用程序。 您需要使用以下方法注释尤里卡主类

@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

假设您现在有一个名为“ LoginServer”的微服务,让我们看看如何在启动时向发现服务器(Eureka Server)注册该服务。

这里是LoginServer.java的Spring Boot启动类:

@EnableAutoConfiguration
@EnableDiscoveryClient
public class LoginServer {
public static void main(String[] args) {

     // Will configure using login-server.yml

     System.setProperty("spring.config.name", "login-server");
     SpringApplication.run(LoginServer.class, args);

  }
}

@EnableDiscoveryClient-启用服务注册和发现。 在这种情况下,此过程使用其应用程序名称(在YML配置文件中配置)向发现服务器服务注册自己。

让我们看看完整的设置:

首先,在src / main / resources包文件夹中创建一个login-server.yml文件(扩展名应为.yml)。 并编写这些配置并保存。

# Spring properties
spring:
 application:
   name: login-server # This name used as ID so ("spring.config.name", 
                      #"login-server"); must be same.

# Discovery Server Access
eureka:
 client:
  serviceUrl:
   defaultZone: http://localhost:1111/eureka/

# HTTP Server
server:
 port: 2222   # HTTP (Tomcat) port

运行LoginServer并使其完成初始化。 通过将此URL http:// localhost:1111放入您喜欢的浏览器中以打开仪表板并刷新。 几秒钟后,您应该会看到LOGIN-SERVER。 通常,注册最多需要30秒(默认情况下),因此请等待或重新启动。

这是微服务完整的注册过程。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM