SpringCloud 的服务注册和发现是由Eureka来完成。
1.eureka server
1.1 依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
1.2 @EnableEurekaServer 注册服务
@SpringBootApplication @EnableEurekaServer //开启服务注册中心 public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
1.3. 配置
server: port: 8761 eureka: instance: hostname: localhost client: # 表示本应用是否是向注册中心注册自己 register-with-eureka: false # 是否检索服务 fetch-registry: false
2. eureka client
2.1 依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
2.2 @EnableDiscoveryClient
@SpringBootApplication @EnableDiscoveryClient public class EurekaClientApplication { public static void main(String[] args) { SpringApplication.run(EurekaClientApplication.class, args); } }
2.3 配置
eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ # 客户端名称 instance: hostname: client-1 spring: application: # 注册到服务端名称 name: eureka-client# 客户端端口 server: port: 8081
原文地址:https://www.cnblogs.com/appleat/p/9983800.html
时间: 2024-10-04 03:33:26