天天看点

spring cloud-eurake服务注册与发现(二)

目录

Eurake是什么

Eurake的基本架构

三大角色

springcloud-eureka-7001    eureka服务注册中心 Module

修改springcloud-model-provider-dept-8001

actuator与注册微服务信息完善

Eureka的自我保护

Eureka服务发现

Eureka集群

Eurake是什么

Eureka是Netflix的一个子模块,也是核心模块之一。Eureka是一个基于REST的服务,用于定位服务,以实现云端中间层服务发现和故障转移。

服务注册与发现对于微服务架构来说是非常重要的,有了服务发现与注册,只需要使用服务的标识符,就可以访问到服务,而不需要修改服务调用的配置文件了。

功能类似于dubbo的注册中心,比如Zookeeper。

Eurake的基本架构

Spring Cloud 封装了 Netflix 公司开发的 Eureka 模块来实现服务注册和发现(请对比Zookeeper)。

Eureka 采用了 C-S 的设计架构。Eureka Server 作为服务注册功能的服务器,它是服务注册中心。

而系统中的其他微服务,使用 Eureka 的客户端连接到 Eureka Server并维持心跳连接。这样系统的维护人员就可以通过

Eureka Server 来监控系统中各个微服务是否正常运行。SpringCloud 的一些其他模块(比如Zuul)就可以通过

Eureka Server 来发现系统中的其他微服务,并执行相关的逻辑。

请注意和Dubbo的架构对比

Eureka包含两个组件:Eureka Server和Eureka Client

Eureka Server提供服务注册服务

各个节点启动后,会在EurekaServer中进行注册,这样EurekaServer中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观的看到

EurekaClient是一个Java客户端,用于简化Eureka Server的交互,客户端同时也具备一个内置的、使用轮询(round-robin)负载算法的负载均衡器。

在应用启动后,将会向Eureka Server发送心跳(默认周期为30秒)。如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,

EurekaServer将会从服务注册表中把这个服务节点移除(默认90秒)

三大角色

Eureka Server 提供服务注册和发现

Service Provider服务提供方将自身服务注册到Eureka,从而使服务消费方能够找到

Service Consumer服务消费方从Eureka获取注册服务列表,从而能够消费服务

springcloud-eureka-7001    eureka服务注册中心 Module

spring cloud-eurake服务注册与发现(二)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.jiangjy.springcloud</groupId>
    <artifactId>springcloud-model</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>springcloud-model-eureka-7001</artifactId>
  
  <!-- 约定 -->
  <dependencies>
  <!--eureka-server服务端 -->
  	<dependency>
  		<groupId>org.springframework.cloud</groupId>
  		<artifactId>spring-cloud-starter-eureka-server</artifactId>
  	</dependency>
  	
  	<!-- 修改后立即生效,热部署 -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>springloaded</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-devtools</artifactId>
	</dependency>
  </dependencies>
  
</project>
           
server:
  port: 7001
  
eureka:
  instance:
    hostname: localhost  #eureka服务端的实例名称
  client:
    register-with-eureka: false    #false表示不向注册中心注册自己。   
    fetch-registry: false   #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/    #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。   
           
package com.jiangjy.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer  //EurekaServer服务器端启动类,接受其它微服务注册进来
public class EurekaServer7001_APP {

	public static void main(String[] args) {
		SpringApplication.run(EurekaServer7001_APP.class, args);
	}
}

           

修改springcloud-model-provider-dept-8001

spring cloud-eurake服务注册与发现(二)

约定

<!-- Euraka客户端  将微服务provider侧注册进eureka -->
		<dependency>
	  		<groupId>org.springframework.cloud</groupId>
	  		<artifactId>spring-cloud-starter-eureka</artifactId>
  		</dependency>
  		<dependency>
  			<groupId>org.springframework.cloud</groupId>
  			<artifactId>spring-cloud-starter-config</artifactId>
  		</dependency>
           

配置

eureka:
  client:   #客户端注册进eureka服务列表内
    service-url:
      defalutZone: http://localhost:7001/eureka/
           

主启动类添加@EnableEurekaClient注解

spring cloud-eurake服务注册与发现(二)
spring cloud-eurake服务注册与发现(二)

注意:路径不要写错了

测试

spring cloud-eurake服务注册与发现(二)
spring cloud-eurake服务注册与发现(二)
spring cloud-eurake服务注册与发现(二)

主机名称:服务名称修改:

spring cloud-eurake服务注册与发现(二)

actuator与注册微服务信息完善

超链接点击服务报告ErrorPage

spring cloud-eurake服务注册与发现(二)

先有jar支持  在 配置  在编码

操作: 1.在springcloud-model-provider-dept-8001中pom.xml

<!-- actuator监控信息完善 -->
  		<dependency>
  			<groupId>org.springframework.boot</groupId>
  			<artifactId>spring-boot-starter-actuator</artifactId>
  		</dependency>
           

2.在总的父工程microservicecloud修改pom.xml添加构建build信息

<build>
  	<!-- 所有资源的目录在src/main/resources下面 -->
  	<resources>
			<resource>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
			</resource>
		</resources>
		<plugins>
			<!-- 使用插件来读取目录资源下的文件  以 $开始   $结尾 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
				<configuration>
					<delimiters>
						<delimit>$</delimit>
					</delimiters>
				</configuration>
			</plugin>
		</plugins>
  </build>
           

