SpringCloud笔记(二)高可用注册中心的实现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhoujian_Liu/article/details/85179740

目录

1、要实现的拓扑

2、eureka-sever01和eureka-sever02

3、启动服务提供者

3.1 pom.xml

3.2 application.yml

3.3 业务类

4、启动服务消费者

4.1 pom.xml 与服务提供者相同

4.2 application.yml

4.3 controller

4.4 启动类

5、模拟宕机情况

     

在微服务架构中,注册中心是十分核心的组件,可以实现服务治理。如果注册中心发生故障,就会导致整合微服务无法访问,因此应该对注册中心进行高可用的部署。

      对Eureka进行高可用的思想非常简单:就是将自己作为服务注册给其他注册中心,这样就可以形成一组互相注册的注册中心,以实现服务清单的同步,实现高可用的目的。

在之前的博客中SpringCloud笔记(一)服务注册与服务发现,搭建注册中心时,有两个参数:

 register-with-eureka: false
 fetch-registry: false

都设置成了false,因为自己当时搭建的Eureka-Server本身就是注册中心,因此就不用将自己注册给注册中心,而搭建高可用的注册中心,均需要设置成true,将注册中心当做服务注册给其他注册中心。

下面我们开始搭建一个高可用的注册中心。

1、要实现的拓扑

2、eureka-sever01和eureka-sever02

要导入的依赖在之前的博客中已经提过这里不再赘述。这里主要讲一下application.yml怎样配置。

eureka-server01的application.yml

spring:
  application:
    name: eureka-server

server:
  port: 8100

eureka:
  instance:
    hostname: 127.0.0.1

  client:
    service-url:
      defaultZone: http://${eureka.instance.hostname}:8200/eureka/

    fetch-registry: true
    register-with-eureka: true

eureka-server02的application.yml

spring:
  application:
    name: eureka-server

server:
  port: 8200

eureka:
  instance:
    hostname: 127.0.0.1

  client:
    service-url:
      defaultZone: http://${eureka.instance.hostname}:8100/eureka/

    fetch-registry: true
    register-with-eureka: true

在这里要注意一点:两个eureka-server的名称必须起的相同,并在注意在eureka-sever01的application.yml中的service-url配置的是eureka-server02的的地址,同理在eureka-sever02的application.yml中的service-url配置的是eureka-server01的的地址,这样进行相互注册。

启动类:

@SpringBootApplication
@EnableEurekaServer
public class EurekasServer01Application {

    public static void main(String[] args) {
        SpringApplication.run(EurekasServer01Application.class, args);
    }
}



@SpringBootApplication
@EnableEurekaServer
public class EurekasServer02Application {

    public static void main(String[] args) {
        SpringApplication.run(EurekasServer02Application.class, args);
    }
}

将两个eureka-server均启动后,等待一段时间(同步数据),再查看,如下:

这样实现了注册中心的高可用。

3、启动服务提供者

3.1 pom.xml

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

3.2 application.yml

spring:
  application:
    name: app-producer
server:
  port: 8001
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8100/eureka/,http://localhost:8200/eureka/

    register-with-eureka: true
    fetch-registry: true

在defaultZone中注册eureka集群的地址。

3.3 业务类

@SpringBootApplication
@RestController
@EnableEurekaClient
public class ProducerApplication {

    @RequestMapping("/getMember")
    public String getMember() {
        return "调用会员服务成功!";
    }

    public static void main(String[] args) {
        SpringApplication.run(ProducerApplication.class, args);
    }
}

为了方便,将controller与启动类写在了一起。

4、启动服务消费者

4.1 pom.xml 与服务提供者相同

4.2 application.yml

spring:
  application:
    name: app-consumer
server:
  port: 8002
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8100/eureka/,http://localhost:8200/eureka/
    fetch-registry: true
    register-with-eureka: true

4.3 controller

@RestController
public class ConsumerController {

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/getOrder")
    public String getOrder() {
        //调用生产者的服务 PRC远程服务调用
        String serviceUrl = "http://app-producer/getMember";
        //在注册中心根据app-producer转换为对应的IP地址,底层调用httpClient发起调用
        String result = restTemplate.getForObject(serviceUrl, String.class);
        return result;
    }

}

4.4 启动类

@SpringBootApplication
@EnableEurekaClient
public class ConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

当服务提供者和服务消费者都启动后,注册中心集群中会选举其中之一当做主的注册中心,而另一个当做备用。

如下,选择的eureka-server02当做了主注册中心,将服务优先注册在eureka-server02上。

而eureka-sever01就被当做从注册中心,上面只维持了和eureka-server02的连接。

这时访问http://localhost:8002/getOrder,通过consumer远程调用producer的服务:

5、模拟宕机情况

现在我们来模拟主机eureka-server02宕机的情况,看会发生什么?

现在将eureka-server02停掉:

经过一段时间,eureka-sever01就从备变为主,并且将原来eureka-server02上面的数据全部同步了过来:

再次访问:

因此:虽然有两个注册中心,但在使用的时候是主从方式,先选择一个主注册中心存储数据,当主宕机时,将数据会同步到从服务器,(需要一会儿时间才能完成同步)。

项目源码:https://gitee.com/liuzhoujian/springcloud-eureka-cluster

猜你喜欢

转载自blog.csdn.net/zhoujian_Liu/article/details/85179740