springcloud服务注册中心Eureka

£神魔★判官ぃ 2022-01-26 15:49 580阅读 0赞

启动一个服务注册中心,只需要一个注解@EnableEurekaServer

  1. package com.dalingjia.eurekaserver;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  5. /**
  6. * 启动一个服务注册中心,只需要一个注解@EnableEurekaServer
  7. */
  8. @EnableEurekaServer
  9. @SpringBootApplication
  10. public class EurekaServerApplication {
  11. public static void main(String[] args) {
  12. SpringApplication.run(EurekaServerApplication.class, args);
  13. }
  14. }

配置文件如下,表明自己是一个服务注册中心:

  1. server:
  2. port: 8761
  3. eureka:
  4. instance:
  5. hostname: localhost
  6. client:
  7. registerWithEureka: false
  8. fetchRegistry: false
  9. serviceUrl:
  10. defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

Eureka客服端,通过注解@EnableEurekaClient表明自己是一个eurekaClient:

  1. /**
  2. * 通过注解@EnableEurekaClient 表明自己是一个eurekaclient.
  3. */
  4. @SpringBootApplication
  5. @EnableEurekaClient
  6. @RestController
  7. @EnableHystrix
  8. @EnableHystrixDashboard
  9. public class ServiceHiApplication {
  10. public static void main(String[] args) {
  11. SpringApplication.run(ServiceHiApplication.class, args);
  12. }
  13. }

客户端配置:

  1. eureka:
  2. client:
  3. serviceUrl:
  4. defaultZone: http://localhost:8761/eureka/
  5. server:
  6. port: 8762
  7. spring:
  8. application:
  9. name: service-hi

启动服务端和客户端,访问localhost:8761,可以看到注册到eureka的当前实例:
在这里插入图片描述

@EnableDiscoveryClient与@EnableEurekaClient区别

简而言之 spring cloud 中服务发现有多种实现(eureka、consul、zookeeper等等),@EnableDiscoveryClient 基于 spring-cloud-commons, @EnableEurekaClient 基于 spring-cloud-netflix。

一句话:如果选用eureka做注册中心,那么推荐@EnableEurekaClient;如果不是eureka,那么推荐使用@EnableDiscoveryClient。

@EnableEurekaClient源码如下:

  1. package org.springframework.cloud.netflix.eureka;
  2. import java.lang.annotation.Documented;
  3. import java.lang.annotation.ElementType;
  4. import java.lang.annotation.Inherited;
  5. import java.lang.annotation.Retention;
  6. import java.lang.annotation.RetentionPolicy;
  7. import java.lang.annotation.Target;
  8. @Target({ElementType.TYPE})
  9. @Retention(RetentionPolicy.RUNTIME)
  10. @Documented
  11. @Inherited
  12. public @interface EnableEurekaClient {
  13. }

@EnableDiscoveryClient源码如下:

  1. package org.springframework.cloud.client.discovery;
  2. import java.lang.annotation.Documented;
  3. import java.lang.annotation.ElementType;
  4. import java.lang.annotation.Inherited;
  5. import java.lang.annotation.Retention;
  6. import java.lang.annotation.RetentionPolicy;
  7. import java.lang.annotation.Target;
  8. import org.springframework.context.annotation.Import;
  9. @Target({ElementType.TYPE})
  10. @Retention(RetentionPolicy.RUNTIME)
  11. @Documented
  12. @Inherited
  13. @Import({EnableDiscoveryClientImportSelector.class})
  14. public @interface EnableDiscoveryClient {
  15. boolean autoRegister() default true;
  16. }

发表评论

表情:
评论列表 (有 0 条评论,580人围观)

还没有评论,来说两句吧...

相关阅读