文字描述
- 搭建EurekaServer
1.1 创建工程
1.2 导入坐标
1.3 配置application.yml
1.4 配置启动类
- 将服务提供者(EurekaClient)注册到EurekaServer
2.1 导入坐标
2.2 配置application.yml
2.3 配置启动类
- 服务消费者通过注册中心获取服务列表并调用
3.1 把服务消费者配置为EurekaClient(同步骤2)
3.2 注入springCloud提供的获取元数据的工具类DiscoveryClient
3.3 DiscoveryClient根据服务名称获取服务实例(包含服务的各个元数据)
3.4 根据服务实例中的ip和port进行组装路径
代码
1. 搭建EurekaServer
<!-- 导入坐标 -->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
#配置application.yml
server:
port: 8763
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false #是否将自己注册到注