SpringCloud笔记Eureka服务注册中心Zookeeper服务注册中心Consul服务注册中心案例详解

Posted undo_try

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringCloud笔记Eureka服务注册中心Zookeeper服务注册中心Consul服务注册中心案例详解相关的知识,希望对你有一定的参考价值。

一、简介

技术版本
Spring Cloud版本Hoxton.SR1
Spring Boot版本2.2.2RELEASE
Cloud Alibaba版本2.1.0.RELEASE

官方地址

git地址: https://github.com/spring-projects/spring-boot/releases/

Spring Boot官网: https://spring.io/projects/spring-boot

git地址: https://github.com/spring-projects/spring-cloud

Spring Boot官网: https://spring.io/projects/spring-cloud

SpringCloud的版本

Spring Cloud 采用了英国伦敦地铁站的名称来命名,并由地铁站名称字母A-Z依次类推的形式来发布迭代版本SpringCloud是一个由许多子项目组成的综合项目,各子项目有不同的发布节奏。为了管理SpringCloud与各子项目的版本依赖关系,发布了一个清单,其中包括了某个SpringCloud版本对应的子项目版本。为了避免SpringCloud版本号与子项目版本号混淆,SpringCloud版本采用了名称而非版本号的命名,这些版本的名字采用了伦敦地铁站的名字,根据字母表的顺序来对应版本时间顺序。例如Angel是第一个版本, Brixton是第二个版本。当SpringCloud的发布内容积累到临界点或者一个重大BUG被解决后,会发布一个"service releases"版本,简称SRX版本,比如Greenwich.SR2就是SpringCloud发布的Greenwich版本的第2个SRX版本。

1、SpringBoot和SpringCloud的关系

网站: https://spring.io/projects/spring-cloud#overview

