Spring Cloud Eureka 服务治理

一、概念理解

二、搭建注册中心

2.1、创建eureka-server项目

使用的maven依赖:

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>1.5.12.RELEASE</version>

<relativePath/>

</parent>

<dependencies>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-eureka-server</artifactId>

</dependency>

</dependencies>

<dependencyManagement>

<dependencies>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-dependencies</artifactId>

<version>Edgware.SR3</version>

<type>pom</type>

<scope>import</scope>

</dependency>

</dependencies>

</dependencyManagement>

使用@EnableEurekaServer启动服务注册中心,如下:

@EnableEurekaServer

@SpringBootApplication

public class EurekaServerApplication {

public static void main(String[] args) {

SpringApplication.run(EurekaServerApplication.class, args);

}

}

设置端口和域名地址:

server.port=1111

eureka.instance.hostname=localhost

默认情况下,服务注册中心也会将自己作为客户端来尝试注册他自己,所以我们需要禁用他的客户端注

册行为,在application.properties中加入如下配置:

#禁用服务注册中心客户端注册行为

#eureka.client.register-with-eureka 代表不像注册中心注册自己

eureka.client.register-with-eureka=false

#由于注册中心的职责就是为了维护服务实例,它并不需要去检索服务,所以这里也设置成false

eureka.client.fetch-registry=false

设置好后启动并方位:http://localhost:1111/,可以看到如下:


2.2、自我保护

默认情况下,如果Eureka Server在一定时间内没有接收到某个微服务实例的心跳,Eureka Server将会

注销该实例(默认90秒)。但是当网络分区故障发生时,微服务与Eureka Server之间无法正常通信,

以上行为可能变得非常危险了——因为微服务本身其实是健康的,此时本不应该注销这个微服务。

Eureka通过“自我保护模式”来解决这个问题——当Eureka Server节点在短时间内丢失过多客户端时

(可能发生了网络分区故障),那么这个节点就会进入自我保护模式。一旦进入该模式,Eureka

Server就会保护服务注册表中的信息,不再删除服务注册表中的数据(也就是不会注销任何微服务)。

当网络故障恢复后,该Eureka Server节点会自动退出自我保护模式。

综上,自我保护模式是一种应对网络异常的安全保护措施。它的架构哲学是宁可同时保留所有微服务

(健康的微服务和不健康的微服务都会保留),也不盲目注销任何健康的微服务。使用自我保护模式,

可以让Eureka集群更加的健壮、稳定。

但是,在我们实际生产中,我们云环境同一个Region下不会发生大规模网络分区状况,所以没有启用自

我保护。在开发过程中,因为要保证微服务实例尽量准确,所以也建议不要开启,关闭如下:

三、注册 服务提供者

3.1、创建eureka-client项目

使用的maven依赖:

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>1.5.12.RELEASE</version>

<relativePath/>

</parent>

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<java.version>1.8</java.version>

</properties>

<dependencies>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-eureka-server</artifactId>

</dependency>

</dependencies>

<dependencyManagement>

<dependencies>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-dependencies</artifactId>

<version>Edgware.SR3</version>

<type>pom</type>

<scope>import</scope>

</dependency>

</dependencies>

</dependencyManagement>

添加controller,通过注入DiscoveryClient对象,在日志中打印出服务相关内容,如下:

@RestController

public class HelloController {

private final Logger logger = Logger.getLogger(getClass());

@Autowired

private DiscoveryClient discoveryClient;

@RequestMapping("/hello")

public String index() {

ServiceInstance instance = discoveryClient.getLocalServiceInstance();

logger.info("/hello,host:" + instance.getHost() + ",server_id:" +

instance.getServiceId());

return "Hello Word";

}

}

添加@EnableDiscoveryClient注解激活DiscoveryClient实现,如下:

@EnableDiscoveryClient

@SpringBootApplication

public class EurekaClientApplication {

public static void main(String[] args) {

SpringApplication.run(EurekaClientApplication.class, args);

}

}

最后,添加配置文件,指定微服务名称和注册中心地址,如下:

#微服务名称

spring.application.name=eureka-client

#注册中心地址

eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka

启动成功后,注册中心的控制台会打印如下日志,表示启动成功:


同时,监控首页可看到如下微服务信息:

访问微服务地址:http://localhost:8001/hello 可看到打印的日志:


