SpringCloud实战(一)-高可用的服务注册中心(Eureka)

由于工作的原因,最近特意研究了一下SpringCloud相关微服务组件的实现原理,并将各个微服务组件进行了单机搭建和伪集群搭建,本专栏的SpringBoot版本是基于2.0.3版本,SpringCloud版本是Finchley版本。

Finchley版本的官方文档如下:

http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html

一、什么是微服务(Microservice)

微服务英文名称Microservice,Microservice架构模式就是将整个Web应用组织为一系列小的Web服务。这些小的Web服务可以独立地编译及部署,并通过各自暴露的API接口相互通讯。它们彼此相互协作,作为一个整体为用户提供功能,却可以独立地进行扩。

微服务架构需要的功能或使用场景

1:我们把整个系统根据业务拆分成几个子系统。

2:每个子系统可以部署多个应用,多个应用之间使用负载均衡。

3:需要一个服务注册中心,所有的服务都在注册中心注册,负载均衡也是通过在注册中心注册的服务来使用一定策略来实现。

4:所有的客户端都通过同一个网关地址访问后台的服务,通过路由配置,网关来判断一个URL请求由哪个服务处理。请求转发到服务上的时候也使用负载均衡。

5:服务之间有时候也需要相互访问。例如有一个用户模块,其他服务在处理一些业务的时候,要获取用户服务的用户数据。

6:需要一个断路器,及时处理服务调用时的超时和错误,防止由于其中一个服务的问题而导致整体系统的瘫痪。

7:还需要一个监控功能,监控每个服务调用花费的时间等。

二、SpringCloud项目简介

springCloud是基于SpringBoot的一整套实现微服务的框架。他提供了微服务开发所需的配置管理、服务发现、断路器、智能路由、微代理、控制总线、全局锁、决策竞选、分布式会话和集群状态管理等组件。

相关组件架构图

三、创建高可用服务注册中心(eureka server cluster)

SpringCloud支持Eureka、Zookeeper、Consul,这里我们采用Eureka作为服务注册中心。

3.1 maven依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.oal.microservice</groupId>
        <artifactId>openAiLab</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <groupId>com.oal.microservice</groupId>
    <artifactId>eureka-server</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eureka-server</name>
    <description>服务发现-server</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

3.2 配置文件

因为我只有一台机器,所以这里我们创建三台Eureka伪集群,通过不同的profiles来区分他们运行环境,这里我们可以通过maven命令启动,命令如下:

    spring-boot:run -Dspring-boot.run.profiles=eureka-serve-01

也可以通过Idea来启动伪集群,这里勾选可运行多实例,如下图: 

我们需要创建多个配置文件来区分他们不同的运行环境,相关配置如下:

application.yml

spring:
  application:
    name: eureka-server-cluster
  profiles:
    active: eureka-serve-01

application-eureka-serve-01.yml

server:
  port: 8759

spring:
  profiles: eureka-serve-01

eureka:
  instance:
    hostname: eureka-serve-01
    instance-id: eureka-serve-01
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://eureka-serve-02:8760/eureka/,http://eureka-serve-03:8761/eureka/

application-eureka-serve-02.yml

server:
  port: 8760

spring:
  profiles: eureka-serve-02

eureka:
  instance:
    hostname: eureka-serve-02
    instance-id: eureka-serve-02
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://eureka-serve-01:8759/eureka/,http://eureka-serve-03:8761/eureka/

application-eureka-serve-03.yml

server:
  port: 8761

spring:
  profiles: eureka-serve-03

eureka:
  instance:
    hostname: eureka-serve-03
    instance-id: eureka-serve-03
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://eureka-serve-01:8759/eureka/,http://eureka-serve-02:8760/eureka/

这里我们将三台Eureka伪集群通过defaultCone进行相互注册,保证高可用,我们需要注意三台伪集群各server.port、eureka.instance.hostname、eureka.instance.instance-id需要唯一,并且eureka.client.service-url.defaultZone需要三台机器需要互相注册另外两台机器才可保证高可用,图示如下:

EurekaServerApplication.java

package com.oal.microservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

同时我们需要配置本地域名解析,在C:\Windows\System32\drivers\etc\hosts中添加以下内容

    127.0.0.1 eureka-serve-01 eureka-serve-02 eureka-serve-03

添加完域名解析后,我们通过改变application.yml文件中的spirng.profiles.active参数以此启动三台机器。

启动完成后访问任意节点,这里我们访问 http://localhost:8759/,图示如下:

高可用Eureka服务端就搭建完成了。

四、创建一个服务提供者 (eureka client)

下面我们来搭建Eureka客户端,直接上代码。

4.1 manven依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.oal.microservice</groupId>
        <artifactId>openAiLab</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <groupId>com.oal.microservice</groupId>
    <artifactId>eureka-client</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eureka-client</name>
    <description>服务发现-client</description>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

4.2 配置文件(application.yml)

server:
  port: 8763

spring:
  application:
    name: service-hi

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

4.3 启动类(EurekaClientApplication.java)

@SpringBootApplication
@EnableEurekaClient
@RestController
public class EurekaClientApplication {

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

    @Value("${server.port}")
    String port;

    @RequestMapping("/hi")
    public String hi(@RequestParam(value = "name", defaultValue = "forezp") String name) {
        return "hi " + name + " ,i am from port:" + port;
    }
}

客户端启动成功后再次访问 http://localhost:8759/,图示如下:

红字是因为Eureka Server开启了保护模式,Eureka Server在运行期间,会统计心跳失败的比例在15分钟之内是否低于85%,如果出现低于的情况(在单机调试的时候很容易满足,实际在生产环境上通常是由于网络不稳定导致),Eureka Server会将当前的实例注册信息保护起来,同时提示这个警告。

此时客户端已经成功注册到服务端注册列表中,我们访问另外另个服务端节点也会发现SERVICE-HI,图示如下:

高可用Eureka集群搭建完成。

猜你喜欢

转载自blog.csdn.net/qq_19734597/article/details/90020180