注册中心主要操作

  • 服务注册
  • 服务续约
  • 服务剔除
  • 服务下线
  • 服务发现
  • 集群信息同步

一、了解Eureka

1.1 简介

Eureka是Netflix开源的一款提供服务注册和发现的产品,它提供了完整的Service Registry和Service Discovery实现。也是springcloud体系中最重要最核心的组件之一。

详情请看https://github.com/Netflix/eureka/wiki/Eureka-at-a-glance

1.2 作为服务注册中心有哪些实现?

  • Eureka (2.0 版本不再更新)

Eureka是Netflix开源的一款提供服务注册和发现的产品,它提供了完整的Service Registry和Service Discovery实现。也是springcloud体系中最重要最核心的组件之一。

  • spring-cloud-alibaba Nacos

该项目通过自动配置以及其他 Spring 编程模型的习惯用法为 Spring Boot 应用程序在服务注册与发现方面提供和 Nacos 的无缝集成。 通过一些简单的注解,您可以快速来注册一个服务,并使用经过双十一考验的 Nacos 组件来作为大规模分布式系统的服务注册中心。

  • Spring Cloud Consul

Consul是由HashiCorp基于Go语言开发的支持多数据中心分布式高可用的服务发布和注册服务软件, 采用Raft算法保证服务的一致性,且支持健康检查。

  • Spring Cloud Zookeeper

zookeeper它是一个分布式服务框架,是Apache Hadoop 的一个子项目,它主要是用来解决分布式应用中经常遇到的一些数据统一管理问题,简单来说zookeeper=文件系统+监听通知机制。

二、案例实践

2.1 启动一个简单的Eureka Server

这里使用的SpringBoot 的版本为2.1.4.RELEASE ,SpringCloud版本为Greenwich.SR1

  • 创建SpringBoot eureka-server
  • pom.xml ```xml <?xml version=”1.0” encoding=”UTF-8”?> 4.0.0

    1. <groupId>org.springframework.boot</groupId>
    2. <artifactId>spring-boot-starter-parent</artifactId>
    3. <version>2.1.4.RELEASE</version>
    4. <relativePath/> <!-- lookup parent from repository -->

    cn.hdj eureka-server 0.0.1-SNAPSHOT eureke-server

    Demo project for Spring Boot UTF-8 UTF-8 1.8 Greenwich.SR1 org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-eureka-server org.springframework.boot spring-boot-starter-test test

    1. <dependencies>
    2. <dependency>
    3. <groupId>org.springframework.cloud</groupId>
    4. <artifactId>spring-cloud-dependencies</artifactId>
    5. <version>${spring-cloud.version}</version>
    6. <type>pom</type>
    7. <scope>import</scope>
    8. </dependency>
    9. </dependencies>

    org.springframework.boot spring-boot-maven-plugin

  1. - 添加启动代码中添加@EnableEurekaServer注解,注册为服务中心
  2. ```java
  3. @SpringBootApplication
  4. @EnableEurekaServer //标识为EurekaServer
  5. public class EurekaServerApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(EurekaServerApplication.class, args);
  8. }
  9. }
  • 配置文件application.yml(程序启动后,再读取的配置)

    1. server:
    2. port: 8761 #使用默认端口
    3. eureka:
    4. client:
    5. #表示是否将自己注册到Eureka Server,默认为true。
    6. registerWithEureka: false
    7. #表示是否从Eureka Server获取注册信息,默认为true。
    8. fetchRegistry: false
  • bootstrap.yml (程序启动时,需要读取的配置)

    1. spring:
    2. application:
    3. name: eureka-server

    更多的参数选项请查看EurekaInstanceConfigBean and EurekaClientConfigBean

  • 启动程序访问: http://localhost:8761

可以看到下面的页面,其中还没有发现任何服务
image.png

