SpringCloud踩坑笔记 | 简单的注册中心集群
#1、创建父级maven工程,删除src目录,导入依赖
<packaging>pom</packaging>
<!--导入需要花一定的时间,请耐心等待-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
#2、创建注册中心子工程,导入依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
</dependencies>
//3、创建对应包后创建启动类
package com.ifilldram.leancloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class Eureka7001 {
public static void main(String[] args) {
SpringApplication.run(Eureka7001.class,args);
}
}
#4、添加配置文件application.properties
spring.application.name=eureka-server
server.port=7001
eureka.instance.hostname=localhost
#fetch-registry如果为false, 则表示自己为注册中心
eureka.client.fetch-registry=false
#表示是否向eureka注册中心注册自己
eureka.client.register-with-eureka=false
#5、创建服务提供者子工程,导入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.3.1.RELEASE</version>
</dependency>
</dependencies>
#6、添加配置文件application.properties
spring.application.name=eureka-client
server.port=8001
eureka.client.serviceUrl.defaultZone=http://localhost:7001/eureka/
eureka.instance.instance-id=springcloud-client
#eureka.instance.appname=iFillDream
info.compay.name=iFillDream
info.auth.name=RickSun
//7、创建对应包后创建启动类
package com.ifilldream.leancloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
//启动类
@SpringBootApplication
@EnableDiscoveryClient //服务发现
public class Client8001 {
public static void main(String[] args) {
SpringApplication.run(Client8001.class, args);
}
}
//8、创建Controller
package com.ifilldream.leancloud.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class DcController {
@Autowired
DiscoveryClient discoveryClient;
@GetMapping("/getServices")
public String dc() {
String services = "Services:" + discoveryClient.getServices();
System.out.println(services);
//获取实例,serviceId为服务提供者的spring.application.name值
List<ServiceInstance> instanceList = discoveryClient.getInstances("springcloud-client");
for (ServiceInstance instance : instanceList) {
System.out.println(
instance.getHost()+" "+
instance.getPort()+" "+
instance.getUri()+" "+
instance.getServiceId()
);
}
return services;
}
}
到此就整合好了,开始做注册中心集群,这里的例子使用的是本地集群,下面的才是本文的正式内容。
一、创建新注册中心
创建新的子工程,与现有的注册中心子工程一样,然后创建一样的启动类。那么如何让几个服务提供者和注册中心集群关联呢?
修改第一个注册中心配置即7001端口的配置
spring.application.name=eureka-server
server.port=7001
#可以将hostname换成实际域名
eureka.instance.hostname=localhost
#fetch-registry如果为false, 则表示自己为注册中心
eureka.client.fetch-registry=false
#表示是否向eureka注册中心注册自己
eureka.client.register-with-eureka=false
#eureka.client.serviceUrl.defaultZone= http://${eureka.instance.hostname}:${server.port}/eureka/
#关联另外一个注册中心
eureka.client.serviceUrl.defaultZone= http://localhost:7002/eureka/
修改第二个注册中心配置即7002端口的配置
spring.application.name=eureka-server
server.port=7002
eureka.instance.hostname=localhost
#fetch-registry如果为false, 则表示自己为注册中心
eureka.client.fetch-registry=false
#表示是否向eureka注册中心注册自己
eureka.client.register-with-eureka=false
#eureka.client.serviceUrl.defaultZone= http://${eureka.instance.hostname}:${server.port}/eureka/
#关联另外一个注册中心
eureka.client.serviceUrl.defaultZone= http://localhost:7001/eureka/
修改服务提供者配置
spring.application.name=eureka-client
server.port=8001
eureka.client.serviceUrl.defaultZone=http://localhost:7001/eureka/,http://localhost:7002/eureka/
eureka.instance.instance-id=springcloud-client
#eureka.instance.appname=iFillDream
info.compay.name=iFillDream
info.auth.name=RickSun
好了,先启动两个注册中心再启动服务提供者,此时浏览器不管输入http://localhost:7001/ 还是 http://localhost:7002/ 都显示一样的效果,服务提供者都注册到了两个注册中心,如果其中一个注册中心崩溃了,另外一个依然可用。