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