
承接上文,我们在上文中已经成功搭建了一个简单的SpringCloud项目,实现了服务消费端对服务提供端的服务的简单访问。本文将在上文的基础上整合Eureka完成服务注册中心的搭建。
在开启正文之前,先让我们来简单了解一下eureka产生的背景~
在开发过程中,我们总是需要手动的使用ip:port的方式去直接调用服务,而随着项目复杂程度的加深,服务数量也在飞速增长,仅仅靠传统的调用方式显然已经无法满足对于服务的维护和负载均衡的实现。此时,服务注册中心的概念便应运而生。
使用服务注册中心,能够实现服务治理,服务动态扩容,以及服务调用的负载均衡,而我们今天要说的Eureka,便是实现服务注册中心的元老级选手。
Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,SpringCloud将它集成在其子项目spring-cloud-netflix中,以实现SpringCloud的服务发现功能。
简单来说:就是服务提供者将服务注册进Eureka中,服务调用者再通过Eureka获取相对应的服务,在这过程中,Eureka实现了服务的 自动注册、 服务发现和 状态监控。

提供完成的服务注册和服务发现实现
与spirngcloud无缝集成
采用AP而非CP
Eureka的功能专注于服务注册和发现,不会出现数据的竞态。即在同一时间内,不会有多个线程竞争同一数据。AP原则保证了可用性,实现了最终一致性。

eurekaAP架构

- <dependencies>
-
- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
- dependency>
-
- <dependency>
- <groupId>com.canrioyuangroupId>
- <artifactId>cloud-api-commonsartifactId>
- <version>${project.version}version>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-actuatorartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-devtoolsartifactId>
- <scope>runtimescope>
- <optional>trueoptional>
- dependency>
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- dependency>
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- dependency>
- dependencies>
- server:
- port: 7001
-
-
- eureka:
- instance:
- hostname: eureka7001.com #eureka服务端的实例名称
- client:
- register-with-eureka: false #false表示不向注册中心注册自己。
- fetch-registry: false #表示是否从Eureka Server获取注册的服务信息
- service-url:
- #单机即指向7001自己
- defaultZone: http://eureka7001.com:7001/eureka
- package com.canrioyuan;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
- @SpringBootApplication
- @EnableEurekaServer//表示这是一个服务注册中心的服务组件
- public class MainEureka7001 {
- public static void main(String[] args) {
- SpringApplication.run(MainEureka7001.class,args);
- }
- }

- <dependency>
- <groupId>org.springframework.cloudgroupId>
- <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
- dependency>
- eureka:
- instance:
- instance-id: payment8001
- #以IP地址注册到服务中心,相互注册使用IP地址
- prefer-ip-address: true
-
-
- client:
- #表示是否将自己注册进EurekaServer中,默认为true
- register-with-eureka: true
- #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
- fetch-registry: true
- service-url:
- defaultZone: http://localhost:7001/eureka
- package com.canrioyuan;
-
- import org.mybatis.spring.annotation.MapperScan;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
-
- @SpringBootApplication
- @MapperScan(value = "com.canrioyuan.mapper")
- @EnableEurekaClient
- //@EnableDiscoveryClient //可以通过服务发现来获得该服务的信息
- /**
- * @EnableEurekaClient 和 @EnableDiscoveryClient
- * 两者之间,如果注册中心使用Eureka我们推荐使用@EnableEurekaClient
- * 如果是其他的服务注册中心推荐使用@EnableDiscoveryClient来实现服务发现
- */
- public class PaymentMain8001 {
- public static void main(String[] args){
- SpringApplication.run(PaymentMain8001.class,args);
- }
- }


虽然Eureka的功能很强大,但值得一提的是, Eureka2.0已于2018年7月份”停止更新。市面上也开始涌现出如Consul、Nacos、Zookeeper等用于替代的Eureka的技术,但是Eureka作为服务注册中心的开创者,其很多结构和思想还是很值得我们去学习的。