更详细版本的查看地址: https://start.spring.io/actuator/info


    "git":
        "branch":"64b2829d7b37506fa4e7e5361462ac3ca91c1692",
        "commit":
            "id":"64b2829",
            "time":"2023-02-13T11:11:29Z"
        
    ,
    "build":
        "version":"0.0.1-SNAPSHOT",
        "artifact":"start-site",
        "versions":
            "spring-boot":"3.0.2",
            "initializr":"0.20.0-SNAPSHOT"
        ,
        "name":"start.spring.io website",
        "time":"2023-02-13T11:12:37.946Z",
        "group":"io.spring.start"
    ,
    "bom-ranges":
        "codecentric-spring-boot-admin":
            "2.4.3":"Spring Boot >=2.3.0.M1 and <2.5.0-M1",
            "2.5.6":"Spring Boot >=2.5.0.M1 and <2.6.0-M1",
            "2.6.8":"Spring Boot >=2.6.0.M1 and <2.7.0-M1",
            "2.7.4":"Spring Boot >=2.7.0.M1 and <3.0.0-M1",
            "3.0.0-M4":"Spring Boot >=3.0.0-M1 and <3.1.0-M1"
        ,
        "solace-spring-boot":
            "1.1.0":"Spring Boot >=2.3.0.M1 and <2.6.0-M1",
            "1.2.2":"Spring Boot >=2.6.0.M1 and <3.0.0-M1"
        ,
        "solace-spring-cloud":
            "1.1.1":"Spring Boot >=2.3.0.M1 and <2.4.0-M1",
            "2.1.0":"Spring Boot >=2.4.0.M1 and <2.6.0-M1",
            "2.3.2":"Spring Boot >=2.6.0.M1 and <3.0.0-M1"
        ,
        "spring-cloud":
            "Hoxton.SR12":"Spring Boot >=2.2.0.RELEASE and <2.4.0.M1",
            "2020.0.6":"Spring Boot >=2.4.0.M1 and <2.6.0-M1",
            "2021.0.0-M1":"Spring Boot >=2.6.0-M1 and <2.6.0-M3",
            "2021.0.0-M3":"Spring Boot >=2.6.0-M3 and <2.6.0-RC1",
            "2021.0.0-RC1":"Spring Boot >=2.6.0-RC1 and <2.6.1",
            "2021.0.5":"Spring Boot >=2.6.1 and <3.0.0-M1",
            "2022.0.0-M1":"Spring Boot >=3.0.0-M1 and <3.0.0-M2",
            "2022.0.0-M2":"Spring Boot >=3.0.0-M2 and <3.0.0-M3",
            "2022.0.0-M3":"Spring Boot >=3.0.0-M3 and <3.0.0-M4",
            "2022.0.0-M4":"Spring Boot >=3.0.0-M4 and <3.0.0-M5",
            "2022.0.0-M5":"Spring Boot >=3.0.0-M5 and <3.0.0-RC1",
            "2022.0.0-RC1":"Spring Boot >=3.0.0-RC1 and <3.0.0-RC2",
            "2022.0.0-RC2":"Spring Boot >=3.0.0-RC2 and <3.0.0",
            "2022.0.1":"Spring Boot >=3.0.0 and <3.1.0-M1"
        ,
        "spring-cloud-azure":
            "4.6.0":"Spring Boot >=2.5.0.M1 and <3.0.0-M1",
            "5.0.0":"Spring Boot >=3.0.0-M1 and <3.1.0-M1"
        ,
        "spring-cloud-gcp":
            "2.0.11":"Spring Boot >=2.4.0-M1 and <2.6.0-M1",
            "3.4.3":"Spring Boot >=2.6.0-M1 and <3.0.0-M1",
            "4.0.0":"Spring Boot >=3.0.0-M1 and <3.1.0-M1"
        ,
        "spring-cloud-services":
            "2.3.0.RELEASE":"Spring Boot >=2.3.0.RELEASE and <2.4.0-M1",
            "2.4.1":"Spring Boot >=2.4.0-M1 and <2.5.0-M1",
            "3.3.0":"Spring Boot >=2.5.0-M1 and <2.6.0-M1",
            "3.4.0":"Spring Boot >=2.6.0-M1 and <2.7.0-M1",
            "3.5.0":"Spring Boot >=2.7.0-M1 and <3.0.0-M1",
            "4.0.0":"Spring Boot >=3.0.0 and <3.1.0-M1"
        ,
        "spring-shell":
            "2.1.6":"Spring Boot >=2.7.0 and <3.0.0-M1",
            "3.0.0":"Spring Boot >=3.0.0 and <3.1.0-M1"
        ,
        "vaadin":
            "14.9.6":"Spring Boot >=2.1.0.RELEASE and <2.6.0-M1",
            "23.2.15":"Spring Boot >=2.6.0-M1 and <2.7.0-M1",
            "23.3.5":"Spring Boot >=2.7.0-M1 and <2.8.0-M1"
        ,
        "wavefront":
            "2.0.2":"Spring Boot >=2.1.0.RELEASE and <2.4.0-M1",
            "2.1.1":"Spring Boot >=2.4.0-M1 and <2.5.0-M1",
            "2.2.2":"Spring Boot >=2.5.0-M1 and <2.7.0-M1",
            "2.3.4":"Spring Boot >=2.7.0-M1 and <3.0.0-M1",
            "3.0.1":"Spring Boot >=3.0.0-M1 and <3.1.0-M1"
        
    ,
    "dependency-ranges":
        "okta":
            "1.4.0":"Spring Boot >=2.2.0.RELEASE and <2.4.0-M1",
            "1.5.1":"Spring Boot >=2.4.0-M1 and <2.4.1",
            "2.0.1":"Spring Boot >=2.4.1 and <2.5.0-M1",
            "2.1.6":"Spring Boot >=2.5.0-M1 and <3.0.0-M1",
            "3.0.2":"Spring Boot >=3.0.0-M1 and <3.1.0-M1",
            "managed":"Spring Boot >=3.1.0-M1"
        ,
        "mybatis":
            "2.1.4":"Spring Boot >=2.1.0.RELEASE and <2.5.0-M1",
            "2.2.2":"Spring Boot >=2.5.0-M1 and <2.7.0-M1",
            "2.3.0":"Spring Boot >=2.7.0-M1 and <3.0.0-M1",
            "3.0.0":"Spring Boot >=3.0.0-M1"
        ,
        "camel":
            "3.5.0":"Spring Boot >=2.3.0.M1 and <2.4.0-M1",
            "3.10.0":"Spring Boot >=2.4.0.M1 and <2.5.0-M1",
            "3.13.0":"Spring Boot >=2.5.0.M1 and <2.6.0-M1",
            "3.17.0":"Spring Boot >=2.6.0.M1 and <2.7.0-M1",
            "3.20.2":"Spring Boot >=2.7.0.M1 and <3.0.0-M1",
            "4.0.0-M1":"Spring Boot >=3.0.0-M1 and <3.1.0-M1"
        ,
        "picocli":
            "4.7.0":"Spring Boot >=2.5.0.RELEASE and <3.1.0-M1"
        ,
        "open-service-broker":
            "3.2.0":"Spring Boot >=2.3.0.M1 and <2.4.0-M1",
            "3.3.1":"Spring Boot >=2.4.0-M1 and <2.5.0-M1",
            "3.4.1":"Spring Boot >=2.5.0-M1 and <2.6.0-M1",
            "3.5.0":"Spring Boot >=2.6.0-M1 and <2.7.0-M1"
        
    

