一,什么是注册中心
服务注册中心 :Service Registry
二,注册中心有什么作用
- 服务注册/反注册:保存服务提供者和服务调用者的信息
- 服务订阅/取消订阅:服务调用者订阅服务提供者的信息,最好有实时推送的功能
- 服务路由(可选):具有筛选整合服务提供者的能力。
- 配置订阅:服务提供者和服务调用者订阅微服务相关的配置
- 配置下发:主动将配置推送给服务提供者和服务调用者
- 检测服务提供者的健康情况
三,常见注册中心
- Zookeeper
- Eureka
- Consul
- Nacos
四,Eureka 的概述
4.1 Eureka 的基础知识
- 提供服务注册和发现
- 服务提供方
- 将自身服务注册到Eureka,从而使服务消费方能够找到
- 服务消费方
- 从Eureka获取注册服务列表,从而能够消费服务
五,注册中心搭建
我前面已经搭建了一个微服务,不过是服务消费方直接硬编码请求服务提供方。
springCloud01: springCloud01-20211225 练习springCloudhttps://gitee.com/vegetarian/spring-cloud01.git下面使用注册中心,来操作一下。
5.1 搭建Eureka服务中心
1,搭建eurekaServer 模块
2,引入eureka 依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
3,设置eureka 配置文件
server:
port: 9003
spring:
application:
name: eureka-order
eureka:
instance:
hostname: localhost
client:
registerWithEureka : false # 是否将自己注册到注册中心
fetchRegistry : false # 是否从eureka中获取注册信息
serviceUrl : #配置暴露给eureka client 的请求地址
defaultZone : http://${eureka.instance.hostname}:${server.port}/eureka/
4,配置eureka 启动类
package com.zjk.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class,args);
}
}
@EnableEurekaServer : 激活Eureka Server端配置
然后启动eurekaServer 模块,在浏览器访问配置文件的端口,出现下面页面即是访问成功!

