SpringCloud学习一
单体应用存在的问题
-
随着业务的发展,开发变得越来越复杂。
-
修改、新增某个功能,需要对整个系统进行测试、重新部署。
-
一个模块出现问题,很可能导致整个系统崩溃。
-
多个开发团队同时对数据进行管理,容易产生安全漏洞。
-
各个模块使用同一种技术进行开发,各个模块很难根据实际情况选择更合适的技术框架,局限性很大。
-
模块内容过于复杂,如果员工离职,可能需要很长时间才能完成工作交接。
分布式、集群
集群:一台服务器无法负荷高并发的数据访问量,那么就设置十台服务器一起分担压力,十台不行就设置一百台(物理层面)。很多人干同一件事情,来分摊压力。
分布式:将一个复杂问题拆分成若干个简单的小问题,将一个大型的项目架构拆分成若干个微服务来协同完成。(软件设计层面)。将一个庞大的工作拆分成若干个小步骤,分别由不同的人完成这些小步骤,最终将所有的结果进行整合实现大的需求。
服务治理的核心又三部分组成:服务提供者、服务消费者、注册中心。
在分布式系统架构中,每个微服务在启动时,将自己的信息存储在注册中心,叫做服务注册。
服务消费者从注册中心获取服务提供者的网络信息,通过该信息调用服务,叫做服务发现。
Spring Cloud 的服务治理使用 Eureka 来实现,Eureka 是 Netflix 开源的基于 REST 的服务治理解决方案,Spring Cloud 集成了 Eureka,提供服务注册和服务发现的功能,可以和基于 Spring Boot 搭建的微服务应用轻松完成整合,开箱即用,Spring Cloud Eureka。
Spring Cloud Eureka
-
Eureka Server,注册中心
-
Eureka Client,所有要进行注册的微服务通过 Eureka Client 连接到 Eureka Server,完成注册。
Eureka Server代码实现
整体结构:
-
创建父工程,pom.xml
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.7.RELEASE</version>
</parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- 解决 JDK 9 以上没有 JAXB API 的问题 --><dependency><groupId>javax.xml.bind</groupId><artifactId>jaxb-api</artifactId><version>2.3.0</version></dependency><dependency><groupId>com.sun.xml.bind</groupId><artifactId>jaxb-impl</artifactId><version>2.3.0</version></dependency><dependency><groupId>com.sun.xml.bind</groupId><artifactId>jaxb-core</artifactId><version>2.3.0</version></dependency><dependency><groupId>javax.activation</groupId><artifactId>activation</artifactId><version>1.1.1</version></dependency>
</dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Finchley.SR2</version><type>pom</type><scope>import</scope></dependency></dependencies>
</dependencyManagement>
- 在父工程下创建 Module,pom.xml
<dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId><version>2.0.2.RELEASE</version></dependency>
</dependencies>
- 创建配置文件 application.yml,添加 Eureka Server 相关配置。
server:port: 8761
eureka:client:register-with-eureka: falsefetch-registry: falseservice-url:defaultZone: http://localhost:8761/eureka/
> 属性说明
`server.port`:当前 Eureka Server 服务端口。
`eureka.client.register-with-eureka`:是否将当前的 Eureka Server 服务作为客户端进行注册。
`eureka.client.fetch-fegistry`:是否获取其他 Eureka Server 服务的数据。
`eureka.client.service-url.defaultZone`:注册中心的访问地址。
- 创建启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {public static void main(String[] args) {SpringApplication.run(EurekaServerApplication.class,args);}
}
注解说明:
@SpringBootApplication
:声明该类是 Spring Boot 服务的入口。
@EnableEurekaServer
:声明该类是一个 Eureka Server 微服务,提供服务注册和服务发现功能,即注册中心。
运行:
浏览器输入:
localhost:8761
Eurekahttp://localhost:8761/