2、关于Cloud各种组件的停更/升级/替换

官网:https://cloud.spring.io/spring-cloud-static/Hoxton.SR1/reference/htmlsingle/

官网:https://docs.spring.io/spring-boot/docs/2.2.2.RELEASE/reference/htmlsingle/

Spring Cloud中文文档: https://www.bookstack.cn/read/spring-cloud-docs/docs-index.md

升级后:

3、微服务架构编码构建(cloud-provider-payment8001)

3.1 pom文件

<?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">
    <parent>
        <artifactId>springcloud</artifactId>
        <groupId>com.yyds</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-provider-payment8001</artifactId>



    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>


        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>


        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.22</version>
        </dependency>


        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>


        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        
    </dependencies>


</project>

3.2 改yml文件

server:
  port: 8001


spring:
  application:
    name: cloud-payment-service



  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/springcloud?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: root



mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.yyds.springcloud.entities

3.3 主启动

package com.yyds.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class PaymentMain8001 
    public static void main(String[] args) 
        SpringApplication.run(PaymentMain8001.class, args);
        System.out.println("*******************PaymentMain8001启动成功********************");
    


3.4 业务类

3.4.1 dao及mapper.xml

package com.yyds.springcloud.dao;

import com.yyds.springcloud.entities.Payment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

@Mapper
public interface PaymentDao 

    int create(Payment  payment);


    Payment getPaymentById(@Param("id") Long id);


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yyds.springcloud.dao.PaymentDao">

    <resultMap id="BaseResultMap" type="payment">
        <result column="id" property="id"/>
        <result column="serial" property="serial"/>
    </resultMap>

    <insert id="create" parameterType="Payment" useGeneratedKeys="true" keyProperty="id">
        insert into payment (serial) value (#serial);
    </insert>


   <select id="getPaymentById" parameterType="Long" resultMap="BaseResultMap">
       select * from payment where id = #id
   </select>

</mapper>

3.4.2 service

package com.yyds.springcloud.service;

import com.yyds.springcloud.entities.Payment;
import org.apache.ibatis.annotations.Param;

public interface PaymentService 
     int create(Payment payment);
     Payment getPaymentById(@Param("id") Long id);

package com.yyds.springcloud.service.impl;

import com.yyds.springcloud.dao.PaymentDao;
import com.yyds.springcloud.entities.Payment;
import com.yyds.springcloud.service.PaymentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class PaymentServiceImpl implements PaymentService 

    @Resource
    private PaymentDao paymentDao;

    @Override
    public int create(Payment payment) 
        return paymentDao.create(payment);
    

    @Override
    public Payment getPaymentById(Long id) 
        return paymentDao.getPaymentById(id);
    

3.4.3 controller

package com.yyds.springcloud.controller;

import com.yyds.springcloud.entities.CommonResult;
import com.yyds.springcloud.entities.Payment;
import com.yyds.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/payment")
@Slf4j
public class PaymentController 
    @Resource
    private PaymentService paymentService;

    @PostMapping("/create")
    public CommonResult<Payment> create(@RequestBody  Payment payment)
        int result = paymentService.create(payment);
        if(result > 0)
            return new CommonResult(200,"插入数据库成功",result);
        else 
            return new CommonResult(444,"插入数据库失败",null);
        
    

    @GetMapping("/get/id")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id)
        Payment payment = paymentService.getPaymentById(id);
        log.info("*****查询结果:"以上是关于SpringCloud笔记Eureka服务注册中心Zookeeper服务注册中心Consul服务注册中心案例详解的主要内容,如果未能解决你的问题,请参考以下文章

SpringCloud学习笔记-p1(服务拆分&远程调用&Eureka注册中心&Ribbon负载均衡)

springcloud笔记三注册中心eurekazookeeperconsul

SpringCloud系列服务注册中心Eureka基础

SpringCloud之eureka注册中心入门

SpringCloud--注册中心Eureka

SpringCloud Eureka(注册中心集群)