没有合适的资源?快使用搜索试试~ 我知道了~
首页springcloudalibaba.pdf
资源详情
资源评论
资源推荐

spring cloud alibaba
第一章 初识 Spring Cloud
一、微服务架构
"微服务”一词源于 Martin Fowler的名为 Microservices的博文,可以在他的官方博客上找到
http://m
artinfowler.com/articles/microservices.html
微服务是系统架构上的一种设计风格,它的主旨是将一个原本独立的系统拆分成多个小型服务,这些
小型服务都在各自独立的进程中运行,服务之间一般通过 HTTP 的 RESTfuL API 进行通信协作。
由于有了轻量级的通信协作基础,所以这些微服务可以使用不同的语言来编写。
https://blog.csdn.net/zpoison/article/details/80729052
看一个真真牛逼的项目架构
二、走进 Spring Cloud
Spring Cloud 是一系列框架的有序集合,他不是一个简单的框架,他是一套解决方案。
Spring Cloud 并没有重复制造轮子,它只是将目前各家公司开发的比较成熟、经得起实际考验的服
务框架组合起来。
通过 Spring Boot 风格进行再封装屏蔽掉了复杂的配置和实现原理,最终给开发者留出了一套简单
易懂、易部署和易维护的分布式系统开发工具包。
它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配
置中心、消息总线、负载均衡、 断路器、数据监控等,都可以用Spring Boot的开发风格做到一键
启动和部署。
Spring Cloud项目官方网址:
https://spring.io/projects/spring-cloud
Spring Cloud 版本命名方式采用了伦敦地铁站的名称,同时根据字母表的顺序来对应版本时间顺
序,比如:最早的Release版本:Angel,第二个Release版本:Brixton,然后是Camden、
Dalston、Edgware,Finchley,Greenwich,Hoxton。
IT楠老师

Spring Cloud Version Spring Cloud Alibaba Version Spring boot Version
Spring Cloud Edgware 1.5.1.RELEASE 1.5.X.RELEASE
Spring Cloud Finchley 2.0.2.RELEASE 2.0.X.RELEASE
Spring Cloud Greenwich 2.1.2.RELEASE 2.1.X.RELEASE
Spring Cloud Hoxton.RELEASE 2.2.0.RELEASE 2.2.X.RELEASE
Spring Cloud Hoxton.SR3 2.2.1.RELEASE 2.2.5.RELEASE
目前最新的是Hoxton版本。
• 微服务就是将项目的各个模块拆分为可独立运行、部署、测试的架构设计风格。
• Spring 公司将其他公司中微服务架构常用的组件整合起来,并使用 SpringBoot 简化其开发、配置。称
为 Spring Cloud
• Spring Cloud 与 Dubbo都是实现微服务有效的工具。Dubbo 性能更好,而 Spring Cloud 功能更全
面。
第二章 服务通信的问题
所谓微服务就是要将一个臃肿的项目拆分成多个细分业务的服务,要解决的就是三高问题。
1. 高性能:响应要快,
2. 高并发:人多了不蹦
3. 高可用:崩了一些服务器还能用
首先我们解决第一个问题、多个服务之间怎么通信呀
服务之间通信,两个后台通信,就是我们的网络通信,可以是socket写,当然我们有更简单的http
这类的框架很多,httpclient,okhttp等都行
一、RestTemplate 远程调用
Spring提供的一种简单便捷的模板类,用于在 java 代码里访问 restful 服务。
其功能与 HttpClient 类似,但是 RestTemplate 实现更优雅,使用更方便。
手动创建一个父工程
1、父工程 spring-cloud-parent
IT楠老师

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>cn.itnanls</groupId>
<artifactId>study-spring-cloud</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>provider</module>
</modules>
<packaging>pom</packaging>
<!--统一管理jar包版本-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring-boot.version>2.2.2</spring-boot.version>
<alibaba.cloud.version>2.1.0</alibaba.cloud.version>
</properties>
<!--子模块继承之后,提供作用:锁定版本+子modlue不用写groupId和version-->
<dependencyManagement>
<dependencies>
<!--spring boot 2.2.2-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- spring cloud Hoxton.SR3-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--spring cloud alibaba 2.1.0 RELEASE-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</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
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
IT楠老师

注入一个RestTemplate
RestTemplateConfig
服务一
服务二
</dependencyManagement>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork> <!-- 如果没有该配置,devtools不会生效 -->
</configuration>
</plugin>
</plugins>
</build>
</project>
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
1
2
3
4
5
6
7
8
/**
* @author itnanls
* @date 2021/4/12
*/
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(){
return "hello,B!" ;
}
}
1
2
3
4
5
6
7
8
9
10
11
/**
* @author itnanls
* @date 2021/4/12
*/
@RestController
public class HelloTwo {
1
2
3
4
5
6
IT楠老师

调用成功。
当然其中有很多的方法,大家可以尝试一下
但是在微服务里还是觉得麻烦
问题拓展
将来服务越来越多,订单服务一台机子也撑不下,同时我们总是依靠ip去寻找服务也不太合适,如果每
一个服务都有一个名字就好了。
大家还记得dns吗?
当大家记不住ip地址,就搞一个域名嘛,访问的时候根据域名找一下ip不就行了?,只要域名不变,以
后ip随便变,都没有影响
所以在微服务里出现第一个词语,叫注册中心
这类组件有很多 eureka,zookeeper等等
这玩意就提供了一个服务中心,给我们上线的服务注册。
二、Nacos服务管理
Nacos(Dynamic Naming and Configuration Service) 是阿里巴巴2018年7月开源的项目。
它专注于服务发现和配置管理领域 致力于帮助您发现、配置和管理微服务。Nacos 支持几乎所有
主流类型的“服务”的发现、配置和管理。
一句话概括就是Nacos = Spring Cloud注册中心 + Spring Cloud配置中心。
官网:
https://nacos.io/
下载地址:
https://github.com/alibaba/nacos/releases
####
1、环境搭建
看资料中的代码即可
1、环境准备。
下载nacos,双击startup.cmd。
@Resource
private RestTemplate restTemplate;
@GetMapping("fromOne")
public String fromA(){
// 发送http请求,调用远程服务
ResponseEntity<String> stringResponseEntity =
restTemplate.getForEntity("/hello", String.class);
return stringResponseEntity.getBody();
}
}
7
8
9
10
11
12
13
14
15
16
17
18
IT楠老师
剩余63页未读,继续阅读


















安全验证
文档复制为VIP权益,开通VIP直接复制

评论0