作者:Surpasser
今天开始学习SpringCloud的相关知识,作为初学者不想过多的深究原理和底层,把学会使用作为本节的目标。
什么是微服务架构
简单地说,微服务是系统架构上的一种设计风格,它的主旨是将一个原本独立的系统拆分成多个小型服务,这些小型服务都在各自独立的进程中运行,服务之间通过基于HTTP的RESTful API进行通信协作。被拆分成的每一个小型服务都围绕着系统中的某一项或一些羁合度较高的业务功能进行构建,并且每个服务都维护着自身的数据存储、业务开发、自动化测试案例以及独立部署机制。由于有了轻量级的通信协作基础,所以这些微服务可以使用不同的语言来编写。
单体架构和分布式微服务的区别
单体架构的优点:
- 易于开发:开发人员使用当前开发工具在短时间内就可以开发出单体应用。
- 易于测试:因为不需要依赖其他接口,测试可以节约很多时间。
- 易于部署:你只需要将目录部署在运行环境中即可。
单体架构的缺点:
- 灵活度不够:如果程序有任何修改,修改的不只是一个点,而是自上而下地去修改,测试时必须等到整个程序部署完后才能看出效果。在开发过程可能需要等待其他开发人员开发完成后才能完成部署,降低了团队的灵活性。
- 降低系统的性能:原本可以直接访问数据库但是现在多了一层。即使只包含一个功能点,也需要在各个层写上代码。
- 系统启动慢:一个进程包含了所有业务逻辑,涉及的启动模块过多,导致系统的启动时间延长。
- 系统扩展性比较差:增加新东西的时候不能针对单个点增加,要全局性地增加。牵一发而动全身。
SOA架构的优点:
- 把模块拆分,使用接口通信,降低模块之间的藕合度。
- 把项目拆分成若干个子项目,不同的团队负责不同的子项目。
- 增加功能时只需要增加一个子项目,调用其他系统的接口即可。
- 可以灵活地进行分布式部署。
SOA架构的缺点:
- 系统之间的交互需要使用远程通信,接口开发增加工作量。
SpringCloud五大组件
- Eureka:注册中心
- Zuul、Gateway:网关
- Ribbon:负载均衡
- Feign:服务调用
- Hystrix或Resilience4j:熔断器
项目搭建
注册中心的服务端和客户端
版本说明:
Spring Boot | Spring Cloud |
1.2.x | Angel版本 |
1.3.x | Brixton版本 |
1.4.x stripes | Camden版本 |
1.5.x | Dalston版本、Edgware版本 |
2.0.x | Finchley版本 |
2.1.x | Greenwich.SR2 |
SpringBoot和SpringCloud的版本必须对应。详细请查看官方对应数据:https://start.spring.io/actuator/info,也许你会看着很乱,不要着急,找一个JSON转换就OK啦。
创建Maven父工程,添加pom配置
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
创建Eureka Server子工程,配置pom文件
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
在server启动类添加注解@EnableEurekaServer
@EnableEurekaServer
@SpringBootApplication
public class ServerApplication {
public static void main(String[] args) {
SpringApplication.run(ServerApplication.class, args);
}
}
创建application.yml文件
server:
port: 8001
eureka:
instance:
hostname: localhost
client:
#服务端server禁止自己注册自己
register-with-eureka: false
#此客户端是否获取eureka服务器注册表上的注册信息,默认为true
fetch-registry: false
service-url: #指定交互和通信的url
default-zone: http://${eureka.instance.hostname}:${server.port}/eureka/
server:
#当eureka server启动的时候,不能从对等节点获取instance注册信息的情况,应等待多长时间。
wait-time-in-ms-when-sync-empty: 0
#服务端开启自我保护模式。无论什么情况,服务端都会保持一定数量的服务。避免client与server的网络问题,而出现大量的服务被清除。
enable-self-preservation: false
创建Eureka Client子工程,配置pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<!--添加web,不添加启动时自动停止-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
client启动类添加注解@EnableDiscoveryClient
@EnableDiscoveryClient
@SpringBootApplication
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
}
创建application.yml文件
server:
port: 8101
#客户端必须起名字,否则注册为unknow
spring:
application:
name: client
eureka:
client:
service-url:
#填入server通信url
defaultZone: http://localhost:8001/eureka/
先后启动Server和Client,访问http://localhost:8001,可以看到如图的界面:
- 界面为Eureka Server的服务。
- 箭头指的名字为Eureka Client的服务。
访问 http://127.0.0.1:8001/eureka/apps 能够看到所有注册到Eureka Server的Client信息。
访问 http://127.0.0.1:8001/eureka/apps/+applicationName 能够看到对应Client注册到Eureka Server的信息。
注册中心到这里了,后面继续!!!