十六、springcloud(二)Eureka集群

1、创建子工程spring-cloud-peer(jar)

2、创建application-peer1.properties,application-peer2.properties

  application-peer1.properties

spring.application.name=spring-cloud-eureka

server.port=
#是否将自己注册到Eureka Server
#eureka.client.register-with-eureka=false
#是否从Eureka Server获取注册信息
#eureka.client.fetch-registry=false eureka.instance.hostname=peer1
eureka.client.serviceUrl.defaultZone=http://peer2:8001/eureka/

  

application-peer2.properties

spring.application.name=spring-cloud-eureka

server.port=
#是否将自己注册到Eureka Server
#eureka.client.register-with-eureka=false
#是否从Eureka Server获取注册信息
#eureka.client.fetch-registry=false eureka.instance.hostname=peer2
eureka.client.serviceUrl.defaultZone=http://peer1:8000/eureka/

3、创建main方法

package com.lofty.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication
@EnableEurekaServer
public class SpringCloudEurekaApplication { public static void main(String[] args) {
SpringApplication.run(SpringCloudEurekaApplication.class, args);
}
}

4、host转换

127.0.0.1 peer1
127.0.0.1 peer2

5、打包启动

java -jar spring-cloud-eureka-peer.jar --spring.profiles.active=peer1
java -jar spring-cloud-eureka-peer.jar --spring.profiles.active=peer2

6、http://localhost:8000/

7、多节点集群

  application.yml

---
spring:
application:
name: spring-cloud-eureka
profiles: peer1
server:
port: 8000
eureka:
instance:
hostname: peer1
client:
serviceUrl:
defaultZone: http://peer2:8001/eureka/,http://peer3:8002/eureka/
---
spring:
application:
name: spring-cloud-eureka
profiles: peer2
server:
port: 8001
eureka:
instance:
hostname: peer2
client:
serviceUrl:
defaultZone: http://peer1:8000/eureka/,http://peer3:8002/eureka/
---
spring:
application:
name: spring-cloud-eureka
profiles: peer3
server:
port: 8002
eureka:
instance:
hostname: peer3
client:
serviceUrl:
defaultZone: http://peer1:8000/eureka/,http://peer2:8001/eureka/

分别以peer1、peer2、peer3的配置参数启动eureka注册中心。

java -jar spring-cloud-eureka.jar --spring.profiles.active=peer1
java -jar spring-cloud-eureka.jar --spring.profiles.active=peer2
java -jar spring-cloud-eureka.jar --spring.profiles.active=peer3
上一篇:SpringBoot2.0 基础案例(09):集成JPA持久层框架,简化数据库操作


下一篇:Activity的生命周期,BACK键和HOME键生命周期