application.yml   在添加了jar包支持的情况下

info:
  app.name: jiangjy-springcloud
  company.name: www.jiangjy.com
  build.artifactId: $project.artifactId$ 
  build.version: $project.version$
           

展示info信息

spring cloud-eurake服务注册与发现(二)

Eureka的自我保护

什么是自我保护模式? 

默认情况下,如果EurekaServer在一定时间内没有接收到某个微服务实例的心跳,EurekaServer将会注销该实例(默认90秒)。

但是当网络分区故障发生时,微服务与EurekaServer之间无法正常通信,以上行为可能变得非常危险了——因为微服务本身其实是健康的,此时本不应该注销这个微服务。

Eureka通过“自我保护模式”来解决这个问题——当EurekaServer节点在短时间内丢失过多客户端时(可能发生了网络分区故障),那么这个节点就会进入自我保护模式。

一旦进入该模式,EurekaServer就会保护服务注册表中的信息,不再删除服务注册表中的数据(也就是不会注销任何微服务)。

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

在自我保护模式中,Eureka Server会保护服务注册表中的信息,不再注销任何服务实例。

当它收到的心跳数重新恢复到阈值以上时,该Eureka Server节点就会自动退出自我保护模式。

它的设计哲学就是宁可保留错误的服务注册信息,也不盲目注销任何可能健康的服务实例。一句话讲解:好死不如赖活着

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

也不盲目注销任何健康的微服务。使用自我保护模式,可以让Eureka集群更加的健壮、稳定。

在Spring Cloud中,可以使用eureka.server.enable-self-preservation = false 禁用自我保护模式。

Eureka服务发现

对外暴露方法了解本服务

第一步:需要在服务中提供描述自己的方法

spring cloud-eurake服务注册与发现(二)
@Autowired
	private DiscoveryClient client;
	
	@RequestMapping(value="dept/discovery",method = RequestMethod.GET)
	public Object discovery() {
		List<String> services = client.getServices();
		System.out.println("*********" + services);
		for (String service : services) {
			System.out.println("service: " + service);	
		}
		List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-MODEL-DEPT");
		for (ServiceInstance serviceInstance : instances) {
			System.out.println(serviceInstance.getServiceId() + "\t" + serviceInstance.getHost() 
			+ "\t" + serviceInstance.getPort() +"\t" + serviceInstance.getUri());
		}
		
		return this.client;
	}
           

主启动类添加注解@EnableDiscoveryClient //服务发现

spring cloud-eurake服务注册与发现(二)

自测:先启动EurekaServer,再启动8001主启动类,等一会让他入住EurekaServer服务

http://localhost:8001/dept/discovery

spring cloud-eurake服务注册与发现(二)

完成后修改80消费者

spring cloud-eurake服务注册与发现(二)

@RequestMapping(value = "consumer/dept/discovery")
	public Object discovery() {
		return restTemplate.getForObject(REST_URL_PREFIX+"/dept/discovery", Object.class);
	}
           

运行结果:

spring cloud-eurake服务注册与发现(二)

Eureka集群

新建springcloud-model-eureka-7002/ springcloud-model-eureka-7003

按照7001模板粘体pom.xml到7002/7003

修改7002/7003得主启动类

修改映射配置

       找到C:\Windows\System32\drivers\etc路径下的hosts文件

spring cloud-eurake服务注册与发现(二)

一台是没有问题,多台需要做域名映射

127.0.0.1       eureka7001.com

127.0.0.1       eureka7002.com

127.0.0.1       eureka7003.com

3台eureka服务器得application.yml配置/7001/7002/7003

server:
  port: 7001
  
eureka:
  instance:
    #hostname: localhost  #eureka服务端的实例名称
    hostname: eureka7001.com  #eureka服务端的实例名称-在hosts中做得配置
  client:
    register-with-eureka: false    #false表示不向注册中心注册自己。   
    fetch-registry: false   #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    service-url:
      #单机版 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/    #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。
      defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/  #多台服务器配置
server:
  port: 7002
  
eureka:
  instance:
    #hostname: localhost  #eureka服务端的实例名称
    hostname: eureka7002.com  #eureka服务端的实例名称-在hosts中做得配置
  client:
    register-with-eureka: false    #false表示不向注册中心注册自己。   
    fetch-registry: false   #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    service-url:
      #单机版 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/    #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/  #多台服务器配置
server:
  port: 7003
  
eureka:
  instance:
    #hostname: localhost  #eureka服务端的实例名称
    hostname: eureka7003.com  #eureka服务端的实例名称-在hosts中做得配置
  client:
    register-with-eureka: false    #false表示不向注册中心注册自己。   
    fetch-registry: false   #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    service-url:
      #单机版 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/    #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/  #多台服务器配置
           

springcloud-model-provider-dept-8001微服务发布到上面3台eureka集群配置中

单机版得时候只需要注册到一台,现在多台服务器注册,需要修改

#单机版      defaultZone: http://localhost:7001/eureka/
        defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/

           

测试:启动eureka服务7001/7002/7003

再启动8001

此时:访问地址改变了:应该是

eureka7001.com:7001

eureka7002.com:7002

eureka7003.com:7003

spring cloud-eurake服务注册与发现(二)
spring cloud-eurake服务注册与发现(二)
spring cloud-eurake服务注册与发现(二)

测试成功完成;

继续阅读