第一 新建项目
创建注册中心
cloud-eureka-server7001 cloud-eureka-server7002
改写pom文件
要想集群在pom文件中加入 eureka依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependencies>
<!-- eureka服务注册-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>com.zzy.springcoud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
写yml文件
register-with-eureka:false表示是否向eureka注册中心注册自己
fetch-register :false 表示等于false则自己为注册中心
service-url 表示绑定其他的注册中心
server:
port: 7001
#eyreka配置
eureka:
instance:
hostname: eureka7001.com # eureka服务端的实例名字
client:
register-with-eureka: false #表示是否向eureka注册中心注册自己(不要自己注册自己)
fetch-registry: false #如果为false 则自己为注册中心
service-url: #监控页面 单机 集群
defaultZone: http://eureka7002.com:7002/eureka/
编写启动类
在主启动类头部启动eurekaserver注册中心@EnableEurekaServer 是用来区分注册中心和服务提供者的差别
package com.zzy.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer //表示7001来管理服务注册中心
public class EurekaMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7001.class,args);
}
}
cloud-eureka-server7002同7001一样配置
服务提供者如何绑定自己到注册中心
准备工作 创建(服务提供者) cloud-provider-payment8001 cloud-provider-payment8002
在pom文件中加入
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
写yml
defaultZone :注册中心的地址
eureka:
client:
register-with-eureka: true #是否将自己注册到eureka的注册中心去 默认是true
#是否从eureka server 抓取自己的注册信息 默认是ture 单节点无所谓 集群的时候 必须用
fetchRegistry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
主启动类
在主启动类 加入注册@EnableEurekaClient