2.2 将服务注册到 Eureka Server

  • 创建SpringBoot项目 euraka-waiter-service
  • pom.xml ```xml <?xml version=”1.0” encoding=”UTF-8”?> 4.0.0

    1. <groupId>org.springframework.boot</groupId>
    2. <artifactId>spring-boot-starter-parent</artifactId>
    3. <version>2.1.4.RELEASE</version>
    4. <relativePath/> <!-- lookup parent from repository -->

    cn.hdj euraka-waiter-service 0.0.1-SNAPSHOT eurakaWaiterService

    euraka-waiter-service 1.8 Greenwich.SR1

    1. <dependency>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-starter-web</artifactId>
    4. </dependency>
    5. <dependency>
    6. <groupId>org.springframework.cloud</groupId>
    7. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    8. </dependency>
    9. <dependency>
    10. <groupId>org.springframework.boot</groupId>
    11. <artifactId>spring-boot-starter-test</artifactId>
    12. <scope>test</scope>
    13. <exclusions>
    14. <exclusion>
    15. <groupId>org.junit.vintage</groupId>
    16. <artifactId>junit-vintage-engine</artifactId>
    17. </exclusion>
    18. </exclusions>
    19. </dependency>

    1. <dependencies>
    2. <dependency>
    3. <groupId>org.springframework.cloud</groupId>
    4. <artifactId>spring-cloud-dependencies</artifactId>
    5. <version>${spring-cloud.version}</version>
    6. <type>pom</type>
    7. <scope>import</scope>
    8. </dependency>
    9. </dependencies>

    org.springframework.boot spring-boot-maven-plugin

  1. - 添加启动代码中添加@EnableDiscoveryClient注解,注册到服务中心
  2. ```java
  3. @EnableDiscoveryClient
  4. @SpringBootApplication
  5. public class EurakaWaiterServiceApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(EurakaWaiterServiceApplication.class, args);
  8. }
  9. }
  • 配置文件application.yml

    1. server:
    2. port: 0 #随机端口
  • bootstrap.yml

    1. spring:
    2. application:
    3. name: waiter-service
  • 启动 eureka-server 项目

  • 再启动 euraka-waiter-service

访问 http://localhost:8761
image.png

2.3 创建集群版Eureka Server

2.2.1 双节点Eureka Server的创建 (eureka-server-two)
  • 创建application-peer1.properties,作为peer1服务中心的配置,并将serviceUrl指向peer2

    1. server:
    2. port: 8001
    3. spring:
    4. application:
    5. name: spring-cloud-eureka
    6. eureka:
    7. instance:
    8. hostname: peer1
    9. client:
    10. serviceUrl:
    11. defaultZone: http://peer2:8002/eureka/
  • 创建application-peer2.properties,作为peer2服务中心的配置,并将serviceUrl指向peer1

    1. server:
    2. port: 8002
    3. spring:
    4. application:
    5. name: spring-cloud-eureka
    6. eureka:
    7. instance:
    8. hostname: peer2
    9. client:
    10. serviceUrl:
    11. defaultZone: http://peer1:8001/eureka/
  • 配置hosts

Window下配置: 自行搜索
Linux 下配置

  1. sudo vim /etc/hosts
  2. #添加以下映射
  3. 127.0.0.1 peer1
  4. 127.0.0.1 peer2
  • 打包启动

IDEA 中配置peer1 和peer2 ,然后分别启动

image.png
都启动后访问: http://peer1:8001/ 就能看到peer2 节点信息
image.png
当我们手动停止peer2 节点后
image.png

也可以使用maven 打为jar包

  1. #打包
  2. mvn clean package -Dmaven.test.skip=true
  3. # 分别以peer1和peeer2 配置信息启动eureka
  4. java -jar ./target/eureke-server-two-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
  5. java -jar ./target/eureke-server-two-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2

到此双节点的配置完成

2.2.2 配置三个节点
  • 修改配置

application-peer1.yml

  1. server:
  2. port: 8001
  3. spring:
  4. application:
  5. name: spring-cloud-eureka
  6. eureka:
  7. instance:
  8. hostname: peer1
  9. client:
  10. serviceUrl:
  11. defaultZone: http://peer2:8002/eureka/,http://peer3:8003/eureka/

application-peer2.yml

  1. server:
  2. port: 8002
  3. spring:
  4. application:
  5. name: spring-cloud-eureka
  6. eureka:
  7. instance:
  8. hostname: peer2
  9. client:
  10. serviceUrl:
  11. defaultZone: http://peer1:8001/eureka/,http://peer3:8002/eureka/

application-peer3.yml

  1. spring:
  2. application:
  3. name: spring-cloud-eureka
  4. profiles: peer3
  5. server:
  6. port: 8003
  7. eureka:
  8. instance:
  9. hostname: peer3
  10. client:
  11. serviceUrl:
  12. defaultZone: http://peer1:8001/eureka/,http://peer2:8002/eureka/

image.png
集群配置完成

项目地址
https://github.com/h-dj/SpringCloud-Learning

参考