Eureka注册中心

199 阅读1分钟

image.png 图片来源:衡水中学的一分钟www.bilibili.com/video/BV14E…

为啥要有注册中心

在前后端分离架构中,服务层被拆分成了很多微服务,微服务的信息如何管理?
Spring Cloud中提供服务注册中心来管理微服务信息。 为什么要用注册中心?

  1. 微服务数量多,要进行远程调用就需要知道服务端的ip地址和端口,注册中心帮助我们管理这些服务的ip和端口。
  2. 微服务会实时上报自己的状态,注册中心统一管理这些微服务的状态,将存在问题的服务剔除服务列表,客户端获取到可用的服务进行调用。

Eureka介绍

SpringCloudEureka是对Netflix公司的Eureka的二次封装,它实现了服务治理的功能,SpringCloudEureka提供服务端与客户端,服务端即是Eureka服务注册中心,客户端完成微服务向Eureka服务的注册与发现。服务端和客户端均采用Java语言编写。

image.png

搭建环境

添加依赖

父工程依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>Finchley.SR1</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

Eureka Server工程

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
</dependencies>

启动类@EnableEurekaServer

@EnableEurekaServer  //标识这是一个EurekaServer
@SpringBootApplication
public class GorvernCenterApplication {
    public static void main(String[] args) {
        SpringApplication.run(GorvernCenterApplication.class,args);
    }
}