Spring Cloud 基于 Spring Boot,所以在前几篇,咱们系统地学习了 Spring Boot 的基础知识,为深刻研究Spring Cloud打下扎实的基础。java
从本章开始,咱们将正式进入探索Spring Cloud秘密的旅程中。学习完本课程后,读者将从中学习到如何搭建一个完整的分布式架构,从而向架构师方向靠近。web
微服务概述spring
根据百度百科的描述,微服务架构是一项在云中部署应用和服务的新技术。大部分围绕微服务的争论都集中在容器或其余技术是否能很好的实施微服务,而红帽说 API 应该是重点。浏览器
微服务能够在“本身的程序”中运行,并经过“轻量级设备与 HTTP 型 API 进行沟通”。关键在于该服务能够在本身的程序中运行。经过这一点咱们就能够将服务公开与微服务架构(在现有系统中分布一个 API)区分开来。在服务公开中,许多服务均可以被内部独立进程所限制。若是其中任何一个服务须要增长某种功能,那么就必须缩小进程范围。在微服务架构中,只须要在特定的某种服务中增长所需功能,而不影响总体进程。架构
微服务的核心是 API,在一个大型系统中,咱们能够将其拆分为一个个的子模块,每个模块就能够是一个服务,各服务之间经过 API 进行通讯。并发
什么是 Spring Cloudapp
Spring Cloud是微服务架构思想的一个具体实现,它为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理、服务发、断路器,智能路由、微代理、控制总线等)。框架
Spring Cloud 基于 Spring Boot 框架,它不重复造轮子,而是将第三方实现的微服务应用的一些模块集成进去。准确的说,Spring Cloud 是一个容器。分布式
最简单的 Spring Cloud 项目spring-boot
学习任何一门语言和框架,从 Hello World 入门是最合适的,Spring Cloud 也不例外,接下来,咱们就来实现一个最简单的 Spring Cloud 项目。
最简单的 Spring Cloud 微服务架构包括服务发现和服务提供者(即一个大型系统拆分出来的子模块),最极端的微服务能够作到一个方法就是一个服务,一个方法就是一个项目。在一个系统中,服务怎么拆分,要具体问题具体分析,也取决于系统的并发性、高可用性等因素。
闲话少说,请看代码。
首先是服务发现,这里咱们采用 Eureka。
pom.xml 文件添加以下内容:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR5</version> <type>pom</type> <scope>import</scope> <exclusions> </exclusions> </dependency> </dependencies> </dependencyManagement>
增长 application.yml 文件,添加以下内容:
server: port: 8761 spring: profiles: active: dev eureka: server: enable-self-preservation: false instance: preferIpAddress: true hostname: ${spring.cloud.client.ipAddress} instanceId: ${spring.cloud.client.ipAddress}:${server.port} client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
添加一个启动类 Application.java:
@SpringBootApplication @EnableEurekaServer public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
而后再建立一个项目,实现服务提供者,在 pom.xml 添加以下内容:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR5</version> <type>pom</type> <scope>import</scope> <exclusions> </exclusions> </dependency> </dependencies> </dependencyManagement>
增长 application.yml,并增长以下内容:
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ server: port: 8762 spring: application: name: hello
增长一个启动类:
@SpringBootApplication @EnableEurekaClient @RestController public class HelloController { public static void main(String[] args) { SpringApplication.run(HelloController.class, args); } @Value("${server.port}") String port; @RequestMapping("/hello") public String home(String name) { return "hi "+name+",i am from port:" +port; } }
这时,分别启动服务发现和服务提供者,浏览器输入:http://localhost:8761,即服务发现的地址:
能够发现,服务提供者 Hello 已经注册到服务发现中了,而后咱们请求 hello 接口地址:http://localhost:8762/hello?name=lynn,便可以看到下面返回数据:
以上只是 Spring Cloud 的入门实例,是为了给你们展现什么是 Spring Cloud