javascript
用Spring Cloud和Docker搭建微服务平台
This blog series will introduce you to some of the foundational concepts of building a microservice-based platform using Spring Cloud and Docker.
?本系列文章將向你介紹一些有關使用Spring Cloud和Docker搭建微服務平臺的基本概念。
?
What is Spring Cloud?
Spring Cloud is a collection of tools from Pivotal that provides solutions to some of the commonly encountered patterns when building distributed systems. If you’re familiar with building applications with Spring Framework, Spring Cloud builds upon some of its common building blocks.
Among the solutions provided by Spring Cloud, you will find tools for the following problems:
l? Configuration management
l? Service discovery
l? Circuit breakers
l? Distributed sessions
Spring Cloud是一組由Pivotal公司開發的工具集,為搭建分布式系統提供一些常見模式的解決方案。如果你熟悉使用Spring Framework來搭建應用程序的話,會發現,Spring Cloud是構建在它的一些常用構建塊的基礎上的。
在Spring Cloud提供的眾多解決方案中,你會找到能夠解決如下問題的工具:
l? 配置管理
l? 服務發現
l? 斷路器
l? 分布式會話
?
Spring Boot
The great part about working with Spring Cloud is that it builds on the concepts of Spring Boot.
Spring Cloud中的絕大部分,是構建在Spring Boot的概念之上的。
?
For those of you who are new to Spring Boot, the name of the project means exactly what it says. You get all of the best things of the Spring Framework and ecosystem of projects, tuned to perfection, with minimal configuration, all ready for production.
對于那些剛接觸Spring Boot的人來說,這個項目的名字就是它的含義。你將會得到Spring Framework和項目生態系統最好的東西,經過最少的配置,調整到完美,做好生產前的所有準備。
?
Service Discovery and Intelligent Routing
服務發現和智能路由
?
Each service has a dedicated purpose in a microservices architecture. When building a microservices architecture on Spring Cloud, there are a few primary concerns to deal with first. The first two microservices you will want to create are the Configuration Service, and the Discovery Service.
在微服務框架下,每個服務都只專注于一個目標。用Spring Cloud搭建微服務框架的時候,有幾個先要處理的關鍵點。首先要創建的兩個微服務是:配置服務(configuration service)和發現服務(discovery service)。
?
?
?
The graphic above illustrates a 4-microservice setup, with the connections between them indicating a dependency.
上面的圖片演示了一個由4個微服務組成的系統,以及他們之間的依賴關系。
?
The configuration service sits at the top, in yellow, and is depended on by the other microservices. The discovery service sits at the bottom, in blue, and also is depended upon by the other microservices.
在最上面的黃色標記的配置服務(configuration service),被其他所有的微服務所依賴。最下面的藍色標記的發現服務(discovery service),同樣是被其他所有的微服務所依賴。
?
In green, we have two microservices that deal with a part of the domain of the example application I will use throughout this blog series: movies and recommendations.
綠色的兩個微服務將會作為例子,貫穿整篇文章。他們分別提供電影播放和簡介的服務。
?
Configuration Service
?
The configuration service is a vital component of any microservices architecture. Based on the twelve-factor app methodology, configurations for your microservice applications should be stored in the environment and not in the project.
配置服務(configuration service)對所有微服務框架都是一個只管重要的組件。基于“12-factor app”方法論(附2),微服務應用程序的配置信息被保存在系統環境中,而不是工程文件里。
?
The configuration service is essential because it handles the configurations for all of the services through a simple point-to-point service call to retrieve those configurations. The advantages of this are multi-purpose.
配置服務(configuration service)很重要的原因是:通過一個簡單的點對點服務調用,所有的服務就可以通過它獲取自己的配置信息。這樣做的好處就是:多用途(一次配置多處使用)。
?
Let's assume that we have multiple deployment environments. If we have a staging environment and a production environment, configurations for those environments will be different. A configuration service might have a dedicated Git repository for the configurations of that environment. None of the other environments will be able to access this configuration, it is available only to the configuration service running in that environment.
假設我們有多個部署環境。一個測試環境,一個生產環境,他們的配置信息肯定是不一樣的。配置服務(configuration service)會有一個獨立的Git倉庫,每個環境只會讀取自己專屬的配置信息。
?
?
When the configuration service starts up, it will reference the path to those configuration files and begin to serve them up to the microservices that request those configurations. Each microservice can have their configuration file configured to the specifics of the environment that it is running in. In doing this, the configuration is both externalized and centralized in one place that can be version-controlled and revised without having to restart a service to change a configuration.
當配置服務(configuration service)啟動之后,他會根據路徑為其他的微服務們提供讀取配置文件的服務。每個微服務都擁有它正在運行的環境所使用的配置文件。實現了配置信息的外置化、集中化,并將它們置于版本控制下。這樣一來,修改配置信息也就不用重啟(配置)服務了。
?
With management endpoints available from Spring Cloud, you can make a configuration change in the environment and signal a refresh to the discovery service that will force all consumers to fetch the new configurations.
基于Spring Cloud對端點的管理,你可以在修改配置信息之后,刷新發現服務(discovery service),即實現了調用者(consumers)去提取新的配置信息。
?
Discovery Service
The discovery service is another vital component of our microservice architecture. The discovery service handles maintaining a list of service instances that are available for work within a cluster. Within applications, service-to-service calls are made using clients. For this example project, I used Spring Cloud Feign, a client-based API for RESTful microservices that originated from the Netflix OSS project.
發現服務(discovery service)是微服務架構中另一個必不可少的組件。它負責維護集群里所有正在運行中的服務實例的列表。在應用程序內部,用客戶端的方式來實現服務間的調用。這里,我用的是Spring Cloud Feign, 一個基于RESTful客戶端的API,源自于Netflix OSS項目。
?
@FeignClient("movie")
public interface MovieClient {
??? @RequestMapping(method = RequestMethod.GET, value = "/movies")
??? PagedResources findAll();
?
??? @RequestMapping(method = RequestMethod.GET, value = "/movies/{id}")
??? Movie findById(@RequestParam("id") String id);
?
??? @RequestMapping(method = RequestMethod.POST, value = "/movies",
????? produces = MediaType.APPLICATION_JSON_VALUE)
??? void createMovie(@RequestBody Movie movie);
}
?
In the code example above, I am creating a Feign client that maps to the REST API methods that are exposed by the movie service. Using the?@FeignClient?annotation, I first specify that I want to create a client API for the movie microservice. Next I specify the mappings of the service that I want to consume. I do this by declaring a URL pattern over the methods that describes a route for a REST API.
在上面的代碼里,我創建了一個Feign客戶端,把movie服務中暴露的接口,映射成REST API。首先,我用@FeignClient注解指明,我要創建一個針對movie微服務的客戶端API。然后,我在函數的前面,以URL的形式,具體說明了哪些是接口函數——將它們定義成了REST API的路徑。
?
The wonderfully easy part of creating Feign clients is that all I need to know is the ID of the service that I would like to create a client on. The URL of the service is automatically configured at runtime because each microservice in the cluster will register with the discovery service with its serviceId at startup.
要創建一個Feign客戶端,我只需要知道我要訪問的服務的ID。因為,每個微服務的URL在發現服務(discovery service)啟動的時候,就被注冊在了各自的服務ID上。
?
The same is true for all other services of my microservice architecture. All I need to know is the serviceId of the service I want to communicate with, and everything else will be autowired by Spring.
在我搭建的微服務架構里的其他服務也都一樣的。我想要跟哪個服務通訊,只要知道它的服務ID就可以了,其他的事情由Spring自動裝配。
?
API Gateway
API?網關服務
?
The API gateway service is another vital component if we are going to create a cluster of services managing their own domain entities. The green hexagons below are our data-driven services that manage their own domain entities and even their own databases. By adding an API gateway service, we can create a proxy of each API route that are exposed by the green services.
如果我們想要創建一個服務集群來管理他們自己的域實體(注),那么,API網關服務(API Gateway)也是一個必不可少的組件。
?
?
Let’s assume that both the recommendation service and the movie service expose their own REST API over the domain entities that they manage. The API gateway will discover these services through the discovery service and inject a proxy-based route of the API methods from the other services. In this way, both the recommendation and movie microservice will have a full definition of routes available locally from all the microservices that expose a REST API. The API Gateway will re-route the request to the service instances that own the route being requested through HTTP.
讓我們假設剛才介紹的兩個服務和movie服務已經通過域實體暴露了REST API。API網關服務(API Gateway)會通過發現服務(discovery service)發現這些服務,并且將它們的接口函數,從其他服務那里注入基于代理的路由。這樣,上面說的三個服務就具有了完整的REST API的路由(serviceID+apiUrl)。API網關服務(API Gateway)將接口調用重定向成HTTP請求到對應的服務實例上。
?
Example Project
實例
?
I’ve put together an example project that demonstrates an end-to-end cloud-native platform using Spring Cloud for building a practical microservices architecture.
Demonstrated concepts:
l? Integration testing using Docker
l? Polyglot persistence
l? Microservice architecture
l? Service discovery
l? API gateway
下面,我將用一個端到端的原生云(cloud-native)平臺系統,來演示怎樣用SpringCloud搭建真實的微服務框架。
演示中涉及的相關概念:
l? 用Docker進行集成測試
l? 混合持久化
l? 微服務框架
l? 服務發現
l? API網關
?
Docker
?
Each service is built and deployed using Docker. End-to-end integration testing can be done on a developer’s machine using Docker compose.
每個服務都是Docker編譯和部署的。開發人員在自己的開發環境就可以用Docker實現端到端的集成測試。
?
Polyglot Persistence
混合持久化
?
One of the core concepts of this example project is how polyglot persistence can be approached in practice. Microservices in the project use their own database while integrating with the data from other services through REST or a message bus. For example, you could have a microservice for each of the following databases.
l? Neo4j (graph)
l? MongoDB (document)
l? MySQL (relational)
例子工程里一個核心概念,就是要探討如何實現“混合持久化”(注4)。在整合其他服務的數據時(通過REST或是消息總線),工程中的微服務各自使用獨立的數據庫。舉個例子,每個微服務都可以選擇下面不同的數據庫:
? Neo4j (圖形數據庫,注5)
? MongoDB (文檔導向型數據庫)
? MySQL (關系型數據庫)
?
Microservice architecture
微服務框架
?
This example project demonstrates how to build a new application using microservices, as opposed to a monolith-first strategy. Since each microservice in the project is a module of a single parent project, developers have the advantage of being able to run and develop with each microservice running on their local machine. Adding a new microservice is easy, as the discovery microservice will automatically discover new services running on the cluster.
例子工程演示了怎樣用微服務搭建一個應用程序,而不是采用“整體架構先行”的策略。正是因為每個微服務都是一個“單父項目”,開發人員可以在本機上很方便地單獨運行和開發任何一個微服務。添加一個新的微服務也很簡單,就像發現服務會自動找到集群里新近運行的服務一樣。
?
Service discovery
服務發現
?
This project contains two discovery services, one on Netflix Eureka, and the other uses Consul from Hashicorp. Having multiple discovery services provides the opportunity to use one (Consul) as a DNS provider for the cluster, and the other (Eureka) as a proxy-based API gateway.
本工程有兩個發現服務,一個是Netflix Eureka,另一個是Hashicorp的Consul。用多個發現服務,是為了實現:一個為集群提供DNS功能(Consul),一個作為API網關服務(Eureka)。
?
API gateway
API?網關服務
?
Each microservice will coordinate with Eureka to retrieve API routes for the entire cluster. Using this strategy each microservice in a cluster can be load balanced and exposed through one API gateway. Each service will automatically discover and route API requests to the service that owns the route. This proxying technique is equally helpful when developing user interfaces, as the full API of the platform is available through its own host as a proxy.
每一個微服務都是通過Eureka在整個集群范圍內進行協調獲取API路由的。在此策略下,微服務間實現了負載均衡,并且向外暴露了一同個接口網關。服務會自動發現,并分配到所以求的API自己的路由上。這種代理技術在開發用戶接口的時候同樣有用,作為平臺完整的API通過自己的主機映射為代理服務。
?
Docker Demo
?
The example project uses Docker to build a container image of each of our microservices as a part of the Maven build process. We can easily orchestrate the full microservice cluster on our own machine using Docker compose.
例子工程用Docker為我們的每一個微服務創建一個容器鏡像,作為maven部署的一部分。我們甚至可以很輕松地把整個微服務集群都部署(orchestrate編配)到我們的開發機上。
?
Getting Started
開始吧
?
To get started, visit the GitHub repository for this example project.
在開始之前,先到GitHub資源庫上下載例子工程:
?
https://github.com/kbastani/spring-cloud-microservice-example
?
Clone or fork the project and download the repository to your machine. After downloading, you will need to use both Maven and Docker to compile and build the images locally.
把工程下載到本地之后(克隆或建立分支),你需要用Maven和Doker在本地進行編譯并生成鏡像文件。
?
Download Docker
下載Docker
?
First, download Docker if you haven’t already. Follow the instructions found here, to get Docker up and running on your development machine.
首先,若本地沒有Docker的話,要先下載。根據docs.docker.com/installation/上面的說明,獲取Docker,在開發機上運行。
?
You will also need to install Docker Compose, the installation guide can be found here. If you are using Mac OSX and boot2docker, make sure that you provision the boot2docker-vm on VirtualBox with at least 5GB of memory. The following command will allow you to do this.
你還需要安裝Docker Compose,安裝指導詳見docs.docker.com/compose/install/。如果你使用的是Mac OSX和boot2docker,確認好你在VirtualBox上創建的boot2docker虛擬機的內存是5GB以上。下面是設置內存的命令:
?
$ boot2docker init --memory=5000
?
Requirements
需求
?
The requirements for running this demo on your machine are found below.
在開發機上運行本實例的前提是已安裝如下軟件:
?
l? Maven 3
l? Java 8
l? Docker
l? Docker Compose
?
Building the project
編譯工程
?
To build the project, from the terminal, run the following command at the root of the project.
開始編譯:打開終端(命令行),在工程的根目錄下運行如下命令:
?
$ mvn clean install
?
The project will then download all of the needed dependencies and compile each of the project artifacts. Each service will be built, and then a Maven Docker plugin will automatically build each of the images into your local Docker registry. Docker must be running and available from the command line where you run the?mvn clean install?command for the build to succeed.
After the project successfully builds, you’ll see the following output:
工程將會下載所有的依賴,并依次編譯每一個服務(artifacts)。編譯完之后,Maven上的Docker插件會自動將這些鏡像文件拷貝到你本地的Docker資源庫里。在運行mvn clean install這個命令的時候,Docker必須是運行并可用的,才能保證編譯成功。成功之后,你將會看到如下輸出:
?
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] spring-cloud-microservice-example-parent .......... SUCCESS [? 0.268 s]
[INFO] users-microservice ................................ SUCCESS [ 11.929 s]
[INFO] discovery-microservice ............................ SUCCESS [? 5.640 s]
[INFO] api-gateway-microservice .......................... SUCCESS [? 5.156 s]
[INFO] recommendation-microservice ....................... SUCCESS [? 7.732 s]
[INFO] config-microservice ............................... SUCCESS [? 4.711 s]
[INFO] hystrix-dashboard ................................. SUCCESS [? 4.251 s]
[INFO] consul-microservice ............................... SUCCESS [? 6.763 s]
[INFO] movie-microservice ................................ SUCCESS [? 8.359 s]
[INFO] movies-ui ......................................... SUCCESS [ 15.833 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
?
?
Start the Cluster with Docker Compose
用Docker Compose運行集群
?
Now that each of the images has been built successfully, we can using Docker Compose to spin up our cluster. I’ve included a pre-configured Docker Compose yaml file with the project.
From the project root, navigate to the?spring-cloud-microservice-example/docker?directory.
Now, to startup the microservice cluster, run the following command:
所有的鏡像都已編譯成功,我們可以用Docker Compose把集群運轉起來了。我已經在工程里引入了一個預編譯的Docker Compose yaml文件。
從工程根目錄進入spring-cloud-microservice-example/docker目錄,運行如下命令來啟動微服務集群:
?
$ docker-compose up
?
If everything is configured correctly, each of the container images we built earlier will be launched within their own VM container on Docker and networked for automatic service discovery. You will see a flurry of log output from each of the services as they begin their startup sequence. This might take a few minutes to complete, depending on the performance of the machine you’re running this demo on.
只要正確地配置,我們編譯的鏡像將會在Docker容器里啟動,發現服務自動加載到網絡上。你會在log輸出端看見一陣忙亂,是因為這些服務都在依次啟動各自的應用。這可能會持續幾分鐘,依機器的性能而定。
?
Once the startup sequence is completed, you can navigate to the Eureka host and see which services have registered with the discovery service.
Copy and paste the following command into the terminal where Docker can be accessed using the?$DOCKER_HOST?environment variable.
啟動完畢之后,你可以登陸到Eureka服務器上,查看有哪些服務被發現服務注冊在了上面。
登陸Eureka的方法:在命令行終端運行下面的命令(保證可以通過環境變量 $DOCKER_HOST可以登陸Docker):
?
$ open $(echo \"$(echo $DOCKER_HOST)\"|
??????????? \sed 's/tcp:\/\//http:\/\//g'|
??????????? \sed 's/[0-9]\{4,\}/8761/g'|
??????????? \sed 's/\"//g')
?
If Eureka correctly started up, a browser window will open to the location of the Eureka service’s dashboard, as shown below.
如果Eureka被正常啟動了,瀏覽器會打開Eureka的控制臺,如下圖所示。
?
?
We can see each of the service instances that are running and their status. We can then access one of the data-driven services, for example the movie service.
我們可以在上看見所有已經啟動的服務實例,以及他們的狀態。怎樣登陸一個以數據驅動的服務呢?以movie服務為例:
?
$ open $(echo \"$(echo $DOCKER_HOST)/movie\"|
??????????? \sed 's/tcp:\/\//http:\/\//g'|
??????????? \sed 's/[0-9]\{4,\}/10000/g'|
??????????? \sed 's/\"//g')
?
This command will navigate to the API gateway’s endpoint and proxy to the movie service’s REST API endpoints. These REST APIs have been configured to use HATEOAS, which supports the auto-discovery of all of the service’s functionality as embedded links.
上面的命令會通過API網關找到movie服務的REST API。這些REST API經過配置,使用HATEOAS來支持象內嵌連接一樣找到服務提供的所有功能。
?
{
? "_links" : {
??? "self" : {
????? "href" : "http://192.168.59.103:10000/movie"
??? },
??? "resume" : {
????? "href" : "http://192.168.59.103:10000/movie/resume"
??? },
??? "pause" : {
????? "href" : "http://192.168.59.103:10000/movie/pause"
??? },
??? "restart" : {
????? "href" : "http://192.168.59.103:10000/movie/restart"
??? },
??? "metrics" : {
????? "href" : "http://192.168.59.103:10000/movie/metrics"
??? },
??? "env" : [ {
????? "href" : "http://192.168.59.103:10000/movie/env"
??? }, {
????? "href" : "http://192.168.59.103:10000/movie/env"
??? } ],
??? "archaius" : {
????? "href" : "http://192.168.59.103:10000/movie/archaius"
??? },
??? "beans" : {
????? "href" : "http://192.168.59.103:10000/movie/beans"
??? },
??? "configprops" : {
????? "href" : "http://192.168.59.103:10000/movie/configprops"
??? },
??? "trace" : {
????? "href" : "http://192.168.59.103:10000/movie/trace"
??? },
??? "info" : {
???? ?"href" : "http://192.168.59.103:10000/movie/info"
??? },
??? "health" : {
????? "href" : "http://192.168.59.103:10000/movie/health"
??? },
??? "hystrix.stream" : {
????? "href" : "http://192.168.59.103:10000/movie/hystrix.stream"
??? },
??? "routes" : {
????? "href" : "http://192.168.59.103:10000/movie/routes"
??? },
??? "dump" : {
????? "href" : "http://192.168.59.103:10000/movie/dump"
??? },
??? "refresh" : {
????? "href" : "http://192.168.59.103:10000/movie/refresh"
??? },
??? "mappings" : {
????? "href" : "http://192.168.59.103:10000/movie/mappings"
??? },
??? "autoconfig" : {
????? "href" : "http://192.168.59.103:10000/movie/autoconfig"
??? }
? }
}
?
Conclusion
總結
?
This has been the first part in a multi-part series about building microservice architectures with Spring Cloud and Docker. In this blog post, we went over the following concepts:
l? Service Discovery
l? Externalized Configuration
l? API Gateway
l? Service Orchestration with Docker Compose
本文是介紹如何使用Spring Cloud和Docker搭建微服務框架系列的第一篇。通過本文,我們了解了如下幾個概念:
l? 服務發現
l? 配置外置化
l? API網關
l? 用Docker Compose編配服務
?
In the next blog post, we will go over how to build application front-ends that integrate with our backend services. We will also take a look at a use case for polyglot persistence, using both MySQL (relational) and Neo4j (graph).
下篇文章,我們將會了解怎樣搭建一個前端應用,來跟本文里的后臺服務進行整合。還會通過一個實例講講混合持久化,使用MySQL和Neo4j。
?
Special thanks
特別感謝
?
I want to give special thanks to Josh Long and the rest of the Spring team for giving me the chance to learn first-hand about the many wonderful things that the Spring Framework has to offer. Without Josh's mentorship I would not be able to put into words all of the amazing things that the Spring ecosystem has to offer.
特別感謝Josh Long以及Spring團隊的其他成員,給我機會讓我率先學習到了Spring Framework提供的美妙功能。沒有Josh的指導,我更不可能把Spring生態系統提供功能寫出來。
?
Many of the great open source tools, like Spring Cloud, wouldn't be possible without the thought leadership from people like Adrian Cockcroft (Netflix OSS), Martin Fowler (everything), Sam Newman (O'Reilly's Building Microservices), Ian Robinson (consumer driven contracts), Chris Richardson (Cloud Foundry) and the many others who have helped to make the world of open source software what it is today.
沒有Adrian Cockcroft (Netflix OSS), Martin Fowler (everything), Sam Newman (O'Reilly's Building Microservices), Ian Robinson (consumer driven contracts注6), Chris Richardson (Cloud Foundry)和領導,很多像Spring Cloud這樣偉大的開源工具是不可能實現的,以及其他同樣在開源世界里做出貢獻的人們。
?
?
注1:Spring Cloud
(摘自:http://projects.spring.io/spring-cloud/#quick-start)
Spring Cloud provides tools for developers to quickly build some of the common patterns in distributed systems (e.g. configuration management, service discovery, circuit breakers, intelligent routing, micro-proxy, control bus, one-time tokens, global locks, leadership election, distributed sessions, cluster state). Coordination of distributed systems leads to boiler plate patterns, and using Spring Cloud developers can quickly stand up services and applications that implement those patterns. They will work well in any distributed environment, including the developer's own laptop, bare metal data centres, and managed platforms such as Cloud Foundry.
Spring Cloud提供了一系列工具,以使開發者能夠在分布式系統上快速搭建起常用的功能。比如說:配置管理、服務發現、斷路器、智能路由、微代理、控制總線、one-time tokens、全局鎖、leadership election、分布式會話、集群狀態。分布式系統的協調導致了樣板模式(boiler plate patterns),并且使用Spring Cloud開發人員可以快速地搭建起實現這些模式的服務和應用程序。
?
注2:一句話概括下spring框架及spring cloud框架主要組件
(轉自:http://www.cnblogs.com/skyblog/p/5073843.html)
spring 頂級項目:
Spring IO platform:用于系統部署,是可集成的,構建現代化應用的版本平臺,具體來說當你使用maven dependency引入spring jar包時它就在工作了。
Spring Boot:旨在簡化創建產品級的 Spring 應用和服務,簡化了配置文件,使用嵌入式web服務器,含有諸多開箱即用微服務功能,可以和spring cloud聯合部署。
Spring Framework:即通常所說的spring 框架,是一個開源的Java/Java EE全功能棧應用程序框架,其它spring項目如spring boot也依賴于此框架。
Spring Cloud:微服務工具包,為開發者提供了在分布式系統的配置管理、服務發現、斷路器、智能路由、微代理、控制總線等開發工具包。
Spring XD:是一種運行時環境(服務器軟件,非開發框架),組合spring技術,如spring batch、spring boot、spring data,采集大數據并處理。
Spring Data:是一個數據訪問及操作的工具包,封裝了很多種數據及數據庫的訪問相關技術,包括:jdbc、Redis、MongoDB、Neo4j等。
Spring Batch:批處理框架,或者說是批量任務執行管理器,功能包括任務調度、日志記錄/跟蹤等。
Spring Security:是一個能夠為基于Spring的企業應用系統提供聲明式的安全訪問控制解決方案的安全框架。
Spring Integration:面向企業應用集成(EAI/ESB)的編程框架,支持的通信方式包括HTTP、FTP、TCP/UDP、JMS、RabbitMQ、Email等。
Spring Social:一組工具包,一組連接社交服務API,如Twitter、Facebook、LinkedIn、GitHub等,有幾十個。
Spring AMQP:消息隊列操作的工具包,主要是封裝了RabbitMQ的操作。
Spring HATEOAS:是一個用于支持實現超文本驅動的 REST Web 服務的開發庫。
Spring Mobile:是Spring MVC的擴展,用來簡化手機上的Web應用開發。
Spring for Android:是Spring框架的一個擴展,其主要目的在于簡化Android本地應用的開發,提供RestTemplate來訪問Rest服務。
Spring Web Flow:目標是成為管理Web應用頁面流程的最佳方案,將頁面跳轉流程單獨管理,并可配置。
Spring LDAP:是一個用于操作LDAP的Java工具包,基于Spring的JdbcTemplate模式,簡化LDAP訪問。
Spring Session:session管理的開發工具包,讓你可以把session保存到redis等,進行集群化session管理。
Spring Web Services:是基于Spring的Web服務框架,提供SOAP服務開發,允許通過多種方式創建Web服務。
Spring Shell:提供交互式的Shell可讓你使用簡單的基于Spring的編程模型來開發命令,比如Spring Roo命令。
Spring Roo:是一種Spring開發的輔助工具,使用命令行操作來生成自動化項目,操作非常類似于Rails。
Spring Scala:為Scala語言編程提供的spring框架的封裝(新的編程語言,Java平臺的Scala于2003年底/2004年初發布)。
Spring BlazeDS Integration:一個開發RIA工具包,可以集成Adobe Flex、BlazeDS、Spring以及Java技術創建RIA。
Spring Loaded:用于實現java程序和web應用的熱部署的開源工具。
Spring REST Shell:可以調用Rest服務的命令行工具,敲命令行操作Rest服務。
?
目前來說spring主要集中于spring boot(用于開發微服務)和spring cloud相關框架的開發,spring cloud子項目包括:
Spring Cloud Config:配置管理開發工具包,可以讓你把配置放到遠程服務器,目前支持本地存儲、Git以及Subversion。
Spring Cloud Bus:事件、消息總線,用于在集群(例如,配置變化事件)中傳播狀態變化,可與Spring Cloud Config聯合實現熱部署。
Spring Cloud Netflix:針對多種Netflix組件提供的開發工具包,其中包括Eureka、Hystrix、Zuul、Archaius等。
Netflix Eureka:云端負載均衡,一個基于 REST 的服務,用于定位服務,以實現云端的負載均衡和中間層服務器的故障轉移。
Netflix Hystrix:容錯管理工具,旨在通過控制服務和第三方庫的節點,從而對延遲和故障提供更強大的容錯能力。
Netflix Zuul:邊緣服務工具,是提供動態路由,監控,彈性,安全等的邊緣服務。
Netflix Archaius:配置管理API,包含一系列配置管理API,提供動態類型化屬性、線程安全配置操作、輪詢框架、回調機制等功能。
Spring Cloud for Cloud Foundry:通過Oauth2協議綁定服務到CloudFoundry,CloudFoundry是VMware推出的開源PaaS云平臺。
Spring Cloud Sleuth:日志收集工具包,封裝了Dapper,Zipkin和HTrace操作。
Spring Cloud Data Flow:大數據操作工具,通過命令行方式操作數據流。
Spring Cloud Security:安全工具包,為你的應用程序添加安全控制,主要是指OAuth2。
Spring Cloud Consul:封裝了Consul操作,consul是一個服務發現與配置工具,與Docker容器可以無縫集成。
Spring Cloud Zookeeper:操作Zookeeper的工具包,用于使用zookeeper方式的服務注冊和發現。
Spring Cloud Stream:數據流操作開發包,封裝了與Redis,Rabbit、Kafka等發送接收消息。
Spring Cloud CLI:基于 Spring Boot CLI,可以讓你以命令行方式快速建立云組件。
?
注3:twelve-factor app methodology
(摘自:https://www.sitepoint.com/12-factor-apps-methodology-implement-apps-appfog/)
As made apparent by the title, the 12-Factor App methodology is a list of principles, each explaining the ideal way to handle a subset of your application. The 12 factors are as follows:
1. Codebase – One codebase tracked in revision control, many deploys
The first principle of the 12-Factor App methodology is related to your application’s codebase. The most important point here is to ensure that your application is tracked with revision control, and that it sits in a central repository that is accessible to your developers. This is most commonly handled by using Git or SVN to store your code.
2. Dependencies – Explicitly declare and isolate dependencies
There is no room for assumptions when it comes to dependencies. Anything your applications rely on to run should be controlled and managed to minimize — if not completely eliminate — conflicts.
3. Configuration – Store config in the environment
Configuration, as it relates to API keys, services, and database credentials, should never be hardcoded. This prevents your application from being at risk from both production data leaks and production errors. Instead of hardcoding this information, rely on environment variables to handle this sensitive information.
4. Backing Services – Treat backing services as attached resources
A backing service is one that requires a network connection to run. This is a very popular paradigm found in modern application development, especially prevalent with the rise in popularity of microservice architecture. The 12-Factor App methodology advises developers to treat these services agnostically, meaning changes or modifications should occur without having to make any code changes. Typically, this factor is best handled by calling each backing service through an API, with credentials stored in a configuration file that lives in your runtime environment.
5. Build, release, run – Strictly separate build and run stages
Build, release, and run stages should be treated as completely distinct from one another. Automation and tooling will help to make this principle simpler.
This can be accomplished by using existing tools to fully automate your build process. A tool like Github can be used to tag your latest build, while Jenkins can be used to automate your release stage.
6. Processes – Execute the app as one or more stateless processes
Stateless applications are designed to degrade gracefully. That means if a dependency fails, the app itself does not become a failure. Single points of failure may be difficult, but not impossible, to avoid. The 12-Factor App methodology recommends storing data outside of running code in order to prevent operational headaches and debugging nightmares.
7. Port binding – Export services via port binding
All application services should be accessible via a URL. For web applications, this process happens automatically. This enables 12-Factor Apps to be fully self-contained, avoiding the need to rely on various methods of runtime injection in order to create web facing services.
8. Concurrency – Scale out via the process model
Every process inside your application should be treated as a first-class citizen. That means that each process should be able to scale, restart, or clone itself when needed. This approach will improve the sustainability and scalability of your application as a whole.
9. Disposability – Maximize robustness with fast startup and graceful shutdown
As noted in the previous factor, treating processes as first-class citizens translates to an easier startup and shutdown process. Compare this to an application where all process are bundled together, where startup and shutdown processes can take up to several minutes depending on their size. To ensure your startup and shutdown processes remain seamless, reach for tried and true services that are optimized for speed and performance. Databases and caches like RabbitMQ, Redis, Memcached, and CenturyLink’s own Orchestrate are just a few services that are built to help with this factor.
10. Dev/prod Parity – Keep development, staging, and production as similar as possible
Consistency is key for meeting this factor. When your environments are similar, testing and developing gets much simpler. Similar environments means ensuring that areas such as your infrastructure stack, config management processes, software and runtime versions and deployment tools are the same everywhere. With this approach, fewer bugs will find their way into your production environment, since your test cases can be applied on production-level data.
11. Logs – Treat logs as event streams
Logging is important for debugging and checking up on the general health of your application. At the same time, your application shouldn’t concern itself with the storage of this information. Instead, these logs should be treated as a continuous stream that is captured and stored by a separate service.
12. Admin processes – Run admin/management tasks as one-off processes
One-off admin processes are essentially data collection jobs that are used to gather key information about your application. This information will be needed to asses the state of your production environment, so it’s important to ensure these one-off processes occur in your production environment. That way there can be no discrepancies between the data you need and the data coming from the visible long running production application.
?
簡介
(偷個懶兒,從網上摘個中文的)
如今,軟件通常會作為一種服務來交付,它們被稱為網絡應用程序,或軟件即服務(SaaS)。12-Factor 為構建如下的 SaaS 應用提供了方法論:
l? 使用標準化流程自動配置,從而使新的開發者花費最少的學習成本加入這個項目。
l? 和操作系統之間盡可能的劃清界限,在各個系統中提供最大的可移植性。
l? 適合部署在現代的云計算平臺,從而在服務器和系統管理方面節省資源。
l? 將開發環境和生產環境的差異降至最低,并使用持續交付實施敏捷開發。
l? 可以在工具、架構和開發流程不發生明顯變化的前提下實現擴展。
這套理論適用于任意語言和后端服務(數據庫、消息隊列、緩存等)開發的應用程序。
12-Factor:
I. 基準代碼
一份基準代碼,多份部署
II. 依賴
顯式聲明依賴關系
III. 配置
在環境中存儲配置
IV. 后端服務
把后端服務當作附加資源
V. 構建,發布,運行
嚴格分離構建和運行
VI. 進程
以一個或多個無狀態進程運行應用
VII. 端口綁定
通過端口綁定提供服務
VIII. 并發
通過進程模型進行擴展
IX. 易處理
快速啟動和優雅終止可最大化健壯性
X. 開發環境與線上環境等價
盡可能的保持開發,預發布,線上環境相同
XI. 日志
把日志當作事件流
XII. 管理進程
后臺管理任務當作一次性進程運行
?
注4:混合持久化
(摘自:http://www.cnblogs.com/leetieniu2014/p/5153967.html)
數據庫環境在過去的十多年里增長巨大。每個新的數據庫相較于其它數據庫,有著某些優勢,但是它們做了某種折衷。
事實上,CAP定理【注2】告訴我們,不可能擁有完美的數據庫,我們需要根據應用程序來選擇哪種折衷是可接受的。
帶有這些約束的工作場景符合Martin Fowler推廣的混合持久化思路。混合持久化的思路是指,你應該根據工作選擇合適的數據庫,這樣我們就能兩者兼得了。
我們的數據庫客戶端應該這樣做,它應該能夠與很多不同的數據庫交流。在代碼層面看起來是這樣的:
Client(mongo(details)).put(key, value);
Client(redis(details)).get(key);
?
注5:Neo4j簡介
Neo4j是一個高性能的,NOSQL圖形數據庫,它將結構化數據存儲在網絡上而不是表中。Neo4j也可以被看作是一個高性能的圖引擎,該引擎具有成熟數據庫的所有特性。程序員工作在一個面向對象的、靈活的網絡結構下而不是嚴格、靜態的表中——但是他們可以享受到具備完全的事務特性、企業級的數據庫的所有好處。
Neo4j因其嵌入式、高性能、輕量級等優勢,越來越受到關注。
?
注6:Consumer-Driven Contract
(摘自:http://servicedesignpatterns.com/WebServiceEvolution/ConsumerDrivenContracts)
The?Consumer-Driven Contract?pattern helps service owners create service APIs that reflect client needs; it also helps service owners evolve services without breaking existing clients. Service owners receive integration tests from each client and incorporate these tests into the service's test suite. The set of integration tests received from all existing clients represents the service's aggregate obligations with respect to its client base. The service owner is then free to change and evolve the service just so long as the existing integration tests continue to pass.
“消費者驅動的契約”模式,幫助服務的提供者能夠開發出反應客戶端需求的API;它還幫助服務提供者在不需要關閉客戶端的情況下升級服務程序。服務提供者接收來自客戶端的集成測試,并將這些測試結果整合進測試套件里。這樣一組來自現存客戶端的集成測試報告,反應出了在客戶端層面的集中反饋。隨著集成測試的進行,服務提供者就可以自由地修改和升級服務端程序。
?
生詞:
encounter? 遭遇
pattern??? 模式,模板
circuit??? 回路
circuit breaker?? 斷路器
ecosystem? 生態系統
tune?????? 調整
perfect??? 完美的
intelligent?? 聰明的,智能
illustrate (用圖解等)說明
recommend 介紹,建議
methodology?? (從事某一活動的)方法
factor ??? 因素、要素
multi-purpose 多用途
assume ??? 假設
stage ???? 舞臺
externalize????? 外部化
centralize???????????????? 集權控制
revise??????????????? 修改
endpoint????????? 端點
consumer??????? 消費者
fetch???????????????? 提取
maintain????????? 維護,保持
originate????????? 起源,創辦
annotation????? 注解,注釋
autowire????????? 自動裝配
hexagon?????????? 六邊形
inject??????????????? 注入
proxy??????????????? 代理
demonstrate?? 演示
cloud-native??? 原生云
polyglot?????????? 通曉多種語言的、多種語言混合的
approach????????????????? 靠近,接近
oppose???????????? 反抗,對抗
opposed?????????? 截然不同的,對立的
monolith????????? (尤指古人鑿成,有宗教意義的)單塊巨石
coordinate????? n. 坐標;v. 協調
equally???????????? 平等地
orchestrate???? 編配,組織
fork?????????????????? 餐叉
artifact???????????? 人工制品
spin 高速旋轉
flurry 一陣忙亂(或激動、興奮等)
dashboard?????? 控制面板,儀表盤
data-driven???? 數據驅動
front-end????????????????? 前端
back-end????????? 后端
mentorship????? 導師,指導
boiler??????????????? 鍋爐
plate???????????????? 盤子
evolve????????????? 發展,進化
incorporate???? 合并
aggregate??????? 合計,總數
obligation??????? 義務,職責
?
?
參考:
Spring cloud項目實踐(一)
http://sail-y.github.io/2016/03/21/Spring-cloud項目實踐/
總結
以上是生活随笔為你收集整理的用Spring Cloud和Docker搭建微服务平台的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: yoyo跑_全力冲刺 目标YOYO(图)
- 下一篇: 【工业机器人】工业机器人最全面基础知识科