Spring Cloud(一):服務註冊與發現
Spring Cloud是什麽
Spring Cloud是一系列框架的有序集合。它利用Spring Boot的開發便利性巧妙地簡化了分布式系統基礎設施的開發,如服務發現註冊、配置中心、消息總線、負載均衡、斷路器、數據監控等,都可以用Spring Boot的開發風格做到一鍵啟動和部署。Spring並沒有重復制造輪子,它只是將目前各家公司開發的比較成熟、經得起實際考驗的服務框架組合起來,通過Spring Boot風格進行再封裝屏蔽掉了復雜的配置和實現原理,最終給開發者留出了一套簡單易懂、易部署和易維護的分布式系統開發工具包。
微服務是可以獨立部署、水平擴展、獨立訪問(或者有獨立的數據庫)的服務單元,springcloud就是這些微服務的大管家,采用了微服務這種架構之後,項目的數量會非常多,springcloud做為大管家需要管理好這些微服務,自然需要很多小弟來幫忙。
和Spring Boot 是什麽關系
Spring Boot 是 Spring 的一套快速配置腳手架,可以基於Spring Boot 快速開發單個微服務,Spring Cloud是一個基於Spring Boot實現的雲應用開發工具;Spring Boot專註於快速、方便集成的單個個體,Spring Cloud是關註全局的服務治理框架;Spring Boot使用了默認大於配置的理念,很多集成方案已經幫你選擇好了,能不配置就不配置,Spring Cloud很大的一部分是基於Spring Boot來實現,可以不基於Spring Boot嗎?不可以。
Spring Boot可以離開Spring Cloud獨立使用開發項目,但是Spring Cloud離不開Spring Boot,屬於依賴的關系。
spring -> spring booot > Spring Cloud 這樣的關系。
Spring Cloud的優勢
微服務的框架那麽多比如:dubbo、Kubernetes,為什麽就要使用Spring Cloud的呢?
- 產出於spring大家族,spring在企業級開發框架中無人能敵,來頭很大,可以保證後續的更新、完善。比如dubbo現在就差不多死了
- 有Spring Boot 這個獨立幹將可以省很多事,大大小小的活Spring Boot都搞的挺不錯。
- 作為一個微服務治理的大家夥,考慮的很全面,幾乎服務治理的方方面面都考慮到了,方便開發開箱即用。
- Spring Cloud 活躍度很高,教程很豐富,遇到問題很容易找到解決方案
- 輕輕松松幾行代碼就完成了熔斷、均衡負責、服務中心的各種平臺功能
Spring Cloud對於中小型互聯網公司來說是一種福音,因為這類公司往往沒有實力或者沒有足夠的資金投入去開發自己的分布式系統基礎設施,使用Spring Cloud一站式解決方案能在從容應對業務發展的同時大大減少開發成本。同時,隨著近幾年微服務架構和Docker容器概念的火爆,也會讓Spring Cloud在未來越來越“雲”化的軟件開發風格中立有一席之地,尤其是在目前五花八門的分布式解決方案中提供了標準化的、全站式的技術方案,意義可能會堪比當前Servlet規範的誕生,有效推進服務端軟件系統技術水平的進步。
Spring Cloud Eureka
我們用Spring Cloud Eureka來實現服務治理。
創建“服務註冊中心”
創建一個基礎的Spring Boot工程,命名為springboot-register,並在pom.xml
中引入需要的依賴內容:
1 <dependencies> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-web</artifactId> 5 </dependency> 6 7 <dependency> 8 <groupId>org.springframework.cloud</groupId> 9 <artifactId>spring-cloud-starter-eureka-server</artifactId> 10 </dependency> 11 <dependency> 12 <groupId>org.springframework.boot</groupId> 13 <artifactId>spring-boot-starter-test</artifactId> 14 <scope>test</scope> 15 </dependency> 16 </dependencies> 17 <dependencyManagement> 18 <dependencies> 19 <dependency> 20 <groupId>org.springframework.cloud</groupId> 21 <artifactId>spring-cloud-dependencies</artifactId> 22 <version>Brixton.SR5</version> 23 <type>pom</type> 24 <scope>import</scope> 25 </dependency> 26 </dependencies> 27 </dependencyManagement>
通過@EnableEurekaServer
註解啟動一個服務註冊中心提供給其他應用進行對話:
@EnableEurekaServer @SpringBootApplication public class ApplicationRegister { public static void main(String[] args) { SpringApplication.run(ApplicationRegister.class, args); } }
在application.properties
配置文件中增加如下信息:
server.port=1111 eureka.server.enable-self-preservation=false eureka.instance.hostname=localhost eureka.client.register-with-eureka=false eureka.client.fetch-registry=false eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
訪問:http://localhost:1111/ ,如圖所示:
頁面中還沒有任何服務!
註冊“服務提供方”
創建一個基本的Spring Boot應用。命名為spring boot-client
,在pom.xml
中,加入如下配置:
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-web</artifactId> 4 </dependency> 5 6 <dependency> 7 <groupId>org.springframework.cloud</groupId> 8 <artifactId>spring-cloud-starter-eureka-server</artifactId> 9 </dependency> 10 <dependency> 11 <groupId>org.springframework.boot</groupId> 12 <artifactId>spring-boot-starter-test</artifactId> 13 <scope>test</scope> 14 </dependency> 15 </dependencies> 16 <dependencyManagement> 17 <dependencies> 18 <dependency> 19 <groupId>org.springframework.cloud</groupId> 20 <artifactId>spring-cloud-dependencies</artifactId> 21 <version>Brixton.SR5</version> 22 <type>pom</type> 23 <scope>import</scope> 24 </dependency> 25 </dependencies> 26 </dependencyManagement>
編寫一個controller:
1 @RestController 2 public class HelloController { 3 private final Logger logger = Logger.getLogger(getClass()); 4 5 @Autowired 6 private DiscoveryClient client; 7 8 @RequestMapping(value = "/hello", method = RequestMethod.GET) 9 public String index() throws Exception{ 10 11 return "Hello World"; 12 } 13 }
最後在應用主類中通過加上@EnableDiscoveryClient
註解,實現Controller中對服務信息的輸出:
1 @EnableDiscoveryClient 2 @SpringBootApplication 3 public class ApplicationClient { 4 5 public static void main(String[] args) { 6 SpringApplication.run(ApplicationClient.class, args); 7 } 8 9 }
在application.properties配置文件中增加如下信息:
server.port=2224 spring.application.name=hello-service eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
啟動該工程後,再次訪問:http://localhost:1111/ ,如圖:
我們也可以通過直接訪問spring boot-client服務提供的/hello接口,訪問:http://localhost:2224/hello,得到如下輸出:
這樣我們的服務註冊與發現就完成了。
Spring Cloud(一):服務註冊與發現