共计 5623 个字符,预计需要花费 15 分钟才能阅读完成。
Eureka 注册核心 - 介绍
又称服务中心,治理各种服务性能包含 服务的注册、发现、熔断、负载、降级
等。
应用背景
- 任何一个服务都不能间接去掉用,都须要通过
注册核心
来调用。通过服务中心来获取服务你不须要关注你调用的我的项目 IP 地址,由几台服务器组成,每次间接去服务中心获取能够应用的服务去调用既可。 - 因为各种服务都注册到了服务中心,就有了很多高级性能条件。
比方几台服务提供雷同服务来做客户端负载平衡(Ribbon);
监控服务器调用成功率来做断路器(Hystrix),移除服务列表中的故障点;
监控服务调用工夫来对不同的服务器设置不同的权重、智能路有(Zuul)等等;
SpringCloud -Eureka 注册核心
Spring Cloud 封装了 Netflix 公司开发的 Eureka 模块来实现服务注册和发现。Eureka 采纳了 C-S
的设计架构。
Eureka Server 作为服务注册性能的服务器 ,它是服务注册核心。而零碎中的 其余微服务,应用 Eureka 的客户端连贯到 Eureka Server,并维持心跳连贯 。这样零碎的保护人员就能够 通过 Eureka Server 来监控零碎中各个微服务是否失常运行。Spring Cloud 的一些其余模块(比方 Zuul)就能够通过 Eureka Server 来发现零碎中的其余微服务,并执行相干的逻辑。
Eureka 由两个组件组成:Eureka 服务器和 Eureka 客户端。
- Eureka 服务器用作服务注册服务器。
- Eureka 客户端是一个 java 客户端,用来简化与服务器的交互、作为轮询负载均衡器,并提供服务的故障切换反对。
- Netflix 在其生产环境中应用的是另外的客户端,它提供基于流量、资源利用率以及出错状态的加权负载平衡。
Eureka 架构图
上图简要形容了 Eureka 的根本架构,由 3 个角色组成:
1、Eureka Server
- Eureka Server 作为一个独立的部署单元,以 REST API 的模式为服务实例提供了注册、治理和查问等操作。同时,Eureka Server 也为咱们提供了可视化的监控页面,能够直观地看到各个 Eureka Server 以后的运行状态和所有已注册服务的状况。
2、Service Provider
- 服务提供方
- 将本身服务注册到 Eureka,从而使服务生产方可能找到
3、Service Consumer
- 服务生产方
- 从 Eureka 获取注册服务列表,从而可能生产服务
上手 -Eureka Server
1. 创立一个根底的 Spring Boot 工程,pom 中增加依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 引入 Eureka-server 相干 jar-->
<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>Brixton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2. 启动代码中增加 @EnableEurekaServer
注解
@SpringBootApplication
@EnableEurekaServer
public class SpringCloudEurekaApplication {public static void main(String[] args) {SpringApplication.run(SpringCloudEurekaApplication.class, args);
}
}
3. 默认设置下,该服务注册核心也会将本人作为客户端来尝试注册它本人,所以咱们须要禁用它的客户端注册行为,在 application.properties
增加以下配置:
spring.application.name=Eureka Server
server.port=7001
#示意是否将本人注册到 Eureka Server,默认为 true
eureka.client.register-with-eureka=false
#示意是否从 Eureka Server 获取注册信息,默认为 true
eureka.client.fetch-registry=false
#设置与 Eureka Server 交互的地址,查问服务和注册服务都须要依赖这个地址。默认是 http://localhost:8761/eureka;多个地址可应用 , 分隔
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
application.yml 模式(上面不再给出)
eureka:
client:
fetch-registry:
register-with-eureka: false
serviceUrl:
defaultZone: http://localhost:${server.port}/eureka/
server:
port: 7001
spring:
application:
name: Eureka Server
启动工程后,拜访:http://localhost:7001/ 能够看到没有任何服务
上手 -Eureka Client
创立提供服务的客户端,并向服务注册核心注册本人。实现一个 RESTful API,通过传入两个参数 a 和 b,最初返回 a + b 的后果
1. 创立一个根底的 Spring Boot 工程,pom 中增加依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2. 实现 /add
申请解决接口,通过 DiscoveryClient
对象,在日志中打印出服务实例的相干内容
@RestController
public class ComputeController {private final Logger logger = Logger.getLogger(getClass());
@Autowired
private DiscoveryClient client;
@RequestMapping(value = "/add" ,method = RequestMethod.GET)
public Integer add(@RequestParam Integer a, @RequestParam Integer b) {ServiceInstance instance = client.getLocalServiceInstance();
Integer r = a + b;
logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r);
return r;
}
}
3. 启动代码中增加 @EnableDiscoveryClient
注解,该注解能激活 Eureka 中的 DiscoveryClient
实现,能力实现 Controller 中对服务信息的输入。
@SpringBootApplication
@EnableEurekaServer
public class SpringCloudEurekaApplication {public static void main(String[] args) {SpringApplication.run(SpringCloudEurekaApplication.class, args);
}
}
4. 在 application.properties
增加以下配置:
# 能够指定微服务的名称后续在调用的时候只须要应用该名称就能够进行服务的拜访。spring.application.name=Eureka Client
server.port=8001 #端口
#属性对应服务注册核心的配置内容,指定服务注册核心的地位
eureka.client.serviceUrl.defaultZone=http://localhost:7001/eureka/
启动工程后,再次拜访:http://localhost:7001/ 能够看到定义的服务被注册了
进行服务
1. 间接停掉服务。
默认状况下,如果 Eureka Server 在 90 秒
没有收到 Eureka 客户的续约,它会将实例从其注册表中删除。但这种做法的不好之处在于,客户端曾经进行了运行,但依然在注册核心的列表中。尽管通过肯定的负载平衡策略或应用熔断器能够让服务失常进行,但有没有办法让注册核心马上晓得服务曾经下线呢?
2. 为了让注册核心马上晓得服务要下线,能够向 eureka 注册核心发送delete 申请
格局为 /eureka/apps/{application.name}(application 的 name)/ 划红线的地址
划红线的地址 eureka.client.instance.instance-id
上面是下线一个 hello-service 的例子。
下图是用 postman 发送 delete 申请
值得注意的是,Eureka 客户端每隔一段时间(默认 30 秒)会发送一次心跳到注册核心续约。如果通过这种形式下线了一个服务,而没有及时停掉的话,该服务很快又会回到服务列表中。
所以,能够 先停掉服务,再发送申请将其从列表中移除。
http://127.0.0.1:7001/eureka/apps/config-server/WINDOWS-61L7RBP:config-server:8769
3. 客户端被动告诉注册核心下线
如果你的 eureka 客户端是是一个 spring boot 利用,能够通过调用以下代码告诉注册核心下线。
DiscoveryManager.getInstance().shutdownComponent();
例子如下,在 eureka client 中
@RestController
public class HelloController {
@Autowired
private DiscoveryClient client;
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String index() {java.util.List<ServiceInstance> instances = client.getInstances("hello-service");
return "Hello World";
}
@RequestMapping(value = "/offline", method = RequestMethod.GET)
public void offLine(){DiscoveryManager.getInstance().shutdownComponent();}
}
GET 申请 127.0.0.1:6020/offline