1.微服的注册Eureka以及发现
Eureka基本机构主要包括以下3个⻆⾊:
- Eureka Server:服务注册中⼼
- Provider Service:服务提供者
- Consumer Service:服务消费
服务注册中心:
1.POM文件依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
2.yml文件配置
server:
port: 9001 #eureka注册端口
spring:
application:
name: bill-eureka
#eureka配置
eureka:
client:
service-url:
# eureka 服务地址,如果是集群的话;需要指定其它集群eureka地址
defaultZone: http://localhost:9001/eureka
# 不注册⾃⼰
register-with-eureka: false #因为是注册中心
# 不拉取服务
fetch-registry: false #因为是注册中心
server:
enable-self-preservation: false # 关闭⾃我保护模式(缺省为打开)
eviction-interval-timer-in-ms: 1000 # 扫描失效服务的间隔时间(缺省为60*1000ms)
#默认情况下,Eureka Server的⾃我保护模式是开启的ture,⽣产环境下这很有效,保证了⼤多
数服务依然可⽤,但是这给我们的开发带来了麻烦, 因此开发阶段我们都会关闭⾃我保护模式
instance:
# ⼼跳间隔时间
lease-renewal-interval-in-seconds: 30
# 没收到⼼跳多⻓时间剔除
lease-expiration-duration-in-seconds: 90
3.启动器的注解 注意: @EnableEurekaServer,声明当前应⽤为Eureka Server
服务的提供者与消费者
一般情况下,服务的提供者既是提供者,同时也是消费者,所以配置基本一致,只是 在controller层消费者调用提供者
1.POM依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
2.配置文件
服务提供者(spring-bill的yml文件的配置
server:
port: ${port:10000}
spring:
application:
name: spring-bill
eureka:
client:
#eureka服务地址
service-url:
defaultZone: http://localhost:9001/eureka
#默认注册自己
fetch-registry: true #可以省略 默认为true
#默认可以拉取
register-with-eureka: true
消费者(spring-customer)的yml文件的配置
server:
port: 10005
spring:
application:
name: spring-customer
eureka:
client:
service-url:
defaultZone: http://localhost:9001/eureka
fetch-registry: true
register-with-eureka: true
3.启动器的注解 在服务的提供者与消费者启动器添加注解 注意: @EnableDiscoveryClient,表示注册到eureka中
测试结果 访问 http://localhost:9001/