如何在Spring Cloud Netflix eureka上注册spring boot微服务?

Laz*_*Guy 1 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服务器中注册这些服务时遇到了问题.我提到了一些样本.我不理解那些.我对microsvc-one和microsvc-two的application.yml文件以及eureka服务器的application.yml文件进行了一些更改.但它仍显示为空.

所有更改都需要或丢失或正确配置,以便我的服务在eureka上注册.

我还有其他问题,比如我需要创建一个单独的项目,其中包含@EnableConfigServer和@SpringBootApplication Annotations,而不是上述2个微服务和eureka服务器项目模块,以便在eureka上注册服务.我在大多数例子中看到了这些.

如果是的话.我们如何在这些之间建立联系?

Gri*_*pal 6

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

我想你有一个微服务器和Eureka服务器的弹簧启动应用程序.你需要用eintka主类注释

@SpringBootApplication
@EnableEurekaServer
@EnableDiscoveryClient

public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

另外,你需要注释你微服务的主类...

@SpringBootApplication
@EnableDiscoveryClient
public class MicroApplication {

    public static void main(String[] args) {
        SpringApplication.run(MicroApplication.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

既然你没有在这里提出application.yml文件就是你需要的.

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

eureka:
  client:
    serviceUrl:
      defaultZone: ${eurekaurl:http://localhost:8761/eureka/} 
Run Code Online (Sandbox Code Playgroud)

在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
Run Code Online (Sandbox Code Playgroud)