1.Eureka: 尤里卡,netflix在设计Eureka的时候,遵循的是ap原则,是其中的一个子模块,也是核心模块之一,Eureka是基于REST的服务,用于定位服务,以实现云端中间层服务的发现与故障转移,服务注册与发现对于微服务来说,是非常重要的,有了服务的注册与发现,只需要使用服务的标识符,就可以访问到服务,而不需要修改服务调用的配置文件,功能类似于Dubbo的注册中心zookeeper; 2.Eureka的基本原理:
Eureka的基本架构:1.springCloud封装了netFlix公司开发的Eureka模块来实现服务的注册与发现。2.采用的是C-S的架构设计EurekaServcer作为服务注册功能的服务器,他是注册中心。3.系统中的其他微服务,使用的eureka的客户端连接到eurekaserver并维持心跳连接,这样系统的维护人员就可以通过eurekaserver来监控系统中的各个服务是否正常运行,springcloud中其他模块就可以通过eurekaserver来发现系统中的其他服务,并执行相关逻辑。三大角色:1.EurekaServer:提供服务的注册与发现。2.ServiceProvider:将自身服务注册到Eureka中,从而是消费者找到。3.ServiceConsumer:服务消费者从Eureka中获取注册服务列表,从而找到消费服务。1.导入依赖
<!--注入依赖eureka--> <dependencies> <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> <version>1.4.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies>2.编写配置文件
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/ #监控地址3.启动Eureka 创建启动类,在启动类上添加@EnableEurekaServer注解。
@SpringBootApplication @EnableEurekaServer //启动Eureka的启动类,可以接受别人注册进来 public class EurekaServer_7001 { public static void main(String[] args) { SpringApplication.run(EurekaServer_7001.class,args); } }4.访问配置文件的配置的监控地址即可访问Eureka:http://localhost:7001/eureka/