介绍并实现Eureka的服务注册与发现,关键代码并不是特别多,主要是一些注解。由于在面试会问到里面的一些概念,优缺点,以及与其他框架的比较,所以需要记住下面的一些常用概念。
Eureka架构模式与Dubbo+Zookeeper的架构模式(点击这个链接)类似,都是往注册中心注册,消费者也是从注册中心去拿取。
不同之处: Dubbo+Zookeeper的远程调用是基于RPC,而Eureka的远程调用时基于REST接口的。Dubbo+Zookeeper需要下载一个客户端,而Eureka是自己开发一个EurekaServer的服务端让其成为服务注册中心。维护人员可以通过Eureka来监控系统中的各个微服务是否正常运行。
整体项目承接于学习笔记二
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>springcloud-consumer-dept-80</artifactId>
<groupId>com.kuang</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../springcloud-consumer-dept-80/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-eureka-7001</artifactId>
<dependencies>
<!--导一个Eureka的包-->
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!--热部署工具-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>
server:
port: 7001
# Eureka的配置
eureka:
instance:
hostname: localhost #Eureka服务端的实例名称
client:
register-with-eureka: false #表示是否向eureka注册中心注册自己,因为这个是服务器端,所以不需要注册自己
fetch-registry: false #fetch-registry如果为false,则表示自己为注册中心
service-url: # 类似于服务监控页面,动态获取
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
package springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer //EnableEurekaServer 服务端启动类,可以接受别人注册进来 //这个就是核心注解
public class EurekaServer_7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaServer_7001.class,args);
}
}
**学习笔记二**中的模块2
增加两个相关依赖
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!--完善监控信息-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
增加Eureka的相关配置
# Eureka的配置
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka/
instance:
instance-id: springcloud-provide-dept-8001 # 修改Eureka上的默认描述信息
#info配置,展示的相关信息
info:
app.name: Kuangsheng
company.name: NCU
package com.kuang.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient //在此服务启动后将自动注册到Eureka中
public class DeptProvider_8001 {
public static void main(String[] args) {
SpringApplication.run(DeptProvider_8001.class,args);
}
}
自我保护机制的关键是:好死不如赖活着 ,一句话总结:某时刻某一个微服务不可以用了,eureka不会立刻清理,eureka依旧会对该微服务的信息进行保存!
package com.kuang.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient //在此服务启动后自动注册到Eureka中
@EnableDiscoveryClient //服务发现
public class DeptProvider_8001 {
public static void main(String[] args) {
SpringApplication.run(DeptProvider_8001.class,args);
}
}
//注册进来的微服务,来获取一些信息
@Autowired
//注这个包是import org.springframework.cloud.client.discovery.DiscoveryClient;
private DiscoveryClient client;
@GetMapping("/discovery")
public Object discovery(){
//获取微服务列表的清单
List<String>services=client.getServices();
System.out.println("discovery=>:"+services);
//得到一个具体的微服务信息,通过具有的微服务id,applicationName
List<ServiceInstance> instances=client.getInstances("SPRINGCLOUD-PROVIDE-DEPT-8001");//这个值是来自于application那一列的值
for (ServiceInstance instance : instances) {
System.out.println(instance.getHost()+"\n"+
instance.getPort()+"\n"+
instance.getUri()+"\n"+instance.getServiceId()
);
}
return this.client;
}
构建一个Eureka集群,做3个注册中心,构建集群有什么好处呢?
回答: 如果有3个注册中心的话,万一崩了一个注册中心,另外两个也可以发挥作用
因为已经有了1个springcloud-eureka-7001注册中心模型,所以只需要按照它的模板在重新复制出两个来即可。如果只是单纯的复制了两个出来,它三个之间没有任何联系,这也叫集群吗?
回答: 它们3需要相互注册相互依赖,才能起来集群的效果,才能起来,一个注册中心崩了,另外的可以顶替上去起到相应的效果。
server:
port: 7001
# Eureka的配置
eureka:
instance:
hostname: eureka7001.com #Eureka服务端的实例名称
client:
register-with-eureka: false #表示是否向eureka注册中心注册自己,因为这个是服务器端,所以不需要注册自己
fetch-registry: false #fetch-registry如果为false,则表示自己为注册中心
service-url: # 类似于服务监控页面
#defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ 这个只是在单机(只有一个注册中心)的情况下的配置
#而集群,需要管理其他节点
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
server:
port: 7002
# Eureka的配置
eureka:
instance:
hostname: eureka7002.com #Eureka服务端的实例名称
client:
register-with-eureka: false #表示是否向eureka注册中心注册自己,因为这个是服务器端,所以不需要注册自己
fetch-registry: false #fetch-registry如果为false,则表示自己为注册中心
service-url: # 类似于服务监控页面
#defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ 这个只是在单机(只有一个注册中心)的情况下的配置
#而集群,需要管理其他节点
defaultZone: http://eureka7003.com:7003/eureka/,http://eureka7001.com:7001/eureka/
server:
port: 7003
# Eureka的配置
eureka:
instance:
hostname: eureka7003.com #Eureka服务端的实例名称
client:
register-with-eureka: false #表示是否向eureka注册中心注册自己,因为这个是服务器端,所以不需要注册自己
fetch-registry: false #fetch-registry如果为false,则表示自己为注册中心
service-url: # 类似于服务监控页面
#defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ 这个只是在单机(只有一个注册中心)的情况下的配置
#而集群,需要管理其他节点
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7001.com:7001/eureka/
为什么还有进行修改呢?
回答 :因为它原来是往服务中心去进行注册,而现在是往集群里面注册所以需要修改
# Eureka的配置
eureka:
client:
service-url:
# 修改添加的地方,其他地方不变
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
instance:
instance-id: springcloud-provide-dept-8001 # 修改Eureka上的默认描述信息
启动 三个集群+服务提供者
首先需要了解一下CAP原则(点击这个链接,看一下CAP原则)
CAP原则包括了一致性,可用性,分区容错性
一个分布式系统不可能同时很好的满足一致性,可用性,分区容错性这个三个需求
根据CAP定理,将NoSQL数据库分成了满足CA原则,满足CP原则和满足AP原则三大类
作为服务注册中心,Eureka与Zookeeper的对比?
因为一个分布式系统不能同时满足C(一致性),A(可用性),P(分区容错性)这三个特性,而分区容错性P在分布式系统中是必须要保证的( 因为如果没有多个节点的话,就不叫分布式系统了)。因此我们只能在A和C之间进行权衡。
这部分内容,代码只是少部分,这一块知识更多的是面试或笔试的相关知识,还是要强行记忆。
博客总结于:狂神说Java视频。B站地址:ttps://space.bilibili.com/95256449