eureka1
创建maven项目
引入依赖
<dependencies> <!-- 服务注册发现Eureka--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> 编写application.yml server: port: 7001 eureka: server: #关闭服务自保 enable-self-preservation: false instance: # eureka服务端的实例名字 hostname: eureka7001.com client: # 表示是否将自己注册到Eureka Server register-with-eureka: false # 表示是否从Eureka Server获取注册的服务信息 fetch-registry: false # 设置与 Eureka server交互的地址查询服务和注册服务都需要依赖这个地址 service-url: defaultZone: http://localhost:7002/eureka/
编写主启动类
@Slf4j @SpringBootApplication @EnableEurekaServer//开启Eureka服务 public class EurekaMain7001 { public static void main(String[] args) { SpringApplication.run(EurekaMain7001.class,args); log.info("*************** Eureka 服务启动成功 端口 7001 ***********"); } }
访问
http://localhost:7001/
eureka2
创建maven项目
引入依赖
<dependencies> <!-- 服务注册发现Eureka--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
编写application.yml
server: port: 7002 eureka: server: #关闭服务自保 enable-self-preservation: false instance: # eureka服务端的实例名字 hostname: eureka7002.com client: # 表示是否将自己注册到Eureka Server register-with-eureka: false # 表示是否从Eureka Server获取注册的服务信息 fetch-registry: false # 设置与 Eureka server交互的地址查询服务和注册服务都需要依赖这个地址 service-url: defaultZone: http://localhost:7001/eureka/
编写主启动类
@Slf4j @SpringBootApplication @EnableEurekaServer//开启Eureka服务 public class EurekaMain7002 { public static void main(String[] args) { SpringApplication.run(EurekaMain7002.class,args); log.info("*************** Eureka 服务启动成功 端口 7001 ***********"); } }
访问
http://localhost:7002/
将微服务发布到eureka集群上
修改ip映射配置
修改C:\Windows\System32\drivers\etc中的hosts
#添加如下配置
127.0.0.1 eureka7001.com 127.0.0.1 eureka7002.com
修改application.yml
server: port: 8001 eureka: instance: #根据需要自己起名字 instance-id: spring-cloud-provider1-8001 client: service-url: # Eureka server 地址 defaultZone: http://localhost:7001/eureka/, http://localhost:7002/eureka/ spring: application: # 设置应用名 name: cloud-payment-provider