一、微服務架構
微服務架構是一種將應用程序設計為一組小型服務的方式,每個服務運行在其獨立的進程中,可以通過輕量級的通信機制相互協作。微服務架構通常是由一組獨立部署的小服務組成,每個服務都使用API接口進行通信。因此,對於每個微服務都需要有獨立的代碼庫、獨立的部署和運行環境。
Spring Cloud作為當前應用廣泛使用的微服務框架,它大幅度簡化了Spring Boot的開發模式。Spring Cloud對於微服務的基礎構建模塊和技術棧,例如註冊中心、配置管理、斷路器、API網關等都進行了封裝和擴展。這其中最常用的有Netflix OSS開發的Eureka、Zuul、Hystrix等。
二、服務註冊與發現(Eureka)
在微服務架構中,服務之間的調用必須通過網絡實現。所以每個服務節點需要知道其他服務的網絡地址。使用服務註冊與發現可以動態地將服務的網絡地址注入到服務調用中。
Eureka是Netflix OSS開發的服務註冊和發現框架,服務提供者將自己的服務註冊到Eureka註冊中心,服務消費者向註冊中心拉取所需要的服務地址和端口信息。Eureka服務器會定時檢查可用服務實例列表,並驅逐已經下線的實例。
Eureka的使用方法:
// 引入Eureka依賴 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> // 主啟動類添加@EnableEurekaServer註解,聲明當前應用是Eureka Server @EnableEurekaServer @SpringBootApplication public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } }
三、服務調用(Feign與Ribbon)
在微服務架構中,服務之間的調用通常使用RESTful API進行通信。Spring Cloud提供了兩種常用的調用方式:Feign和Ribbon。
Feign是一種聲明式、模板化的HTTP客戶端工具,可以使得HTTP調用變得更加簡單和優雅。它使用接口對外暴露服務調用方法,在接口的方法上添加註解@FeignClient指定服務名稱,自動進行負載均衡。Feign在運行時會使用JDK動態代理技術生成接口的實現類,並向@Service註解標識的Spring Bean中注入該實現類。
Ribbon是一個負載均衡工具,使用它可以將請求分發到多個服務實例中。Ribbon與Eureka結合使用,Ribbon會從Eureka Server發現所有的服務實例,同時使用隨機或輪詢等策略決定請求分發的目標實例。在服務調用時,添加註解@LoadBalanced,則Ribbon會自動開啟負載均衡功能。
// 引入Feign和Ribbon依賴 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> // 創建接口並添加@FeignClient註解 @FeignClient(name = "example-service") public interface ExampleService { @GetMapping("/example") String getExample(); } // 在服務中使用@LoadBalanced註解開啟Ribbon負載均衡 @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); }
四、服務熔斷(Hystrix)
服務之間的調用如果出現錯誤,可能會導致鏈式反應。例如,有一個服務故障導致調用它的服務也失敗,最終導致整個系統不可用。為了避免這種情況發生,需要使用服務熔斷技術。
Hystrix是Netflix OSS開發的一種服務熔斷框架,可以實現服務降級、隔離和監控。當某個服務調用失敗達到一定閾值時,Hystrix會自動熔斷該服務,依賴於該服務的其他服務將自動進入降級狀態,以此避免連鎖反應。
Hystrix的使用方法:
// 引入Hystrix依賴 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> // 在應用主啟動類上添加@EnableCircuitBreaker註解,開啟Hystrix功能 @EnableCircuitBreaker @SpringBootApplication public class ExampleServiceApplication { public static void main(String[] args) { SpringApplication.run(ExampleServiceApplication.class, args); } } // 在調用方法上添加@HystrixCommand註解,指定服務熔斷的 fallback方法 @HystrixCommand(fallbackMethod = "fallbackExample") @GetMapping("/example") public String getExample() { // 調用服務方法 return exampleService.example(); } public String fallbackExample() { // 服務熔斷後執行的代碼 return "fallback"; }
五、服務網關(Zuul)
微服務架構中每個服務都會對外暴露API,當服務集群規模較大時,很難管理和維護每個服務的API網關。服務網關可以是一個單一的入口點,將所有服務的API都聚集在一起,統一進行路由和過濾。
Zuul是Netflix OSS開發的一種服務網關框架,可以實現負載均衡、認證、授權、安全、限流、監控等功能。服務路由時,Zuul會根據自定義的路由規則將請求路由到不同的服務節點。
Zuul的使用方法:
// 引入Zuul依賴 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> // 在應用主啟動類上添加@EnableZuulProxy註解,聲明當前應用是Zuul Server @EnableZuulProxy @SpringBootApplication public class GatewayApplication { public static void main(String[] args) { SpringApplication.run(GatewayApplication.class, args); } } // 自定義路由規則 @Bean public PatternServiceRouteMapper serviceRouteMapper() { return new PatternServiceRouteMapper("(?^.+)-(?v.+$)", "${version}/${name}"); }
六、分布式配置中心(Spring Cloud Config)
微服務架構中,不同的服務需要訪問許多配置信息,例如數據庫連接、安全規則等。如果需要修改這些配置信息,必須重新部署服務,會帶來很高的成本和風險。分布式配置中心可以將所有的配置信息統一管理,提供一種可管理的方式來管理應用程序所需的所有配置信息。
Spring Cloud Config是Spring Boot提供的分布式配置工具,可以將所有應用程序所需的配置信息存儲在倉庫中,並允許不同的服務從此倉庫中獲取配置信息。
Spring Cloud Config的使用方法:
// 引入Spring Cloud Config依賴 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> // 配置倉庫地址和獲取方式 spring: cloud: config: server: git: uri: git://github.com/user/repo.git search-paths: '{application}' label: master // 在應用主啟動類上添加@EnableConfigServer註解,聲明當前應用是配置中心 @EnableConfigServer @SpringBootApplication public class ConfigApplication { public static void main(String[] args) { SpringApplication.run(ConfigApplication.class, args); } } // 在應用中獲取配置信息 @Value("${example.property}") private String exampleProperty;
七、總結
本文對Springcloud面試重點內容進行了詳細講解,包括微服務架構、服務註冊與發現、服務調用、服務熔斷、服務網關和分布式配置中心。這些知識點對於掌握Springcloud框架和微服務架構都具備重要意義。希望本文能夠對讀者進行一定的指導和幫助。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/295172.html