Spring Cloud Eureka(二):Eureka 注册中心体验

我会带着你远行 2022-05-10 09:30 255阅读 0赞

1、Eureka 简述

本文主要从应用角度体验一下注册中心的搭建和使用,后文会由浅入深学习Spring Cloud Eureka 的各种原理和机制。

Spring Cloud Eureka 是 Spring Cloud Netflix 组件的一部分,而 Spring Cloud Netflix 又是基于 netflix 的 eureka 封装,所以最终的源码实现在于 eureka 中。废话不多说,先上手体验一下,后文在一步步深入了解。

2、Eureka Server 服务端搭建

1、创建项目,添加 maven 依赖

  1. <modelVersion>4.0.0</modelVersion>
  2. <artifactId>lkf-cloud-eureka</artifactId>
  3. <version>0.0.1-SNAPSHOT</version>
  4. <packaging>jar</packaging>
  5. <name>lkf-cloud-eureka</name>
  6. <parent>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-parent</artifactId>
  9. <version>2.0.4.RELEASE</version>
  10. <relativePath/> <!-- lookup parent from repository -->
  11. </parent>
  12. <properties>
  13. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  14. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  15. <java.version>1.8</java.version>
  16. <spring-cloud.version>Finchley.SR1</spring-cloud.version>
  17. </properties>
  18. <dependencies>
  19. <dependency>
  20. <groupId>org.springframework.cloud</groupId>
  21. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.springframework.cloud</groupId>
  25. <artifactId>spring-cloud-config-server</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-starter-test</artifactId>
  30. <scope>test</scope>
  31. </dependency>
  32. </dependencies>
  33. <dependencyManagement>
  34. <dependencies>
  35. <dependency>
  36. <groupId>org.springframework.cloud</groupId>
  37. <artifactId>spring-cloud-dependencies</artifactId>
  38. <version>${spring-cloud.version}</version>
  39. <type>pom</type>
  40. <scope>import</scope>
  41. </dependency>
  42. </dependencies>
  43. </dependencyManagement>

2、创建启动类,启用注册中心服务 @EnableEurekaServer

  1. @SpringBootApplication
  2. @EnableEurekaServer
  3. public class EurekaApplication {
  4. public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args);
  5. }
  6. }

3、添加配置文件 appication

  1. server.port=8888
  2. eureka.instance.hostname=localhost
  3. #由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己
  4. eureka.client.register-with-eureka=false
  5. #由于注册中心的职责就是维护服务实例,它并不需要去检索服务,所以也设置为false
  6. eureka.client.fetch-registry=false
  7. eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
  8. eureka.server.wait-time-in-ms-when-sync-empty=0
  9. #eureka.instance.prefer-ip-address=true
  10. #eureka.instance.ip-address=127.0.0.1
  11. spring.application.name=lkf-cloud-eureka
  12. spring.cloud.config.uri=${CONFIG_SERVER_URL:http://localhost:8888}

4、启动 Eureka Server 服务

启动应用,访问http://localhost:8888/,可以看到 Eureka Server 自带的 UI 管理界面。

在这里插入图片描述

3、Eureka Client 客户端搭建

1、创建项目 lkf-eureka-client,添加maven 依赖

  1. <modelVersion>4.0.0</modelVersion>
  2. <artifactId>lkf-eureka-client</artifactId>
  3. <version>0.0.1-SNAPSHOT</version>
  4. <packaging>jar</packaging>
  5. <name>lkf-eureka-client</name>
  6. <parent>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-parent</artifactId>
  9. <version>2.0.4.RELEASE</version>
  10. <relativePath/> <!-- lookup parent from repository -->
  11. </parent>
  12. <properties>
  13. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  14. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  15. <java.version>1.8</java.version>
  16. <spring-cloud.version>Finchley.SR1</spring-cloud.version>
  17. </properties>
  18. <dependencies>
  19. <dependency>
  20. <groupId>org.springframework.cloud</groupId>
  21. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-test</artifactId>
  26. <scope>test</scope>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-web</artifactId>
  31. </dependency>
  32. </dependencies>
  33. <dependencyManagement>
  34. <dependencies>
  35. <dependency>
  36. <groupId>org.springframework.cloud</groupId>
  37. <artifactId>spring-cloud-dependencies</artifactId>
  38. <version>${spring-cloud.version}</version>
  39. <type>pom</type>
  40. <scope>import</scope>
  41. </dependency>
  42. </dependencies>
  43. </dependencyManagement>
  44. <build>
  45. <plugins>
  46. <plugin>
  47. <groupId>org.springframework.boot</groupId>
  48. <artifactId>spring-boot-maven-plugin</artifactId>
  49. </plugin>
  50. </plugins>
  51. </build>

这里如果没有引用 spring-boot-starter-web 会报以下错误:

  1. Destroy method on bean with name 'scopedTarget.eurekaClient' threw an exception: org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'eurekaInstanceConfigBean': Singleton bean creation not allowed while singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)

2、创建启动类,启用 @EnableDiscoveryClient 或 @EnableEurekaClient

  1. /**
  2. * @author kaifeng
  3. */
  4. @SpringBootApplication
  5. @EnableDiscoveryClient
  6. public class EurekaClientApplication {
  7. public static void main(String[] args) {
  8. SpringApplication.run(EurekaClientApplication.class, args);
  9. }
  10. }

3、添加配置文件

  1. server.port=8001
  2. spring.application.name=lkf-eureka-client
  3. eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/

4、启动服务,访问注册中心

在这里插入图片描述

发表评论

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

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

相关阅读