3.2、服务续约

注册完服务之后,服务提供者会维护一个心跳告诉注册中心自己还活着,防止被注册中心“剔除任务”。这里

有两个重要的属性维护服务续约

#定义服务续约任务调用间隔时间,默认30s

eureka.instance.lease-renewal-interval-in-seconds=30

#定义服务失效时间,默认90s

eureka.instance.lease-expiration-duration-in-seconds=90

3.3、健康检查

如3.2所诉,可采用心跳的方式检查服务微服务实例的健康状况,但有可能出现虽然微服务挂了,但发送心跳的

线程却还能正常运行,这样的话就不能检查出微服务的实际健康状况。然而在Spring Cloud Eureka 中,我们

可以通过将客户端健康检查交给 spring-boot-actuator 模块的 /health 端点,以实现更加全面的健康状态维

护。

配置如下:(先要引入spring-boot-starter-actuator)

eureka.client.healthcheck.enabled=true

四、高可用的注册中心

4.1、构建双节点的注册中心集群

在分布式环境中,要中分考虑到单节点注册中心发生故障的情况,所以我们要搭建多节点注册中心的集群。

创建application-peer1.properties配置文件:

spring.application.name=eureka-server

server.port=1111

eureka.instance.hostname=peer1

eureka.client.serviceUrl.defaultZone=http://peer2:1112/eureka/

创建application-peer2.properties配置文件:

spring.application.name=eureka-server

server.port=1112

eureka.instance.hostname=peer2

eureka.client.serviceUrl.defaultZone=http://peer1:1111/eureka/

在 /etc/hosts 文件中添加对 peer1、peer2的转换,如下:

127.0.0.1 peer1

127.0.0.1 peer2

通过指定启动参数 spring.profiles.active 属性来分别启动 peer1 和 peer2,如下:

java -jar eureka-server-1.0.0.jar --spring.profiles.active=peer1

java -jar eureka-server-1.0.0.jar --spring.profiles.active=peer2

在设置了多借点的服务注册中心之后,微服务还需要做一些简单的配置才能将服务注册到 Eureka Server 集群

中,如下:

eureka.client.serviceUrl.defaultZone=http://peer1:1111/eureka/,http://peer2:1112/eureka/

访问注册中心,可以发现微服务被同时注册到了peer1和peer2,此时如果peer1挂掉了,peer2还可以正常使

用,从而实现了高可用。

五、服务发现与消费

5.1、创建SpringBoot基础工程来实现微服务消费者

使用的maven依赖:

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>1.5.12.RELEASE</version>

<relativePath/>

</parent>

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<java.version>1.8</java.version>

</properties>

<dependencies>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-web</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-eureka</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-ribbon</artifactId>

</dependency>

</dependencies>

<dependencyManagement>

<dependencies>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-dependencies</artifactId>

<version>Edgware.SR3</version>

<type>pom</type>

<scope>import</scope>

</dependency>

</dependencies>

</dependencyManagement>

添加@EnableDiscoveryClient注解激活DiscoveryClient实现,以获得服务发现的能力,同时,在主类中创建

RestTemplate的Bean实例,并通过 @LoadBalanced 注解开启客户端负载均衡,如下:

@EnableDiscoveryClient

@SpringBootApplication

public class EurekaClientApplication {

@Bean

@LoadBalanced

RestTemplate restTemplate(){

return new RestTemplate();

}

public static void main(String[] args) {

SpringApplication.run(EurekaClientApplication.class, args);

}

}

添加controller:

@RestController

public class ConsumerController {

@Autowired

private RestTemplate restTemplate;

@RequestMapping("/ribbon-consumer")

public String index() {

return restTemplate.getForEntity("http://eurekaserver/

hello",String.class).getBody();

}

}

启动后方位地址:http://localhost:9000/ribbon-consumer 返回 Hello Word

注意:访问时,由于开启了客户端负载均衡,首次请求时会获取所有的微服务实例,再通过轮询的方式方位各

个微服务,通过查看微服务8001,8002的访问日志也可以看出来。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,667评论 5 472
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,361评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,700评论 0 333
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,027评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,988评论 5 361
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,230评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,705评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,366评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,496评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,405评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,453评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,126评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,725评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,803评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,015评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,514评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,111评论 2 341

推荐阅读更多精彩内容