Spring Cloud Eureka 入门之服务注册中心详解 ...

企业动态
Eureka,这里是 Spring Cloud Eureka 的简称,是 Spring Cloud Netflix 组件之一。Spring Cloud Netflix 中核心的组件包括了服务治理(Eureka),服务容断(Hystrix),路由(Zuul)和客户端负载均衡(Ribbon)。在系列第三篇,服务消费者讲解会涉及到 Ribbon 的使用。

 [[195583]]

本文提纲

1. Eureka 服务治理

1.1 什么是 Eureka

1.2 Eureka 集群架构

2. 运行 Eureka 工程 springcloud-eureka-server

3. 详解 Eureka 工程 springcloud-eureka-server

一、Eureka 服务治理

1.1 什么是 Eureka

Eureka,这里是 Spring Cloud Eureka 的简称,是 Spring Cloud Netflix 组件之一。Spring Cloud Netflix 中核心的组件包括了服务治理(Eureka),服务容断(Hystrix),路由(Zuul)和客户端负载均衡(Ribbon)。在系列第三篇,服务消费者讲解会涉及到 Ribbon 的使用。

回到 Spring Cloud Eureka,是基于 Netflix Eureka (Netflix 是 Java 实现的开源软件)。服务治理(Eureka)包括服务注册、服务发现和服务检测监控等,自然本文介绍下 Eureka 作为服务注册中心。

1.2 Eureka 架构

Eureka 作为服务治理,必然满足下面几点:

  • 服务本身不存在单点故障,
  • 支持集群,即高可用性
  • 服务与服务之间通过服务注册中心找到彼此实例

作为服务端(即服务注册中心),包括

  • 管理服务实例
  • 提供服务注册或下线
  • 提供服务发现
  • 提供服务注册表至两类客户端(即服务提供者和消费者)

作为客户端(即服务提供者和消费者),包括

  • 连接服务注册中心
  • 向服务注册中心注册或者下线服务实例
  •  向服务注册中心或服务注册缓存列表查询服务

Eureka 集群架构如图所示:

二、运行工程

运行 Eureka 工程 springcloud-eureka-server

运行环境:JDK 7 或 8,Maven 3.0+

技术栈:Spring Cloud Dalston.SR1、 spring-cloud-netflix 1.3.1、Spring Boot 1.5.4

1. git clone 下载工程 springcloud-learning-example

项目地址见 GitHub - https://github.com/JeffLi1993/springcloud-learning-example

git clone https://github.com/JeffLi1993/springcloud-learning-example.git

2. Maven 编译安装这个工程:

  1. cd springcloud-learning-example 
  2. mvn clean install 

3. 运行 springcloud-eureka-server Eureka 工程

右键 Main 函数 Run Eureka Server 启动类 EurekaServerApplication,启动服务注册中心工程。

EurekaServerApplication 类地址:/springcloud-learning-example/springcloud-eureka-sample/springcloud-eureka-server/src/main/java/org/spring/springboot/EurekaServerApplication.java

