微商城网站建设新闻网站开发步骤
文章目录
- SpringCloud微服务技术栈的注册中心Eureka
- 简介
- Eureka特点
- 操作步骤
- 环境准备
- 创建Eureka Server
- 注册服务提供方
- 调用服务消费方
- 总结
SpringCloud微服务技术栈的注册中心Eureka
简介
在微服务架构中,服务的数量庞大,而且每个服务可能会有多个实例。此时,需要一个中心化的地方来管理和维护各个服务的相关信息,这就是微服务治理中很重要的一环:服务注册与发现。其中,服务注册是指将提供服务的应用实例注册到注册中心,而服务发现则是指从注册中心中获取服务实例列表并调用服务。本文将介绍SpringCloud微服务技术栈中的注册中心Eureka。
Eureka特点
Eureka是Netflix开源的一个基于REST的服务治理解决方案,主要包括服务注册与发现机制。Eureka的特点如下:
- 高可用:Eureka采用了Peer-to-Peer的复制方式,所有节点均可作为服务注册中心,这样就不会出现单点故障;同时,Eureka还支持使用Zookeeper、Consul等作为注册中心。
- 服务注册:服务提供方(Provider)通过HTTP方式向Eureka注册中心注册自己的服务实例,包括应用名、主机名、IP地址和端口等元数据。
- 服务发现:服务消费方(Consumer)通过Eureka注册中心获取对应服务的实例列表,并通过负载均衡算法选取其中一台实例进行调用。
- 心跳机制:Eureka利用心跳机制保证服务实例的可用性,即每隔30秒向注册中心发送一次心跳信号;同时,注册中心会定期清理没有心跳的服务实例。
操作步骤
环境准备
- JDK 1.8
- Spring Boot 2.0.3.RELEASE
- Spring Cloud Finchley.RELEASE
创建Eureka Server
-
在Maven项目中添加以下依赖:
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
-
在启动类上添加
@EnableEurekaServer
注解:@SpringBootApplication @EnableEurekaServer public class EurekaApplication {public static void main(String[] args) {SpringApplication.run(EurekaApplication.class, args);}}
-
在配置文件中添加如下配置:
server:port: 8761eureka:instance:hostname: localhostclient:register-with-eureka: falsefetch-registry: falseserver:enable-self-preservation: false
server.port
:Eureka Server的端口号,默认为8761。eureka.instance.hostname
:当前Eureka Server的地址。eureka.client.register-with-eureka
:是否将当前应用注册到Eureka Server,默认为true。eureka.client.fetch-registry
:是否从Eureka Server获取服务注册信息,默认为true。eureka.server.enable-self-preservation
:是否启用自我保护机制,默认为true。
-
启动项目,访问
http://localhost:8761/
,即可看到Eureka Server的控制台界面。
注册服务提供方
-
在Maven项目中添加以下依赖:
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
-
在配置文件中添加如下配置:
spring:application:name: demo-serviceserver:port: 8080eureka:client:service-url:defaultZone: http://localhost:8761/eureka/
spring.application.name
:当前应用的名称,用于注册到Eureka Server中。server.port
:当前应用的端口号。eureka.client.service-url.defaultZone
:Eureka Server的地址。
-
在启动类上添加
@EnableDiscoveryClient
注解:@SpringBootApplication @EnableDiscoveryClient public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}}
-
编写一个RESTful服务接口作为演示:
@RestController public class DemoController {@GetMapping("/hello")public String hello() {return "Hello, World!";}}
-
启动项目,可以看到服务已成功注册到Eureka Server中。
调用服务消费方
-
在Maven项目中添加以下依赖:
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
-
在配置文件中添加如下配置:
spring:application:name: demo-consumereureka:client:service-url:defaultZone: http://localhost:8761/eureka/
spring.application.name
:当前应用的名称。eureka.client.service-url.defaultZone
:Eureka Server的地址。
-
在启动类上添加
@EnableDiscoveryClient
注解:@SpringBootApplication @EnableDiscoveryClient public class DemoConsumerApplication {public static void main(String[] args) {SpringApplication.run(DemoConsumerApplication.class, args);}}
-
编写一个RESTful服务接口作为演示:
@RestController public class DemoController {@Autowiredprivate RestTemplate restTemplate;@GetMapping("/hello")public String hello() {String url = "http://demo-service/hello";return restTemplate.getForObject(url, String.class);}}
-
启动项目,访问
http://localhost:8080/hello
,会看到返回结果Hello, World!
。
总结
本文介绍了SpringCloud微服务技术栈的注册中心Eureka,并以实际操作的方式进行了详细的说明。在实际使用中,Eureka的高可用、服务注册与发现、心跳机制等功能都能够有效地提升微服务架构的可用性和稳定性。