Spring cloud 各个微服务使用过程中,通过Eureka进行服务的注册与发现。
一、EurekaServer 搭建
1、导入EurekaServer 所需依赖包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
2、在启动类上添加注解 @EnableEurekaServer ,并增加配置 application.yml 配置
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false #false表示不向注册中心注册自己。
fetchRegistry: false #表示自己就是注册中心,我的职责是维护服务实例,不需要去检索服务
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
EurekaServer服务端就配置完成了,接下来 就可以 访问 Eureka 地址进行查看
二、EurekaClient 服务注册
上面服务端就算搭建好了,现在我们需要在我们的被调用方中配置,引入需要的依赖包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
在 application.yml 中添加如下配置
#指定注册中心地址
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
#服务的名称
spring:
application:
name: product-service