控制台 Console 看到这类信息,代表启动成功:

  1. 2017-06-30 10:32:47.549  INFO 2977 --- [      Thread-11] e.s.EurekaServerInitializerConfiguration : Started Eureka Server2017-06-30 10:32:47.625  INFO 2977 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8888 (http) 
  2. 2017-06-30 10:32:47.626  INFO 2977 --- [           main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8888 
  3. 2017-06-30 10:32:47.632  INFO 2977 --- [           main] o.s.springboot.EurekaServerApplication   : Started EurekaServerApplication in 23.168 seconds 

4. 访问 Eureka 注册中心可视化界面

打开浏览器,访问 http://localhost:8888/ ,如图所示

可以看到主体信息包括:

  • 系统状态:环境、运行时间、更新时间等
  • 注册信息:服务名、服务地址、服务状态
  • 基本信息:环境、内存、副本信息
  • 实例信息:IP、端口

三、工程代码详解

详解 Eureka 工程 springcloud-eureka-server

1.springcloud-eureka-server 工程目录结构

  1. ├── pom.xml└── src 
  2.     └── main 
  3.         ├── java 
  4.         │   └── org 
  5.         │       └── spring 
  6.         │           └── springcloud 
  7.         │               ├── EurekaServerApplication.java 
  8.         └── resources 
  9.             └── application.yml 

EurekaServerApplication.java Eureka Server 启动类

application.yml 配置文件

2. pom.xml 配置

  1. <?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  2.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/ma ... gt%3B 
  3.      
  4. <modelVersion>4.0.0</modelVersion>     
  5. <groupId>springcloud</groupId>     
  6. <artifactId>springcloud-eureka-server</artifactId>     
  7. <version>0.0.1-SNAPSHOT</version>     
  8.  
  9. <name>springcloud-eureka-server :: Spring Cloud Eureka 服务注册中心</name>     
  10.  
  11. <!-- Spring Boot 启动父依赖 --><parent><groupId>org.springframework.boot</groupId>         
  12. <artifactId>spring-boot-starter-parent</artifactId>         
  13. <version>1.5.4.RELEASE</version>     
  14. </parent> 
  15.  
  16.  
  17.     <dependencies> 
  18.         <!-- Spring Cloud Netflix Eureka Server 依赖 --> 
  19.         <dependency> 
  20.             <groupId>org.springframework.cloud</groupId>             
  21.     <artifactId>spring-cloud-starter-eureka-server</artifactId>         
  22.     </dependency> 
  23.          
  24.      
  25.     <!-- Spring Boot Test 依赖 --> 
  26.         <dependency> 
  27.             <groupId>org.springframework.boot</groupId>             
  28.     <artifactId>spring-boot-starter-test</artifactId>           <scope>test</scope>         
  29.     </dependency> 
  30.      
  31.     </dependencies> 
  32.     <dependencyManagement> 
  33.         <dependencies> 
  34.             <!-- Spring Cloud Netflix 依赖 --> 
  35.             <dependency> 
  36.                 <groupId>org.springframework.cloud</groupId>                <artifactId>spring-cloud-netflix</artifactId>                 
  37.     <version>1.3.1.RELEASE</version>                <type>pom</type>                 
  38.     <scope>import</scope>             
  39.     </dependency> 
  40.         </dependencies> 
  41.     </dependencyManagement> 
  42.     <build> 
  43.         <plugins> 
  44.             <plugin> 
  45.                 <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-compiler-plugin</artifactId>                 
  46.     <configuration> 
  47.                     <source>1.8</source>                     
  48.     <target>1.8</target>                 
  49.     </configuration> 
  50.             </plugin> 
  51.         </plugins> 
  52.     </build> 
  53. </project> 

使用的依赖是

  • spring-cloud-netflix 1.3.1 是 Spring Cloud Dalston.SR1 版本。
  • spring-cloud-starter-eureka-server Eureka Server 模块依赖

上面提到的客户端负载均衡 Ribbon ,可以依赖树中看出 spring-cloud-starter-eureka-server 依赖了 Ribbon 相关的库。因为一般 eureka 本身作为服务自注册实现高可用,也可以作为客户端调用其他服务。

3. application.yml 配置

  1. server: 
  2.   port: 8888 # 服务端口eureka: 
  3.   instance: 
  4.     hostname: localhost # 设置主机名 
  5.   client: 
  6.     registerWithEureka: false # 是否向 Eureka 注册服务。该应用为服务注册中心,不需要自注册,设置为 false 
  7.     fetchRegistry: false      # 是否检索服务。该应用为服务注册中心,职责为注册和发现服务,无需检索服务,设置为 false 
  8.   server: 
  9.     waitTimeInMsWhenSyncEmpty: 0 # 设置同步为空时的等待时间。默认 5 * MINUTES 

application.property,可以看下面的配置解释:

  • server.port 设置工程服务端口
  • eureka.instance.hostname Eureka 实例主机名
  • eureka.client.registerWithEureka 是否向 Eureka 注册服务。服务注册中心服务,没有作为集群,所以不需要自注册,设置为 false
  • eureka.client.fetchRegistry 是否检索服务。该应用为服务注册中心,职责为注册和发现服务,无需检索服务,设置为 false
  • eureka.server.waitTimeInMsWhenSyncEmpty 设置同步为空时的等待时间。默认 5 * MINUTES

4.注册中心应用启动类

  1. /** 
  2.  * Spring Boot Eureka Server 应用启动类 
  3.  * 
  4.  * Created by bysocket on 21/06/17. 
  5.  */ 
  6.  
  7. @EnableEurekaServer     // Eureka Server 标识 
  8. @SpringBootApplication  // Spring Boot 应用标识 
  9. public class EurekaServerApplication {     
  10.  
  11. public static void main(String args) {        // 程序启动入口 
  12.    
  13.        // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件 
  14.         SpringApplication.run(EurekaServerApplication.class,args); 
  15.         
  16.     } 

@EnableEurekaServer 标志该应用作为 Eureka Server ,并会自动化读取相关配置。

四、小结

此小章节介绍了如何 Eureka 作为服务注册中心 Server,下一小结讲下 服务提供者详解 具体是如何向服务注册中心注册自己的。

【本文为51CTO专栏作者“李强强”的原创稿件,转载请通过51CTO联系作者获取授权】

戳这里,看该作者更多好文

责任编辑:武晓燕 来源: 51CTO专栏
相关推荐

2017-07-11 14:48:33

Spring Clou服务提供者

2017-08-18 15:14:04

Spring Clou服务消费者

2017-06-25 13:33:25

Spring Clou微服务架构

2019-08-23 10:34:05

微服务Eureka架构

2023-04-28 07:52:14

CAPEureka注册中心

2020-06-29 07:58:18

ZooKeeperConsul 注册中心

2021-04-28 08:05:30

SpringCloudEureka服务注册

2020-01-10 10:58:34

ZooKeeperEureka注册中心

2021-01-14 07:54:19

Spring Clou应用路由

2022-02-07 07:10:32

服务注册功能

2018-07-13 09:55:35

Eureka闭源Spring Clou

2021-04-20 17:20:59

SpringColud EurekaNetflix开发

2023-12-23 18:04:40

服务Eureka工具

2018-03-02 16:11:29

Spring Clou分布式服务跟踪

2017-07-28 16:41:53

Spring Clou微服务架构

2021-08-04 11:54:25

Nacos注册中心设计

2023-11-27 00:55:43

Eureka服务

2022-05-02 22:01:49

订阅模式Eureka推送模式

2017-12-20 15:37:39

Spring Clou微服务架构

2018-07-19 14:58:14

Spring Clou微服务架构
点赞
收藏

51CTO技术栈公众号