• springcloudalibaba入门详细使用教程


    一、简介

    很多人可能会问,有了spring cloud这个微服务的框架,为什么又要使用spring cloud alibaba这个框架了?最重要的原因在于spring cloud中的几乎所有的组件都使用Netflix公司的产品,然后在其基础上做了一层封装。然而Netflix的服务发现组件Eureka已经停止更新;而其他的众多组件在2020年停止维护。所以急需其他的一些替代产品,也就是spring cloud alibaba,SpringCloud Alibaba是依赖SpringCloud相关的标准实现的一套微服务的架构。
    作为 Spring Cloud 体系下的新实现,Spring Cloud Alibaba 跟官方的组件或其它的第三方实现如 Netflix, Consul,Zookeeper 等对比,具备了更多的功能:在这里插入图片描述

    微服务架构的常见问题 一旦采用微服务系统架构,就势必会遇到这样几个问题:
    • 这么多小服务,如何管理他们?(服务治理 注册中心[服务注册 发现 剔除])
    • 这么多小服务,他们之间如何通讯?(restful rpc)
    • 这么多小服务,客户端怎么访问他们?(网关 GateWay)
    • 这么多小服务,一旦出现问题了,应该如何自处理?(容错)
    A–>B–>C–D—E sentinel
    • 这么多小服务,一旦出现问题了,应该如何排错? (链路追踪)
    sluth
    对于上面的问题,是任何一个微服务设计者都不能绕过去的,因此大部分的微服务产品都针对每一 个问题提供了相应的组件来解决它们

    二、SpringCloud Alibaba核心组件

    配置中心 nacos : springcloud config
    服务治理中心nacos : Eurke

    2-1、Nacos (配置中心与服务注册与发现)

    Nacos实现了服务的配置中心与服务注册发现的功能,Nacos可以通过可视化的配置降低相关的学习与维护成本,实现动态的配置管理与分环境的配置中心控制。 同时Nacos提供了基于http/RCP的服务注册与发现功能。

    2-2、Sentinel (分布式流控)

    Sentinel是面向分布式微服务架构的轻量级高可用的流控组件,以流量作为切入点,从流量控制,熔断降级,系统负载保护等维度帮助用户保证服务的稳定性。常用与实现限流、熔断降级等策略。

    2-3、RocketMQ (消息队列)/RabbitMq/kafka

    RocketMQ基于Java的高性能、高吞吐量的消息队列,在SpringCloud Alibaba生态用于实现消息驱动的业务开发,常见的消息队列有Kafka、RocketMQ、RabbitMQ等,相关的比较文档可以自行去翻阅。

    2-4、Seata (分布式事务)

    既然是微服务的产品,那么肯定会用到分布式事物。Seata就是阿里巴巴开源的一个高性能分布式事物的解决方案。

    2-5、Dubbo (RPC)

    Dubbo已经在圈内很火了,SpringCloud Alibaba基于上面提到的Nacos服务注册中心也同样整合了Dubbo。
    在这里插入图片描述

    三、为什么大家看好 Spring Cloud Alibaba

    3-1、阿里巴巴强大的技术输出能力

    阿里巴巴无疑是国内开源技术领域的最有影响力的公司之一,已经有Dubbo、Druid,FastJson等成功的开源组件,
    再加上阿里不遗余力的推广,社区发展也非常快。

    3-2、集成Dubbo,利用Dubbo在微服务领域的超高人气

    Dubbo是国内应用最广的分布式服务框架之一,基于Dubbo改造的Dubbox等也有很多公司在使用,
    Spring Cloud Alibaba对Dubbo做了比较好的集成,可以吸引不少使用Dubbo的开发者。

    3-3、云原生趋势,集成阿里云商业化组件

    云原生(Cloud Native)是今年技术领域特别热门的一个词,云原生是一种专门针对云上应用而设计的方法,用于构建和部署应用,以充分发挥云计算的优势。
    Spring Cloud Alibaba 集成了阿里云的商业化组件,可以说天然支持云原生特性。

    四、创建简单项目

    4-1、微服务环境搭建

    微服务环境搭建

    数据库:mysql
    持久层:mybatis-plus
    Springcloud:Hoxton.SR9
    SpringcludAlibaba: 2.2.6.RELEASE
    SpringBoot:2.3.2.RELEASE
    nacos:1.4.2
    最新的版本对应关系可以参考网址:
    https://github.com/alibaba/spring-cloud-alibaba

    4-2、版本依赖

    在这里插入图片描述
    在这里插入图片描述

    4-3、组件版本关系

    每个 Spring Cloud Alibaba 版本及其自身所适配的各组件对应版本如下表所示(注意,Spring Cloud Dubbo 从 2021.0.1.0 起已被移除出主干,不再随主干演进)
    在这里插入图片描述

    4-4、创建项目

    • 先创建springboot
    • 在springboot下创建maven
      在这里插入图片描述

    在这里插入图片描述

    4-4-1 导入springboot的jar包

    
    <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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
        <packaging>pompackaging>
        <modules>
    
            <module>sys-ordermodule>
            <module>sys-promodule>
            <module>sys-commmodule>
        modules>
        <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>2.4.2version>
            <relativePath/> 
        parent>
        <groupId>com.examplegroupId>
        <artifactId>demoartifactId>
        <version>0.0.1-SNAPSHOTversion>
        <name>cloudalibabaname>
        <description>Demo project for Spring Bootdescription>
        
        <properties>
            <java.version>1.8java.version>
            <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
    
            <spring-cloud.version>2020.0.1spring-cloud.version>
            <spring-cloud-alibaba.version>2021.1spring-cloud-alibaba.version>
        properties>
    
    
    
    
    
    
    
    
    
    
    
    
    
        
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloudgroupId>
                    <artifactId>spring-cloud-dependenciesartifactId>
                    <version>${spring-cloud.version}version>
                    <type>pomtype>
                    <scope>importscope>
                dependency>
                <dependency>
                    <groupId>com.alibaba.cloudgroupId>
                    <artifactId>spring-cloud-alibaba-dependenciesartifactId>
                    <version>${spring-cloud-alibaba.version}version>
                    <type>pomtype>
                    <scope>importscope>
                dependency>
            dependencies>
        dependencyManagement>
    
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                    <configuration>
                        <image>
                            <builder>paketobuildpacks/builder-jammy-base:latestbuilder>
                        image>
                    configuration>
                plugin>
            plugins>
        build>
    
    project>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81

    4-4-2 创建maven以及导入jar包

    
    
    <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>demoartifactId>
            <groupId>com.examplegroupId>
            <version>0.0.1-SNAPSHOTversion>
        parent>
        <modelVersion>4.0.0modelVersion>
    
        <artifactId>sys-proartifactId>
    
        <name>sys-proname>
        
        <url>http://www.example.comurl>
    
        <properties>
            <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
            <maven.compiler.source>1.7maven.compiler.source>
            <maven.compiler.target>1.7maven.compiler.target>
        properties>
    
        <dependencies>
            <dependency>
                <groupId>junitgroupId>
                <artifactId>junitartifactId>
                <version>4.11version>
                <scope>testscope>
            dependency>
    
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
    
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
            dependency>
    
    
        dependencies>
    
    project>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    4-4-3 运行App

    package com.example;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    /**
     * Hello world!
     *
     */
    @SpringBootApplication
    public class ProApp
    {
        public static void main( String[] args )
        {
            System.out.println( "Hello World!" );
            SpringApplication.run(ProApp.class );
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    4-4-4 创建实例项目

    entity

    package com.example.entity;
    
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class Product {
        private Integer id;
        private String name;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    controller

    package com.example.controller;
    
    import com.example.entity.Product;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class Procontroller {
    
        @GetMapping("/pro/{id}")
        public Product  getProById(@PathVariable Integer id) {
            return new Product(1,"test"+id);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    在这里插入图片描述

    在这里插入图片描述

    五、创建公共类存放

    5-1、创建sys-comm作为公共类

    在这里插入图片描述

        <dependencies>
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
            dependency>
        dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    package com.example.entity;
    
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class Product {
        private Integer id;
        private String name;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    5-2、sys-order

    pom.xml 需要引入sys-comm公共类

      <dependencies>
        <dependency>
          <groupId>junitgroupId>
          <artifactId>junitartifactId>
          <version>4.11version>
          <scope>testscope>
        dependency>
    
        <dependency>
          <groupId>org.springframework.bootgroupId>
          <artifactId>spring-boot-starter-webartifactId>
          <version>2.4.2version>
        dependency>
    
        <dependency>
          <groupId>com.examplegroupId>
          <artifactId>sys-commartifactId>
          <version>0.0.1-SNAPSHOTversion>
        dependency>
      dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    controller

    package com.example.controller;
    
    import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    
    import javax.annotation.Resource;
    
    @RestController
    public class OrderController {
    
        // 远程调用
        @Resource
        private RestTemplate restTemplate;
    
        public  Object addOrder(@PathVariable Integer pid) {
            KafkaProperties.Producer forObject= restTemplate.getForObject("http://localhost:8082/pro/2"+pid, KafkaProperties.Producer.class);
            return  forObject;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    App

    package com.example;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.Bean;
    import org.springframework.web.client.RestTemplate;
    
    /**
     * Hello world!
     *
     */
    @SpringBootApplication
    public class OrderApp
    {
        public static void main( String[] args )
        {
            System.out.println( "Hello World!" );
            SpringApplication.run(OrderApp.class);
        }
    
        @Bean
        public RestTemplate getRestTemplate(){
            return new RestTemplate();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    在这里插入图片描述

    5-3、sys-pro

    pom.xml 需要引入sys-comm公共类

    
        <dependencies>
            <dependency>
                <groupId>junitgroupId>
                <artifactId>junitartifactId>
                <version>4.11version>
                <scope>testscope>
            dependency>
    
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
    
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
            dependency>
    
    
            <dependency>
                <groupId>com.examplegroupId>
                <artifactId>sys-commartifactId>
                <version>0.0.1-SNAPSHOTversion>
            dependency>
        dependencies>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    controller

    package com.example.controller;
    
    import com.example.entity.Product;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class Procontroller {
    
        @GetMapping("/pro/{id}")
        public Product  getProById(@PathVariable Integer id) {
            return new Product(1,"test"+id);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    App

    package com.example;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    /**
     * Hello world!
     *
     */
    @SpringBootApplication
    public class ProApp
    {
        public static void main( String[] args )
        {
            System.out.println( "Hello World!" );
            SpringApplication.run(ProApp.class );
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    在这里插入图片描述

  • 相关阅读:
    C++继承和派生的基本概念
    台湾SSS鑫创SSS1700替代Cmedia CM6533 24bit 96KHZ USB音频编解码芯片
    js刷题常用基础函数&常用快捷键
    C语言重点突破(3)字符串函数与内存函数的介绍
    Paxos分布式共识算法
    数据结构之初始泛型
    DXP TreeList 目录树
    Android源码编译原生模拟器
    Java并发 | 02.CPU与内核、并发与并行、上下文切换
    什么是市场流动性?
  • 原文地址:https://blog.csdn.net/weixin_54546701/article/details/134376501