• SpringCloud之注册中心


    1.SpringCloud电商示例

    1.1.示例架构

    以常见的电商业务为例

    cloud-demo:
    	- order-service  # 订单服务
        - user-service   # 用户服务
        - product-service # 商品服务
        — cloud-common # 不是应用服务,主要用于存放一些公共的内容,如pojo、baseservice这种、还有utils
    

    cloud-demo模块pom.xml

    
    <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.0modelVersion>
        <groupId>com.acxgroupId>
        <artifactId>cloud-demoartifactId>
        <packaging>pompackaging>
        <version>1.0.0version>
        <modules>
            <module>cloud-commonmodule>
            <module>order-servicemodule>
            <module>product-servicemodule>
            <module>user-servicemodule>
        modules>
    
        <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>2.1.4.RELEASEversion>
        parent>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloudgroupId>
                    <artifactId>spring-cloud-dependenciesartifactId>
                    <version>Greenwich.SR1version>
                    <type>pomtype>
                    <scope>importscope>
                dependency>
            dependencies>
        dependencyManagement>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
        dependencies>
    project>
    

    cloud-common模块pom.xml:

    
    <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">
        <parent>
            <artifactId>cloud-demoartifactId>
            <groupId>com.acxgroupId>
            <version>1.0.0version>
        parent>
        <modelVersion>4.0.0modelVersion>
    
        <groupId>com.acxgroupId>
        <artifactId>cloud-commonartifactId>
        <version>1.0.0version>
        <packaging>jarpackaging>
    
        <properties>
            <lombok.version>1.18.20lombok.version>
            <druid.version>1.1.10druid.version>
            <mybatis.plus.version>3.2.0mybatis.plus.version>
            <redis.version>1.4.1.RELEASEredis.version>
            <fastjson.version>1.2.15fastjson.version>
        properties>
    
        <dependencies>
            
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
                <version>${lombok.version}version>
            dependency>
            
            <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>fastjsonartifactId>
                <version>${fastjson.version}version>
            dependency>
            
            <dependency>
                <groupId>com.baomidougroupId>
                <artifactId>mybatis-plus-boot-starterartifactId>
                <version>${mybatis.plus.version}version>
            dependency>
            
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <scope>runtimescope>
            dependency>
            
            <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>druid-spring-boot-starterartifactId>
                <version>${druid.version}version>
            dependency>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-redisartifactId>
                <version>${redis.version}version>
            dependency>
        dependencies>
    project>
    

    user-service模块pom.xml:

    
    <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">
        <parent>
            <artifactId>cloud-demoartifactId>
            <groupId>com.acxgroupId>
            <version>1.0.0version>
        parent>
        <modelVersion>4.0.0modelVersion>
    
        <groupId>com.acxgroupId>
        <artifactId>user-serviceartifactId>
        <version>1.0.0version>
        <packaging>jarpackaging>
    
        <dependencies>
            <dependency>
                <groupId>com.acxgroupId>
                <artifactId>cloud-commonartifactId>
                <version>1.0.0version>
            dependency>
        dependencies>
    project>
    

    user-service配置文件application.yml

    server:
      port: 8083
    spring:
      application:
        name: user-service
      datasource:
        driver-class-name: com.mysql.jdbc.Driver
        username: root
        password: 123456
        url: jdbc:mysql://localhost:3306/mc_mall?useSSL=false&useUnicode=true&characterEncoding=utf-8
        type: com.alibaba.druid.pool.DruidDataSource
        initialSize: 50   #初始化时建立物理连接的个数
        minIdle: 30    #最小连接池数量
        maxActive: 200  #最大连接池数量
        maxWait: 60000    #获取连接时最大等待时间
        timeBetweenEvictionRunsMillis: 60000    #Destory线程检测连接的间隔时间
        minEvictableIdleTimeMillis: 300000    #连接保持空闲而不被驱逐的最长时间
      redis:
        host: localhost
        port: 6379
        password: 123456
    

    product-service模块pom.xml:

    
    <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">
        <parent>
            <artifactId>cloud-demoartifactId>
            <groupId>com.acxgroupId>
            <version>1.0.0version>
        parent>
        <modelVersion>4.0.0modelVersion>
    
        <groupId>com.acxgroupId>
        <artifactId>product-serviceartifactId>
        <version>1.0.0version>
        <packaging>jarpackaging>
    
        <dependencies>
            <dependency>
                <groupId>com.acxgroupId>
                <artifactId>cloud-commonartifactId>
                <version>1.0.0version>
            dependency>
        dependencies>
    project>
    

    product-service配置文件application.yml

    server:
      port: 8082
    spring:
      application:
        name: product-service
      datasource:
        driver-class-name: com.mysql.jdbc.Driver
        username: root
        password: 123456
        url: jdbc:mysql://localhost:3306/mc_mall?useSSL=false&useUnicode=true&characterEncoding=utf-8
        type: com.alibaba.druid.pool.DruidDataSource
        initialSize: 50   #初始化时建立物理连接的个数
        minIdle: 30    #最小连接池数量
        maxActive: 200  #最大连接池数量
        maxWait: 60000    #获取连接时最大等待时间
        timeBetweenEvictionRunsMillis: 60000    #Destory线程检测连接的间隔时间
        minEvictableIdleTimeMillis: 300000    #连接保持空闲而不被驱逐的最长时间
      redis:
        host: localhost
        port: 6379
        password: 123456
    

    order-service模块pom.xml:

    
    <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">
        <parent>
            <artifactId>cloud-demoartifactId>
            <groupId>com.acxgroupId>
            <version>1.0.0version>
        parent>
        <modelVersion>4.0.0modelVersion>
    
        <groupId>com.acxgroupId>
        <artifactId>order-serviceartifactId>
        <version>1.0.0version>
        <packaging>jarpackaging>
    
        <dependencies>
            <dependency>
                <groupId>com.acxgroupId>
                <artifactId>cloud-commonartifactId>
                <version>1.0.0version>
            dependency>
        dependencies>
    project>
    

    order-service配置文件application.yml

    server:
      port: 8081
    spring:
      application:
        name: order-service
      datasource:
        driver-class-name: com.mysql.jdbc.Driver
        username: root
        password: 123456
        url: jdbc:mysql://localhost:3306/mc_mall?useSSL=false&useUnicode=true&characterEncoding=utf-8
        type: com.alibaba.druid.pool.DruidDataSource
        initialSize: 50   #初始化时建立物理连接的个数
        minIdle: 30    #最小连接池数量
        maxActive: 200  #最大连接池数量
        maxWait: 60000    #获取连接时最大等待时间
        timeBetweenEvictionRunsMillis: 60000    #Destory线程检测连接的间隔时间
        minEvictableIdleTimeMillis: 300000    #连接保持空闲而不被驱逐的最长时间
      redis:
        host: localhost
        port: 6379
        password: 123456
    
    1.2.Spring Cloud与Spring Boot

    关系:Spring Boot是Spring Cloud中必不可少的一部分,Springboot是微服务的基础实现技术。

    Spring Boot和Spring Cloud版本对应表:也可自行去官网查看

    Release TrainBoot Version
    2021.0.x aka Jubilee2.6.x
    2020.0.x aka Ilford2.4.x, 2.5.x (Starting with 2020.0.3)
    Hoxton2.2.x, 2.3.x (Starting with SR5)
    Greenwich2.1.x
    Finchley2.0.x
    Edgware1.5.x
    Dalston1.5.x

    2.Eureka注册中心

    2.1.Eureka Server
    • Eureka2.X版本已经停止更新维护了,但是Eureka1.X版本还是一直在进行更新维护的

    步骤一:新建eureka-service服务,并引入Eureka Server的pom依赖

    
    <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">
        <parent>
            <artifactId>cloud-demoartifactId>
            <groupId>com.acxgroupId>
            <version>1.0.0version>
        parent>
        <modelVersion>4.0.0modelVersion>
    
        <groupId>com.acxgroupId>
        <artifactId>eureka-serviceartifactId>
        <version>1.0.0version>
        <packaging>jarpackaging>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
            dependency>
        dependencies>
    project>
    

    步骤二:eureka-service服务启动类上面加上@EnableEurekaServer标签开启eureka-server服务

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

    步骤三:编写eureka配置application.yml

    server:
      port: 10081
    spring:
      application:
        name: eureka-service
    eureka:
      client:
        service-url:
          defaultZone: http://127.0.0.1:10081/eureka/
        register-with-eureka: false #是否在注册中心注册自己
        fetch-registry: false #是否检索拉取服务列表
    
    2.2.Eureka Client

    步骤一:以order-service服务为例pom文件引入eureka-client依赖,后面依次配置product和user服务即可

    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
    dependency>
    

    步骤二:编写eureka相关的配置文件

    eureka:
      client:
        fetch-registry: true
        register-with-eureka: true
        service-url:
          defaultZone: http://127.0.0.1:10081/eureka/
    
    Eureka控制台查看服务注册详情
    • 游览器输入localhost:100081直接访问euraka控制台,发现order、product、user三个服务注册成功

    在这里插入图片描述

    2.3.Eureka保活机制
    • 每一个eureka-client客户端服务都会定时的想eureka-server注册中心发送一个心跳包进行保活,服务端会通过心跳包来实现对客户端活跃状态进行监控。
  • 相关阅读:
    vue2使用 relation-graph 展示关系图谱
    火星车初级java面向对象
    c++并行与并发
    “智”造未来,江西同为科技(TOWE)参展第四届广州“两交会”圆满落幕
    服务器数据恢复-EMC存储磁盘损坏的RAID5数据恢复案例
    星邺汇捷实习工作日报(持续更新ing)
    分库分表概念、使用场景、带来的问题
    C++——list(2)
    布隆过滤器浅析
    STC8H开发(十一): GPIO单线驱动多个DS18B20数字温度计
  • 原文地址:https://blog.csdn.net/weixin_43054590/article/details/127097909