如何在Spring Cloud Netflix Eureka上注册Spring Boot微服务? [英] How to register spring boot microservices on spring cloud Netflix eureka?

查看:113
本文介绍了如何在Spring Cloud Netflix Eureka上注册Spring Boot微服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们计划使用Spring cloud Netflix oss组件.因此,我正在做一个小样本项目. 我开发了2个spring微服务,这些服务在 http://localhost:9000/microsvc-one

We were planning to use spring cloud Netflix oss components. So I was doing a small sample project. I developed 2 spring microservices and those services runs well on http://localhost:9000/microsvc-one http://localhost:9001/microsvc-two

还编写了一个示例春天云etflix eureka maven项目,该项目在 http://localhost:8761

And also wrote a sample spring cloud etflix eureka maven project which runs well on http://localhost:8761

我在两个春季启动微服务主类上都使用了@EurekaDiscoveryClient和@SpringBootApplication注释

I used annotations @EurekaDiscoveryClient and @SpringBootApplication on both the spring boot microservices main class

我使用了@EnableEurekaServer和@SpringBootApplication注释

I used annotation @EnableEurekaServer and @SpringBootApplication

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

Now I am facing a problem in registering those services in eureka server. I referred some samples. I am not understanding those. I did some changes in application.yml files of microsvc-one and microsvc-two and also application.yml file of eureka server. But still it shows empty.

需要进行哪些所有更改,缺少哪些更改或需要进行正确的配置,以便我的服务已在eureka上注册.

What all changes are required or missing or correct configuration to be done so that my services are being registered on eureka.

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

I also have other question like do i need to create a separate project which has @EnableConfigServer and @SpringBootApplication Annotations other than the above 2 microservices and eureka server project module to register the services on eureka. I see those in most of the examples.

如果可以,我们如何在所有这些之间建立链接?

If yes..how do we link between all these?

推荐答案

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

If you are using springBoot application you will need the annotaion @SpringBootApplication thats why that annotation is there on the project you are seeing. @EnableConfigServer is when you are using the spring-cloud config server it is used to externalize the configuration properties but since you have the application.yml inside the project so you donot need that either.

我认为您有一个针对Microservices和Eureka服务器的Spring Boot应用程序.您需要使用

I am thinking you have a spring boot application for both Microservices and the Eureka server. You need to annotate the eureka main class with

@SpringBootApplication
@EnableEurekaServer
@EnableDiscoveryClient

public class EurekaServerApplication {

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

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

Additionally you need annotate you microservice's main class with..

@SpringBootApplication
@EnableDiscoveryClient
public class MicroApplication {

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

由于您没有问题中的application.yml文件,因此您需要什么.

Since you donot have you application.yml file in the question here is what you need.

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

You need the below configuration in application.yml of the microservices.

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

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

In the Eureka Server application.yml file I have this in mine. you might need to tweak it based on what you want.

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 Cloud Netflix Eureka上注册Spring Boot微服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