一、背景
1.1 應用系統的架構歷史
1.2 什么是微服務?
起源:微服務的概念源於 2014 年 3 月 Martin Fowler 所寫的一篇文章“Microservices”。文中內容提到:微服務架構是一種架構模式,它提倡將單一應用程序划分成一組小的服務,服務之間互相協調、互相配合,為用戶提供最終價值。
通信方式:每個服務運行在其獨立的進程中,服務與服務間采用輕量級的通信機制互相溝通(通常是基於 HTTP 的 RESTful API)。
微服務的常規定義:微服務是一種架構風格,一個大型復雜軟件應用由一個或多個微服務組成。系統中的各個微服務可被獨立部署,各個微服務之間是松耦合的。每個微服務僅關注於完成一件任務。
把原來的一個完整的進程服務,拆分成兩個或兩個以上的進程服務,且互相之間存在調用關系,與原先單一的進程服務相比,就是“微服務”。(微服務是一個比較級的概念,而不是單一的概念)
1.3 微服務架構的優勢
- 可擴展性:在增加業務功能時,單一應用架構需要在原先架構的代碼基礎上做比較大的調整,而微服務架構只需要增加新的微服務節點,並調整與之有關聯的微服務節點即可。在增加業務響應能力時,單一架構需要進行整體擴容,而微服務架構僅需要擴容響應能力不足的微服務節點。
- 容錯性:在系統發生故障時,單一應用架構需要進行整個系統的修復,涉及到代碼的變更和應用的啟停,而微服務架構僅僅需要針對有問題的服務進行代碼的變更和服務的啟停。其他服務可通過重試、熔斷等機制實現應用層面的容錯。
- 技術選型靈活:微服務架構下,每個微服務節點可以根據完成需求功能的不同,自由選擇最適合的技術棧,即使對單一的微服務節點進行重構,成本也非常低。
- 開發運維效率更高:每個微服務節點都是一個單一進程,都專注於單一功能,並通過定義良好的接口清晰表述服務邊界。由於體積小、復雜度低,每個微服務可由一個小規模團隊或者個人完全掌控,易於保持高可維護性和開發效率。
Spring Cloud作為目前最流行的微服務開發框架,不是采用了Spring Cloud框架就實現了微服務架構,具備了微服務架構的優勢。正確的理解是使用Spring Cloud框架開發微服務架構的系統,使系統具備微服務架構的優勢(Spring Cloud就像工具,還需要“做”的過程)。
1.4 什么是Spring Boot?什么是Spring Cloud?
Spring Boot框架是由Pivotal團隊提供的全新框架,其設計目的是用來簡化基於Spring應用的初始搭建以及開發過程。SpringBoot框架使用了特定的方式來進行應用系統的配置,從而使開發人員不再需要耗費大量精力去定義模板化的配置文件。
Spring Cloud是一個基於Spring Boot實現的雲應用開發工具,它為基於JVM的雲應用開發中的配置管理、服務注冊,服務發現、斷路器、智能路由、微代理、控制總線、全局鎖、決策競選、分布式會話和集群狀態管理等操作提供了一種簡單的開發方式。
1.5 微服務、Spring Boot、Spring Cloud三者之間的關系
- 思想:微服務是一種架構的理念,提出了微服務的設計原則,從理論為具體的技術落地提供了指導思想。
- 腳手架:Spring Boot是一套快速配置腳手架,可以基於Spring Boot快速開發單個微服務。
- 多個組件的集合:Spring Cloud是一個基於Spring Boot實現的服務治理工具包;Spring Boot專注於快速、方便集成的單個微服務個體;Spring Cloud關注全局的服務治理框架。
二、技術解析
2.1 Everything is jar, Everything is http
Spring Boot通過@SpringBootApplication注解標識為Spring Boot應用程序。所有的應用都通過jar包方式編譯,部署和運行。
@SpringBootApplication public class Application { private static final Logger LOGGER = LoggerFactory.getLogger(Application.class); public static void main(String[] args) { SpringApplication.run(Application.class, args); LOGGER.info(”啟動成功!"); } }
每個Spring Boot的應用都可以通過內嵌web容器的方式提供http服務,僅僅需要在pom文件中依賴spring-boot-start-web即可,原則上微服務架構希望每個獨立節點都提供http服務。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
2.2 Spring boot Task 任務啟動和定時任務
在Spring Boot需要啟動任務時,只要繼承CommandLineRunner接口實現其run方法即可。
@SpringBootApplication public class ClientDataListener implements CommandLineRunner public void run(String... strings) throws Exception { clientInfoListenerHandler(); } }
在Spring Boot需要執行定時任務時,只需要在定時任務方法上增加@Scheduled(cron = “0 15 0 * * ?”)注解(支持標准cron表達式),並且在服務啟動類上增加@EnableScheduling的注解即可。
@SpringBootApplication @EnableScheduling public class Application { private static final Logger LOGGER = LoggerFactory.getLogger(Application.class); public static void main(String[] args) { SpringApplication.run(Application.class, args); LOGGER.info(”啟動成功!"); } }
// some class @Scheduled(cron = "0 15 0 * * ?") public void someTimeTask() { *** }
2.3 Spring boot Actuator 監控
Actuator是spring boot提供的對應用系統自身進行監控的組件,在引入spring-boot-start-web基礎上引入spring-boot-starter-actuator即可。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
2.4 Spring cloud Config 配置中心
在我們實現微服務架構時,每個微服務節點都需要自身的相關配置數據項,當節點眾多,維護就變得非常困難,因此需要建立一個中心配置服務。
Spring Cloud Config分為兩部分。Spring Cloud Config server作為一個服務進程,Spring Cloud Config File為配置文件存放位置。
2.5 Spring cloud Eureka 服務注冊中心
服務注冊的概念早在微服務架構之前就出現了,微服務架構更是把原先的單一應用節點拆分成非常多的微服務節點。互相之間的調用關系會非常復雜,Spring Cloud Eureka作為注冊中心,所有的微服務都可以將自身注冊到Spring Cloud Eureka進行統一的管理和訪問(Eureka和Zookeeper不同,在AOP原則中選擇了OP,更強調服務的有效性)
2.6 Spring cloud Zuul 服務端智能路由
當我們把所有的服務都注冊到Eureka(服務注冊中心)以后,就涉及到如何調用的問題。Spring Cloud Zuul是Spring Cloud提供的服務端代理組件,可以看做是網關,Zuul通過Eureka獲取到可用的服務,通過映射配置,客戶端通過訪問Zuul來訪問實際需要需要訪問的服務。所有的服務通過spring.application.name做標識,
不同IP地址,相同spring.application.name就是一個服務集群。當我們增加一個相同spring.application.name的節點,Zuul通過和Eureka通信獲取新增節點的信息實現智能路由,增加該類型服務的響應能力。
2.7 Spring cloud Ribbon 客戶端智能路由
與Spring Cloud Zuul的服務端代理相對應,Spring Cloud Ribbon提供了客戶端代理。在服務端代理中,客戶端並不需要知道最終是哪個微服務節點為之提供服務,而客戶端代理獲取實質提供服務的節點,並選擇一個進行服務調用。Ribbon和Zuul相似,也是通過和Eureka(服務注冊中心)進行通信來實現客戶端智能路由。
2.8 Spring cloud Sleuth 分布式追蹤
2.9 Spring cloud Zipkin 調用鏈
2.10 Spring cloud Feign http客戶端
Spring Cloud Feign是一種聲明式、模板化的http客戶端。 使用Spring Cloud Feign請求遠程服務時能夠像調用本地方法一樣,讓開發者感覺不到這是遠程方法(Feign集成了Ribbon做負載均衡)。
把遠程服務和本地服務做映射
@FeignClient(name = "rabbitmq-http", url = "${SKYTRAIN_RABBITMQ_HTTP}") public interface TaskService { @RequestMapping(value = "/api/queues", method = RequestMethod.GET) public String query(@RequestHeader("Authorization") String token); }
以調用本地服務的方式調用遠程服務
@Autowired private TaskService taskService; private String queryRabbitmqStringInfo() { byte[] credentials = Base64 .encodeBase64((rabbitmqHttpUserName + ":" + rabbitmqHttpPassword).getBytes(StandardCharsets.UTF_8)); String token = "Basic " + new String(credentials, StandardCharsets.UTF_8); return taskService.query(token); }
2.11 Spring cloud Hystrix 斷路器
三、微服務實踐
3.1 我們開發的幾個微服務組件—應用管理中心
應用管理中心可以對每個已經注冊的微服務節點進行停止,編譯,打包,部署,啟動的完整的上線操作。
3.2 我們開發的幾個微服務組件—zookeeper數據查詢中心
zookeeper數據查詢中心根據zookeeper地址,端口,命令獲取zookeeper數據信息。
3.3 我們開發的幾個微服務組件—微服務健康檢測中心
健康檢測中心周期性檢查每個微服務的狀態,當發現有微服務狀態處於DOWN或連接超時時,觸發報警。
3.4 我們開發的幾個微服務組件—定時任務查詢中心
// 在BeanPostProcessor子類中攔截 @Component public class SkytrainBeanPostProcessor implements BeanPostProcessor, Ordered { *** /** * Bean 實例化之后進行的處理 */ public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { beanPostProcessor.postProcessAfter(bean, beanName); return bean; } *** } // 攔截后獲取定時任務注解 *** public Object postProcessAfter(Object bean, String beanName) { Class targetClass = AopUtils.getTargetClass(bean); Map annotatedMethods = MethodIntrospector.selectMethods(targetClass, new MethodIntrospector.MetadataLookup() { public Set inspect(Method method) { Set scheduledMethods = AnnotatedElementUtils.getMergedRepeatableAnnotations(method, Scheduled.class, Schedules.class); return (!scheduledMethods.isEmpty() ? scheduledMethods : null); } }); if (!annotatedMethods.isEmpty()) { String className = targetClass.getName(); for (Map.Entry entry : annotatedMethods.entrySet()) { Method method = entry.getKey(); for (Scheduled scheduled : entry.getValue()) { String key = className + ":" + method.getName(); String value = scheduled.toString(); taskInfos.put(key, value); } } } return null; } *** // 獲取定時任務后注冊 *** public void taskRegister() { String nodeInfo = ipAddress + ":" + serverPort + ":"; try { /** * 定時任務 */ Map infos = taskInfos; for (Entry item : infos.entrySet()) { String taskId = nodeInfo + item.getKey(); String taskParameter = item.getValue(); JSONObject info = new JSONObject(); info.put("taskId", taskId); info.put("taskParameter", taskParameter); info.put("applicationName", applicationName); info.put("taskType", "schedule"); LOGGER.info(info.toString()); zooKeeperExecutor.createZKNode(SKYTRAIN_TASK_ZKNODE_PREFIX + taskId, info.toString()); } } catch (Exception ex) { LOGGER.error("", ex); } } ***
3.5 微服務的分類
- 微服務平台組件
- 公共服務組件
- 基礎服務組件/業務服務組件
3.6 整體微服務架構圖
作者:梁鑫
來源:宜信技術學院