Eureka是一个服务管理的平台,主要是管理多个模块之间的使用。eureka分为客户端和客户端,下面我们直接使用:
1.eureka server 服务管理的使用
1. 导入相关依赖
<dependency>
<groupId>org.springframework.cloud</groupId> //这里是服务
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
2. 在springboot启动类中加入注解
@EnableEurekaServer // 开启服务中心
3.编写配置文件
spring:
application:
name: eureka-servereureka:
eureka:
client:
service-url:
defaultZone: http: //localhost:8001/eureka/
fetch-registry: false # 一些服务相关的配置不需要写进去
register-with-eureka: false # 服务器不需要将数据写进去
server:
enable-self-preservation: false # 关闭安全机制
eviction-interval-timer-in-ms: 4000 # 4秒后结束
2.客服端的使用
1. 导入相关依赖
<dependency>
<groupId>org.springframework.cloud</groupId> //这里是客户
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2.编写配置文件
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${server.port}
lease-renewal-interval-in-seconds: 5
lease-expiration-duration-in-seconds: 20
其它要使用的话也是要导入相关的客户端的依赖
我们还可以添加负载均衡来减少集群服务器的压力
1. 要在这里加上这个注解,开启负载均衡,假如有三个服务器,那就大家一人一次(默认是平均的)
2.如果不按照默认的来也可以在配置一下
order:
ribbon: # 这里就是随机使用的
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule