1. Eureka基础架构
在前一篇博客中,提出了三个思考
其实这三件事就是注册中心帮我们去完成的
-
1.消费者该如何获取服务提供者具体信息?
- 服务提供者启动时向eureka注册自己的信息
- eureka保存这些信息
- 消费者根据服务名称向eureka拉取提供者信息
-
2.如果有多个服务提供者,消费者该如何选择?
- 服务消费者利用负载均衡算法,从服务列表中挑选一个
-
3.消费者如何感知服务提供者健康状态?
- 服务提供者会每隔30秒向EurekaServer发送心跳请求,报告健康状态
- eureka会更新记录服务列表信息,心跳不正常会被剔除
- 消费者就可以拉取到最新的信息
2.springboot环境下搭建EurekaServer
①创建项目,引入Eureka服务器端依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
②编写启动类
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class,args);
}
}
③添加application.yml
server:
port: 10086
spring:
application:
name: eurekaserver
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka/
④运行启动类EurekaServerApplication ,输入http://127.0.0.1:10086
测试是否成功
3.配置一个eureka客户端
①引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
②编写application.xml
server:
port: 8081
spring:
application:
name: userservice
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka/
③编写启动类并测试
@SpringBootApplication
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
}
显示服务名则成功
源码
自己写了一个基于springboot+mybatisplus+eureka的小demo,感兴趣的可以去gitee下载
https://gitee.com/le-peng/springboot-demo-eureka/tree/master/springcloud-demo-eureka/userservice