Euraka 是netfix开发的基于REST服务基于AP框架的注册中心,主要是用于服务的注册,管理,负载均衡,服务故障转移
Eureka主要分俩部分
Eureka Server:服务中心Server端,提供服务注册 发现 健康检查等服务
Eureka Client:服务提供者,启动之后会向Eureka Server注册自己的服务
搭建一个Eureka Server
在项目的pom文件中添加eureka server依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
在启动类上添加注解 @EnableEurekaServer 表示这是个EurekaServer服务
再添加上配置文件信息
server:
port: 10010
spring:
application:
name: eureka-server
eureka:
instance:
hostname: localhost
client:
fetch-registry: false #表⽰是否从Eureka Server获取注册信息,默认为true.因为这是⼀个单点的Eureka Server,不需要同步其他的Eureka Server节点的数据,这⾥设置为false
register-with-eureka: false #表⽰是否将⾃⼰注册到Eureka Server,默认为true.由于当前应⽤就是Eureka Server,故⽽设置为false.
#设置与Eureka Server的地址, 查询服务和注册服务都需要依赖这个地址
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
这样当服务启动之后 通过localhost:10010就能访问到eureka的服务中心页面

服务发现
添加依赖eureka client依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
添加配置信息
spring:
application:
name: product-server # 注册服务的名字
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10010/eureka # 注册中心的url
调用服务

使用DiscoveryClient来获取到对应名字的服务 instance
调用的时候通过url调用
通过RestTemplate来发起http请求