zoukankan      html  css  js  c++  java
  • 搭建高可用服务注册中心-Spring Cloud学习第一天(非原创)

    文章大纲

    一、Spring Cloud基础知识介绍
    二、创建单一的服务注册中心
    三、创建一个服务提供者
    四、搭建高可用服务注册中心
    五、项目源码与参考资料下载
    六、参考文章

     

    一、Spring Cloud基础知识介绍

    https://www.cnblogs.com/WUXIAOCHANG/p/10888500.html

    二、创建单一的服务注册中心

    1. 简介

    Spring Cloud是基于Spring Boot的基础进行的,所以我们需要创建一个普通的Spring Boot工程,命名为eureka-server

    2. idea创建基础的Spring Boot项目

     
     

    创建后项目结构如下:

     

    3. pom.xml添加相关依赖

    目前eureka的稳定版本是Dalston.SR3,下面添加了Spring Boot和Spring Cloud相关依赖

    <?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>
    
        <groupId>com.wxc</groupId>
        <artifactId>eureka-server</artifactId>
        <version>1.0-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>eureka-server</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.6.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka-server</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Dalston.SR3</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </project>
    

    4. 新增相关静态资源文件

     

    其中static和templates文件夹为空的,其中application.properties文件内容如下:

    #单机下配置
    #1.server.port=1111表示设置该服务注册中心的端口号
    #2.eureka.instance.hostname=localhost表示设置该服务注册中心的hostname
    #3.eureka.client.register-with-eureka=false,由于我们目前创建的应用是一个服务注册中心,而不是普通的应用,
    # 默认情况下,这个应用会向注册中心(也是它自己)注册它自己,设置为false表示禁止这种默认行为
    #4.eureka.client.fetch-registry=false,表示不去检索其他的服务,因为服务注册中心本身的职责就是维护服务实例,它也不需要去检索其他服务
    server.port=1111
    eureka.instance.hostname=localhost
    eureka.client.register-with-eureka=false
    eureka.client.fetch-registry=false
    eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
    

    温馨提示
    (1)application.properties文件中配置了服务注册中心的信息
    (2)具体服务注册中心信息如下:
      1)server.port=1111表示设置该服务注册中心的端口号
      2)eureka.instance.hostname=localhost表示设置该服务注册中心的hostname
      3)eureka.client.register-with-eureka=false,由于我们目前创建的应用是一个服务注册中心,而不是普通的应用,默认情况下,这个应用会向注册中心(也是它自己)注册它自己,设置为false表示禁止这种默认行为
      4)eureka.client.fetch-registry=false,表示不去检索其他的服务,因为服务注册中心本身的职责就是维护服务实例,它也不需要去检索其他服务

    5. 添加项目启动入口

    在main下新建com.wxc.test包,并新建入口类EurekaServerApplication.java

     

    EurekaServerApplication.java内容如下:

    package com.wxc.test;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    
    @EnableEurekaServer
    @SpringBootApplication
    public class EurekaServerApplication {
        public static void main(String[] args) {
            SpringApplication.run(EurekaServerApplication.class, args);
        }
    }
    

    6. 运行项目并访问

    6.1 运行项目

     

    6.2 运行结果

     

    可以看到,运行成功了。

    6.3 项目访问
    浏览器输入http://localhost:1111,访问情况如下:

     

    此时代表服务注册中心启动并访问成功

    三、创建一个服务提供者

    1. idea创建基础的Spring Boot项目

    创建名为eureka-provider的Spring Boot项目

     
     
     
     

    创建后项目结构如下;

     

    2. pom.xml添加相关依赖

    <?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>
        <groupId>org.sang</groupId>
        <artifactId>provider</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
        <name>provider</name>
        <description>Demo project for Spring Boot</description>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.6.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
        <dependencies>
            <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>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
            </dependency>
        </dependencies>
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Dalston.SR3</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </project>
    

    3. 新增相关静态资源文件

     

    application.properties文件内容如下:

    server.port=8080
    #配置一下服务名和注册中心地址
    spring.application.name=hello-service
    #注册单个服务
    eureka.client.service-url.defaultZone=http://localhost:1111/eureka
    

    4. Controller添加访问入口

    在com.wxc.test包下新建HelloController.java

    package com.wxc.test.controller;
    
    
    import org.apache.log4j.Logger;
    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.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.List;
    
    @RestController
    public class HelloController {
    
        private final Logger logger = Logger.getLogger(getClass());
    
        @Autowired
        private DiscoveryClient discoveryClient;
    
        @RequestMapping(value = "/hello", method = RequestMethod.GET)
        public String index() {
            List<ServiceInstance> instances = discoveryClient.getInstances("hello-service");
            for (int i = 0; i < instances.size(); i++) {
                logger.info("/hello,host:" + instances.get(i).getHost() + ",service_id:" + instances.get(i).getServiceId());
            }
            return "Hello World";
        }
    }
    

    这里创建服务之后,在日志中将服务相关的信息打印出来,创建后项目结构如下:

     

    5. 添加项目启动入口

    在com.wxc.test包下新建ProviderApplication.java,在Spring Boot的入口函数处,通过添加@EnableDiscoveryClient注解来激活Eureka中的DiscoveryClient实现(因为我们在HelloController中注入了DiscoveryClient)

    package com.wxc.test;
    
            import org.springframework.boot.SpringApplication;
            import org.springframework.boot.autoconfigure.SpringBootApplication;
            import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    
    @EnableDiscoveryClient
    @SpringBootApplication
    public class ProviderApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ProviderApplication.class, args);
        }
    }
    

    6. 运行项目并访问

    6.1 运行项目
    首先确保eureka-server注册中心处于启动,可以刷新一下原先的浏览器访问,如下代表启动成功

    在eureka-provider中进行项目启动

     
     

    6.2 项目访问
    http://localhost:1111服务注册中心中点击刷新,之后可以看到服务提供者已经注册成功

     
     

    7. 总结

    如此之后,我们一个服务注册中心就搭建成功了,同时也有一个服务提供者成功的注册了。但是这样还有一个小问题,那就是我们这里是一个单节点的服务注册中心,一旦发生了故障整个服务就瘫痪了,所以在实际应用中,我们需要搭建高可用注册中心,高可用注册中心将在第四点中进行讲解

    四、搭建高可用服务注册中心

    1. 简介

    上面搭建好的服务注册中心是一个单节点的服务注册中心,这样一旦发生了故障,那么整个服务就会瘫痪,所以我们需要一个高可用的服务注册中心,那么在Eureka中,我们通过集群来解决这个问题。Eureka Server的高可用实际上就是将自己作为服务向其他服务注册中心注册自己,这样就会形成一组互相注册的服务注册中心,进而实现服务清单的互相同步,达到高可用的效果。

    2. 服务注册中心创建与配置

    我们需要新建一个服务注册中心,项目名为:eureka-server2,具体创建方式参考二中的内容,创建后项目结构与内容如下:

     

    3. 修改配置文件

    3.1 eureka-server修改
    修改application.properties文件内容如下:

    #(1)在peer1的配置文件中,让它的service-url指向peer2,在peer2的配置文件中让它的service-url指向peer1
    #由于peer1和peer2互相指向对方,实际上我们构建了一个双节点的服务注册中心集群
    spring.application.name=eureka-server
    server.port=1111
    eureka.instance.hostname=localhost
    eureka.client.register-with-eureka=true
    eureka.client.service-url.defaultZone=http://localhost:1112/eureka/
    

    3.2 eureka-server2修改
    修改application.properties文件内容如下:

    #1.server.port=1111表示设置该服务注册中心的端口号
    #2.eureka.instance.hostname=localhost表示设置该服务注册中心的hostname
    #3.eureka.client.register-with-eureka=false,由于我们目前创建的应用是一个服务注册中心,而不是普通的应用,
    # 默认情况下,这个应用会向注册中心(也是它自己)注册它自己,设置为false表示禁止这种默认行为
    #4.eureka.client.fetch-registry=false,表示不去检索其他的服务,因为服务注册中心本身的职责就是维护服务实例,它也不需要去检索其他服务
    spring.application.name=eureka-server
    server.port=1112
    eureka.instance.hostname=localhost
    eureka.client.register-with-eureka=true
    eureka.client.service-url.defaultZone=http://localhost:1111/eureka/
    

    温馨提示
    (1)在peer1的配置文件中,让它的service-url指向peer2,在peer2的配置文件中让它的service-url指向peer1
    (2)为了让peer1和peer2能够被正确的访问到,我们需要在C:WindowsSystem32driversetc目录下的hosts文件总添加两行配置,如下:
    127.0.0.1 peer1
    127.0.0.1 peer2
    (3)由于peer1和peer2互相指向对方,实际上我们构建了一个双节点的服务注册中心集群

    4. 启动集群服务注册中心

    分别运行两个服务注册中心的启动类

     
     

    访问http://localhost:1111/http://localhost:1112/,出现以下内容

     
     

    我们可以看到,在server1的节点的我们已经可以看到server2节点了,在server2的中我们也可以看到server1节点了。如此之后,我们的服务注册中心集群就搭建好了,然后我们可以做一个简单的测试。

    5. 进行集群测试

    5.1 修改eureka_provdier中配置

     

    5.2 启动eureka_provdier项目

     
     

    5.3 访问集群注册中心

     
     

    大家可以看到两个服务注册中心都发现了服务提供者了。

    五、项目源码与参考资料下载

    链接:https://pan.baidu.com/s/1UKgV6Kz9DdguzXOzPONKJg
    提取码:gug4

    六、参考文章

    https://www.cnblogs.com/lenve/p/7985943.html

  • 相关阅读:
    算法导论第11章 散列表
    Ubuntu14.04上安装Jupyter的方法
    WinSCP连接远程的Ubuntu失败
    K-means和K-means++好的网站
    Ubuntu14.04上安装pip的方法
    算法导论第一章
    微服务架构的特点
    国内maven仓库地址 || 某个pom或者jar找不到的解决方法
    REST or RPC?
    zookeeper安装及环境变量设置
  • 原文地址:https://www.cnblogs.com/WUXIAOCHANG/p/10921761.html
Copyright © 2011-2022 走看看