Springcloud_alibaba学习笔记

0. 简介

区别

Spring Cloud vs Spring Cloud Alibaba

Spring官网原文:

Spring Cloud Alibaba provides a one-stop solution for distributed application development. It contains all the components required to develop distributed applications, making it easy for you to develop your applications using Spring Cloud.

With Spring Cloud Alibaba, you only need to add some annotations and a small amount of configurations to connect Spring Cloud applications to the distributed solutions of Alibaba, and build a distributed application system with Alibaba middleware.

翻译:

阿里云为分布式应用开发提供了一站式解决方案。它包含了开发分布式应用程序所需的所有组件,使您可以轻松地使用springcloud开发应用程序。

有了阿里云,你只需要添加一些注解和少量的配置,就可以将Spring云应用连接到阿里的分布式解决方案上,用阿里中间件搭建一个分布式应用系统。

  • spring cloud 用来解决微服务系统中(分布式系统)解决方案
  • spring cloud alibaba 用来解决微服务系统中解决方案

特点

spring cloud alibaba 特点

  • 服务降级和流量控制(Flow Control and Service Degradation),注意服务熔断也属于服务降级:由sentinel替换hystrix
  • 服务注册与发现(Service Registration and discovery;服务注册中心):nacos替换eureka和consul
  • 分布式配置(Distributed Configuration):nacos替换config(alibaba的统一配置中心和服务注册中心和为了一个),无需手动刷新配置,nacos可以自动完成配置刷新
  • 事件驱动(Event-Driven):事件驱动利用RocketMQ替换bus
  • 消息总线(Message Bus):消息总线(异步处理)
  • 分布式事务(Distributed Transaction):Seata
  • Dubbo RPC:集成Dubbo实现服务间通信,Dubbo RPC替换RestTemplate和OpenFeign

组件

  • 服务注册与发现组件 eureka consul nacos
  • 服务间通信组件 restTemplate+ribbon,Openfeign restTemplate+ribbon,Openfeign
  • 服务降级和熔断 hystrix hystrix dashboard sentinel
  • 服务网关组件 gateway gateway
  • 统一配置中心组件 消息总线组件 config bus nacos

1. 环境搭建

维护springcloud依赖 -Hoxton.SR6

维护springcloud alibaba依赖 -2.2.1

集成springboot父项目 -2.2.5

a. 创建一个新的project (empty project)

b. 检查SDK配置版本;检查Maven配置

c. 创建父项目,并删除/src

d. 父项目依赖管理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.example</groupId>
    <artifactId>SpringCloudAlibaba_parent</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--继承springboot父项目-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
    </parent>

    <!--定义版本-->
    <properties>
        <spring.cloud-version>Hoxton.SR6</spring.cloud-version>
        <spring.cloud.alibaba.version>2.2.1.RELEASE</spring.cloud.alibaba.version>
    </properties>

    <!--维护依赖-->
    <dependencyManagement>
        <dependencies>
            <!--SpringCloud依赖-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring.cloud-version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--springcloud alibaba依赖-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring.cloud.alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

注意<type>pom</type><scope>import</scope>这个写法:虽然当前项目是我们的父项目,但还要再引入一个spring-cloud-dependencies父项目,所以采用这种写法。往常在引用依赖时写法就是:<groupId>, <artifactId><version>,这样写引入的都是jar包。而这里的写法产生的效果是:类似引入了一个pom清单(记录各种组件的版本),把spring-cloud-dependencies这个父项目的pom引入到当前pom。

2. Nacos

作用:服务注册中心(重点) + 统一配置中心。

这里主要学习Nacos作为服务注册中心的使用和细节。

a.作为服务注册中心

启动

Nacos作为服务注册中心,和Consul一样,是一款软件。所以需要安装Nacos,安装包下载地址:

https://github.com/alibaba/nacos/releases,进入后点击Asserts进行下载。

5zFBc6

在bin目录中启动nacos:

sh startup.sh -m standalone

访问http://localhost:8848/nacos,查看可视化管理页面,登陆账号密码都是谁"nacos"

zoTThm

ROdi2s

MacosBigSur版本nacos启动失败解决方法

Nacos Client简单上手

  • 创建独立springboot应用
@SpringBootApplication
@EnableDiscoveryClient  // 可省略:原来是consul server,现在是nacos server
public class NacosClientApplication {
   
    public static void main(String[] args) {
   
        SpringApplication.run(NacosClientApplication.class, args);
    }
}
  • 依赖
    • spring-boot-web
    • nacos client
    • 无需健康检查(actuator),nacos已经内部实现了
    <dependencies>
        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值