Spring Cloud_Eureka服务注册与发现

梦里梦外; 2024-03-27 09:13 183阅读 0赞

目录

  • 一、Eureka基础知识
    • 1.什么是服务治理
    • 2.什么是服务注册
    • 3.Eureka两组件
  • 二、单机Eureka构建步骤
    • 1.IDEA生成eurekaServer端服务注册中心
    • 2.服务提供者
    • 3.服务消费者

代码链接
https://github.com/lidonglin-bit/cloud

一、Eureka基础知识

1.什么是服务治理

SpringCloud封装了Netflix公司开发的Eureka模块来实现服务治理。
在传统的RPC远程调用框架中,管理每个服务与服务之间依赖关系比较复杂、所以需要进行服务治理,管理服务与服务之间依赖关联,以实现服务调用,负载均衡、容错等,实现服务发现与注册。

2.什么是服务注册

Eureka采用了CS的设计架构,Eureka Server作为服务注册功能的服务器,它是服务注册中心。
而系统中的其他微服务,使用Eureka的客户端连接到Eureka Server并维持心跳连接。这样系统的维护人员可以通过Eureka Server来监控系统中各个微服务是否正常运行。
在服务注册与发现中,有一个注册中心。当服务器启动的时候,会把当前自己服务器的信息,比如:服务通讯地址等以别名方式注册到注册中心上。
另一方(消费者服务),以该别名的方式去注册中心上获取到实际的服务通讯地址,然后,再实现本地RPC远程调用。
RPC远程调用框架核心设计思想:在于注册中心,因为使用注册中心管理每个服务与服务之间的一个依赖关系(服务治理概念)。
在任何RPC远程框架中,都会有一个注册中心(存放服务地址相关信息(接口地址))。
在这里插入图片描述

3.Eureka两组件

  • Eureka Server提供服务注册服务
    各个微服务节点通过配置启动后,会在Eureka Server中进行注册,这样Eureka Server中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观看到。
  • Eureka Client通过注册中心进行访问
    是一个Java客户端,用于简化Eureka Server的交互,客户端同时也具备一个内置的、使用轮询(round-robin)负载算法的负载均衡器。在应用启动后,将会在Eureka Server发送心跳(默认周期30秒)。如果Eureka Server在多个心跳周期内没有收到某个节点的心跳,Eureka Server将会从服务注册表中把这个服务节点移出(默认90秒)

二、单机Eureka构建步骤

整体结构
在这里插入图片描述

父工程pom文件

  1. <!-- 统一管理jar包版本 -->
  2. <properties>
  3. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  4. <maven.compiler.source>1.8</maven.compiler.source>
  5. <maven.compiler.target>1.8</maven.compiler.target>
  6. </properties>
  7. <!-- 子模块继承之后,提供作用:锁定版本+子modlue不用写groupId和version -->
  8. <dependencyManagement>
  9. <dependencies>
  10. <!--spring boot 2.2.2-->
  11. <dependency>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-dependencies</artifactId>
  14. <version>2.2.2.RELEASE</version>
  15. <type>pom</type>
  16. <scope>import</scope>
  17. </dependency>
  18. <!--spring cloud Hoxton.SR1-->
  19. <dependency>
  20. <groupId>org.springframework.cloud</groupId>
  21. <artifactId>spring-cloud-dependencies</artifactId>
  22. <version>Hoxton.SR1</version>
  23. <type>pom</type>
  24. <scope>import</scope>
  25. </dependency>
  26. <!--spring cloud alibaba 2.1.0.RELEASE-->
  27. <dependency>
  28. <groupId>com.alibaba.cloud</groupId>
  29. <artifactId>spring-cloud-alibaba-dependencies</artifactId>
  30. <version>2.1.0.RELEASE</version>
  31. <type>pom</type>
  32. <scope>import</scope>
  33. </dependency>
  34. </dependencies>
  35. </dependencyManagement>
  36. <build>
  37. <plugins>
  38. <plugin>
  39. <groupId>org.springframework.boot</groupId>
  40. <artifactId>spring-boot-maven-plugin</artifactId>
  41. <configuration>
  42. <fork>true</fork>
  43. <addResources>true</addResources>
  44. </configuration>
  45. </plugin>
  46. </plugins>
  47. </build>

1.IDEA生成eurekaServer端服务注册中心

1.建Module:cloud-eureka-server7001
2.改POM

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-web</artifactId>
  9. </dependency>
  10. </dependencies>
  11. </project>
  • 1.X和2.X的对比说明

    1.X版本


    org.springframework.cloud
    spring-cloud-starter-eureka

    2.X版本


    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-server

3.写YML

  1. server:
  2. port: 7001
  3. spring:
  4. application:
  5. name: cloud-eureka-server7001
  6. eureka:
  7. instance:
  8. hostname: localhost
  9. #因为服务端不需要注册,所有为false
  10. client:
  11. register-with-eureka: false
  12. fetchRegistry: false
  13. service-url:
  14. defaultZone: http://localhost:7001/eureka

4.主启动

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

5.测试

  1. http://localhost:7001/

2.服务提供者

EurekaClient端cloud-provider-payment8001将注册进EurekaServer成为服务提供者provider
1.建Module:cloud-provider-payment8001
2.改POM

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-web</artifactId>
  8. </dependency>

3.写YML

  1. spring:
  2. application:
  3. name: cloud-provider-payment8001
  4. server:
  5. port: 8001
  6. eureka:
  7. client:
  8. register-with-eureka: true
  9. fetchRegistry: true
  10. service-url:
  11. defaultZone: http://localhost:7001/eureka

4.主启动

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

5.测试
先启动EurekaServer

  1. http://localhost:7001/

3.服务消费者

EurekaClient端cloud-consumer-order80将注册进EurekaServer成为服务消费者consumer
1.建Module:cloud-consumer-order80
2.改POM

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-web</artifactId>
  8. </dependency>

3.写YML

  1. spring:
  2. application:
  3. name: cloud-consumer-order80
  4. server:
  5. port: 80
  6. eureka:
  7. client:
  8. register-with-eureka: true
  9. fetchRegistry: true
  10. service-url:
  11. defaultZone: http://localhost:7001/eureka

4.主启动

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

5.测试
1)先要启动EurekaServer,7001服务
2)再要启动服务提供者8001服务和服务消费者80服务
3)eureka服务器
在这里插入图片描述
4)测试查询:http://localhost/consumer/payment/get/31
5)测试添加:postman测试添加
6)测试8001服务和80服务效果一样
在这里插入图片描述

发表评论

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

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

相关阅读

    相关 服务注册发现

    在分布式系统中,各个子系统都是多个实例存在,这个时候必须要引入一个服务协调器,用于给调用方提供可用的调用提供者的命名消息。 服务协调器,如zookeeper,etcd,eu