SpringCloud踩坑笔记 | 简单的注册中心集群

 上次介绍了入门的《SpringCloud踩坑笔记|注册与发现》,今天我们将上次的代码整合一下,创建一个新的工程。

#1、创建父级maven工程,删除src目录,导入依赖
pom


    org.springframework.boot
    spring-boot-starter-parent
    2.0.3.RELEASE
    



    
        org.springframework.boot
        spring-boot-starter
    
    
        org.springframework.boot
        spring-boot-starter-web
    



    
        
            org.springframework.cloud
            spring-cloud-dependencies
            Finchley.RELEASE
            pom
            import
        
    


#2、创建注册中心子工程,导入依赖

    
        org.springframework.cloud
        spring-cloud-starter-eureka-server
        1.4.6.RELEASE
    


//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、创建服务提供者子工程,导入依赖

    
        org.springframework.boot
        spring-boot-starter-actuator
    
    
        org.springframework.cloud
        spring-cloud-starter-eureka
        1.3.1.RELEASE
    


#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 instanceList = discoveryClient.getInstances("springcloud-client");
        for (ServiceInstance instance : instanceList) {
            System.out.println(
                    instance.getHost()+"\t"+
                            instance.getPort()+"\t"+
                            instance.getUri()+"\t"+
                            instance.getServiceId()
            );
        }

        return services;
    }

}

 到此就整合好了,开始做注册中心集群,这里的例子使用的是本地集群,下面的才是本文的正式内容。

一、创建新注册中心

 创建新的子工程,与现有的注册中心子工程一样,然后创建一样的启动类。那么如何让几个服务提供者和注册中心集群关联呢?

准备工作

修改C:\Windows\System32\drivers\etc目录下的hosts文件,在行尾添加如下配置,都指向127.0.0.1:

127.0.0.1       localhost7001
127.0.0.1       localhost7002
修改第一个注册中心配置即7001端口的配置
spring.application.name=eureka-server
server.port=7001

#可以将hostname换成实际域名
eureka.instance.hostname=localhost7001
#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://localhost7002:7002/eureka/
修改第二个注册中心配置即7002端口的配置
spring.application.name=eureka-server
server.port=7002

eureka.instance.hostname=localhost7002
#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://localhost7001:7001/eureka/
修改服务提供者配置
spring.application.name=eureka-client
server.port=8001
eureka.client.serviceUrl.defaultZone=http://localhost7001:7001/eureka/,http://localhost7002: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/都显示一样的效果,服务提供者都注册到了两个注册中心,如果其中一个注册中心崩溃了,另外一个依然可用。

SpringCloud踩坑笔记 | 简单的注册中心集群_第1张图片
统一首发平台为微信公众号iFillDream,搜索关注第一时间掌握最新推送

你可能感兴趣的:(笔记)