实现eureak高可用性及建立eureak服务注册中心及服务提供者

在微服务架构这样的分布式环境中,我们需要充分的考虑发生故障的情况,所以在生产环境中我们必须对各个组件进行高可用部署。Eureka Server 的设计一开始就考虑了这个问题,在Eureka的服务治理设计中,所有的结点,既是服务提供方,也是服务消费方,服务注册中心也不例外。Eureka Server 的高可用实际上就是将自己作为服务向其他服务注册中心注册自己。这样就可以形成一组相互注册的服务注册中心,以实现服务清单的相互同步,达到高可用的效果。

具体实现方式:搭建一个双节点的服务注册中心集群

如A ,B两个注册中心,在建立时分别注册到本身,并相互注册。

实现方法一:

环境为Ubuntu IntellijIDE jdk1.8

步骤1:建立eureak-server注册中心

点击File ,New,Project,选择左侧的Spring Initializr,并配置好JDK版本(一定为jdk1.8)点击next,

默认不变,再点击next,选择右侧的Spring Cloud Discovery,对应选择左侧的Eureka Server,点击next,输入项目名,点击finish 。需要分别建立两个工程。

不需要配置pom.xml

 

在节点A和B中配置@EnableEurekaServer。

@EnableEurekaServer

@SpringBootApplication

public class DemoApplication {

 

public static void main(String[] args) {

SpringApplication.run(DemoApplication.class, args);

}

 

}

分别在图中的application.properties配置文件中进行如下配置

节点A

spring.application.name=eureka.server server.port=1111 eureka.instance.hostname=127.0.0.1 eureka.client.fetch-registry=true eureka.client.register-with-eureka=true eureka.instance.prefer-ip-address=true eureka.client.serviceUrl.defaultZone=http://127.0.0.1:1112/eureka/

节点B

spring.application.name=eureka.server server.port=1112 eureka.instance.hostname=127.0.0.1 eureka.client.fetch-registry=true eureka.client.register-with-eureka=true eureka.instance.prefer-ip-address=true eureka.client.serviceUrl.defaultZone=http://127.0.0.1:1111/eureka/

该配置允许服务注册中心注册自己,因为该配置是默认的,在实际开发过程中可以不加。进行完该配置之后,我们分别访问http://localhost:1111/http://localhost:1112/,注册中心已经相互注册。

 

 

步骤2 建立服务提供方eureka-client,将该服务注册到A节点

点击File ,New,Project,选择左侧的Spring Initializr,并配置好JDK版本(一定为jdk1.8)点击next,

默认不变,再点击next,选择右侧的Spring Cloud Discovery,对应选择左侧的Eureka Discovery Client。点击next,输入项目名,点击finish 。

配置pom.xml,添加

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

在执行main的java类中加入:

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EnableEurekaClient

@SpringBootApplication

public class DemoApplication {

 

public static void main(String[] args) {

SpringApplication.run(DemoApplication.class, args);

}

 

}

 

在DemoApplication的同级目录下,新建一个java类

package com.example.demo;

 

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RestController;

 

@RestController

public class HomeController {

// private final Logger logger = Logger.getLogger(getClass());

 

@Autowired

// private DiscoveryClient client;

 

@RequestMapping(value = "/hello",method = RequestMethod.GET)

public String index(){

// ServiceInstance instance = client.getLocalServiceInstance();

// logger.info("/hello, host:"+instance.getHost()+"service_id"+instance.getServiceId());

return "hello_world";

}

}

application.properties配置文件中进行如下配置

spring.application.name=client-service

 

server.port=8011

eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

注意由于端口名的冲突,如果所有的工程均在一台电脑上建立的,则需要保证端口的不一致性。

我们分别访问http://localhost:1111/http://localhost:1112/,可以看到,client-service同时注册到了上述的两个节点中,

若断开某一个节点,client-service仍然可以向另一个节点注册,

且client-service只注册了一个节点,由于注册节点之间相互注册,所以另一个节点也会被client-service注册。

从而实现了服务注册中心的高可用。

实现方法二:

编程环境 ubuntu jdk1.8 apache-maven-3.5.4-bin.tar.gz

首先安装jdk和maven,并配置环境变量,测试是否已成功安装。

其次下载eureak服务注册代码

 

如有三台服务器,则每台服务器均进行上述操作。

对每台服务器的application.yml文件进行如下配置

server:

port: 8761

eureka:

instance:

hostname: 192.168.1.4

# prefer-ip-address: true

client:

service-url:

defaultZone: http://192.168.1.7:8762/eureka/,http://192.168.1.3:8763/eureka/

server:

waitTimeInMsWhenSyncEmpty: 0

enable-self-preservation: false

其中enable-self-preservation: false表示关闭自我保护模式

client:

service-url:

defaultZone: http://192.168.1.7:8762/eureka/,http://192.168.1.3:8763/eureka/

表示将该服务注册中心,注册到其他的两个服务注册中心,组成集群。注意代码中的阶梯顺序一定要有。

其他两台机器,将修改端口号,主机名及注册中心的网址。

在ssh登录到服务器,进行运行时,防止终端关闭,服务自动关闭,使用如下命令进行服务的开启:

setsid mvn spring-boot:run

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章