future.png
珠玑随笔

联系:

future.png

版权 © 2022 by Hsu

技术: Gatsby

样式: Bulma

许可: CC BY NC SA 4.0

Spring Cloud 搭建 Eurake 服务发现

2022年01月13日

Hsu

1分钟

背景

Eurake 是希腊语, 它的意义是发现.

在 Spring Cloud 微服务架构中, Eurake 的作用是提供一个注册中心, 每个微服务都需要从 Eurake 注册自己, 并且微服务之间的进行通讯也是通过 Eurake 来识别对方.

Eurake Server

创建一个空项目, 引入以下依赖.

  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  </dependency>

启动类添加 @EnableEurekaServer

@SpringBootApplication
@EnableEurekaServer
public class DiscoveryServiceApplication {

  public static void main(String[] args) {
    SpringApplication.run(DiscoveryServiceApplication.class, args);
  }
}

配置

spring.application.name=eureka-server
server.port=8761

# 单节点禁止注册自己 集群需要打开
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

Eurake Client

每个项目, 引入以下依赖.

  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  </dependency>

配置

eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka

参考

https://cloud.spring.io/spring-cloud-netflix/reference/html/

https://cloud.spring.io/spring-cloud-netflix/multi/multi__service_discovery_eureka_clients.html

https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-eureka-server.html

https://spring.io/guides/gs/service-registration-and-discovery/