前言
自動伸縮是每個人都想要的,尤其是在微服務(wù)領(lǐng)域。讓我們看看如何在基于spring boot的應(yīng)用程序中實(shí)現(xiàn)。
我們決定使用kubernetes、pivotal cloud foundry或hashicorp's nomad等工具的一個更重要的原因是為了讓系統(tǒng)可以自動伸縮。當(dāng)然,這些工具也提供了許多其他有用的功能,在這里,我們只是用它們來實(shí)現(xiàn)系統(tǒng)的自動伸縮。乍一看,這似乎很困難,但是,如果我們使用spring boot來構(gòu)建應(yīng)用程序,并使用jenkins來實(shí)現(xiàn)ci,那么就用不了太多工作。
今天,我將向您展示如何使用以下框架/工具實(shí)現(xiàn)這樣的解決方案:
- spring boot
- spring boot actuator
- spring cloud netflix eureka
- jenkins ci
它是如何工作的
每一個包含spring boot actuator庫的spring boot應(yīng)用程序都可以在/actuator/metrics端點(diǎn)下公開metric。許多有價值的metric都可以提供應(yīng)用程序運(yùn)行狀態(tài)的詳細(xì)信息。在討論自動伸縮時,其中一些metric可能特別重要:jvm、cpu metric、正在運(yùn)行的線程數(shù)和http請求數(shù)。有專門的jenkins流水線通過按一定頻率輪詢/actuator/metrics 端點(diǎn)來獲取應(yīng)用程序的指標(biāo)。如果監(jiān)控的任何metric【指標(biāo)】低于或高于目標(biāo)范圍,則它會啟動新實(shí)例或使用另一個actuator端點(diǎn)/actuator/shutdown來關(guān)閉一些正在運(yùn)行的實(shí)例。在此之前,我們需要知道當(dāng)前有那些實(shí)踐在提供服務(wù),只有這樣我們才能在需要的時候關(guān)閉空閑的實(shí)例或啟動新的新例。
在討論了系統(tǒng)架構(gòu)之后,我們就可以繼續(xù)開發(fā)了。這個應(yīng)用程序需要滿足以下要求:它必須有公開的可以優(yōu)雅地關(guān)閉應(yīng)用程序和用來獲取應(yīng)用程序運(yùn)行狀態(tài)metric【指標(biāo)】的端點(diǎn),它需要在啟動完成的同時就完成在eureka的注冊,在關(guān)閉時取消注冊,最后,它還應(yīng)該能夠從空閑端口池中隨機(jī)獲取一個可用的端口。感謝spring boot,只需要約五分鐘,我們可以輕松地實(shí)現(xiàn)所有這些機(jī)制。
動態(tài)端口分配
由于可以在一臺機(jī)器上運(yùn)行多個應(yīng)用程序?qū)嵗晕覀儽仨毐WC端口號不沖突。幸運(yùn)的是,spring boot為應(yīng)用程序提供了這樣的機(jī)制。我們只需要將application.yml中的server.port屬性設(shè)置為0。因?yàn)槲覀兊膽?yīng)用程序會在eureka中注冊,并且發(fā)送唯一的標(biāo)識instanceid,默認(rèn)情況下這個唯一標(biāo)識是將字段spring.cloud.client.hostname, spring.application.name和server.port拼接而成的。
示例應(yīng)用程序的當(dāng)前配置如下所示。
可以看到,我通過將端口號替換為隨機(jī)生成的數(shù)字來改變了生成instanceid字段值的模板。
1
2
3
4
5
6
7
8
|
spring: application: name: example-service server: port: ${port: 0 } eureka: instance: instanceid: ${spring.cloud.client.hostname}:${spring.application.name}:${random. int [ 1 , 999999 ]} |
啟用actuator的metric
為了啟用spring boot actuator,我們需要將下面的依賴添加到pom.xml。
1
2
3
4
|
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-actuator</artifactid> </dependency> |
我們還必須通過http api將屬性management.endpoints.web.exposure.include設(shè)置為'*'來暴露actuator的端點(diǎn)。現(xiàn)在,所有可用的指標(biāo)名稱列表都可以在/actuator/metrics端點(diǎn)中找到,每個指標(biāo)的詳細(xì)信息可以通過/actuator/metrics/{metricname}端點(diǎn)查看。
優(yōu)雅地停止應(yīng)用程序
除了查看metric端點(diǎn)外,spring boot actuator還提供了停止應(yīng)用程序的端點(diǎn)。然而,與其他端點(diǎn)不同的是,缺省情況下,此端點(diǎn)是不可用的。我們必須把management.endpoint.shutdown.enabled設(shè)為true。在那之后,我們就可以通過發(fā)送一個post請求到/actuator/shutdown端點(diǎn)來停止應(yīng)用程序了。
這種停止應(yīng)用程序的方法保證了服務(wù)在停止之前從eureka服務(wù)器注銷。
啟用eureka自動發(fā)現(xiàn)
eureka是最受歡迎的發(fā)現(xiàn)服務(wù)器,特別是使用spring cloud來構(gòu)建微服務(wù)的架構(gòu)。所以,如果你已經(jīng)有了微服務(wù),并且想要為他們提供自動伸縮機(jī)制,那么eureka將是一個自然的選擇。它包含每個應(yīng)用程序注冊實(shí)例的ip地址和端口號。為了啟用eureka客戶端,您只需要將下面的依賴項(xiàng)添加到pom.xml中。
1
2
3
4
|
<dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-netflix-eureka-client</artifactid> </dependency> |
正如之前提到的,我們還必須保證通過客戶端應(yīng)用程序發(fā)送到eureka服務(wù)器的instanceid的唯一性。在“動態(tài)端口分配”中已經(jīng)描述了它。
下一步需要創(chuàng)建一個包含內(nèi)嵌eureka服務(wù)器的應(yīng)用程序。為了實(shí)現(xiàn)這個功能,首先我們需要在pom.xml中添加下面這個依賴:
1
2
3
4
|
<dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-netflix-eureka-server</artifactid> </dependency> |
這個main類需要添加@enableeurekaserver注解。
1
2
3
4
5
6
7
|
@springbootapplication @enableeurekaserver public class discoveryapp { public static void main(string[] args) { new springapplicationbuilder(discoveryapp. class ).run(args); } } |
默認(rèn)情況下,客戶端應(yīng)用程序嘗試使用8761端口連接eureka服務(wù)器。我們只需要單獨(dú)的、獨(dú)立的eureka節(jié)點(diǎn),因此我們將禁用注冊,并嘗試從另一個eureka服務(wù)器實(shí)例中獲取服務(wù)列表。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
spring: application: name: discovery-service server: port: ${port: 8761 } eureka: instance: hostname: localhost client: registerwitheureka: false fetchregistry: false serviceurl: defaultzone: http: //localhost:8761/eureka/ |
我們將使用docker容器來測試上面的自動伸縮系統(tǒng),因此需要使用eureka服務(wù)器來準(zhǔn)備和構(gòu)建image。
dockerfile和image的定義如下所示。
我們可以使用命令docker build -t piomin/discovery-server:2.0來進(jìn)行構(gòu)建。
1
2
3
4
5
6
7
8
|
from openjdk: 8 -jre-alpine env app_file discovery-service- 1.0 -snapshot.jar env app_home /usr/apps expose 8761 copy target/$app_file $app_home/ workdir $app_home entrypoint [ "sh" , "-c" ] cmd [ "exec java -jar $app_file" ] |
為彈性伸縮構(gòu)建一個jenkins流水線
第一步是準(zhǔn)備jenkins流水線,負(fù)責(zé)自動伸縮。我們將創(chuàng)建jenkins聲明式流水線,它每分鐘運(yùn)行一次。可以使用triggers指令配置執(zhí)行周期,它定義了自動化觸發(fā)流水線的方法。我們的流水線將與eureka服務(wù)器和每個使用spring boot actuator的微服務(wù)中公開的metric端點(diǎn)進(jìn)行通信。
測試服務(wù)的名稱是example-service,它和定義在application.yml文件spring.application.name的屬性值(大寫字母)相同。被監(jiān)控的metric是運(yùn)行在tomcat容器中的http listener線程數(shù)。這些線程負(fù)責(zé)處理客戶端的http請求。
1
2
3
4
5
6
7
8
9
10
11
12
|
pipeline { agent any triggers { cron( '* * * * *' ) } environment { service_name = "example-service" metrics_endpoint = "/actuator/metrics/tomcat.threads.busy?tag=name:http-nio-auto-1" shutdown_endpoint = "/actuator/shutdown" } stages { ... } } |
使用eureka整合jenkins流水線
流水線的第一個階段負(fù)責(zé)獲取在discovery服務(wù)器上注冊的服務(wù)列表。eureka發(fā)現(xiàn)了幾個http api端點(diǎn)。其中一個是get /eureka/apps/{servicename},它返回一個給定服務(wù)名稱的所有活動實(shí)例列表。我們正在保存運(yùn)行實(shí)例的數(shù)量和每個實(shí)例metric端點(diǎn)的url。這些值將在流水線的下一個階段中被訪問。
下面的流水線片段可以用來獲取活動應(yīng)用程序?qū)嵗斜怼tage名稱是calculate。我們使用http請求插件 來發(fā)起http連接。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
stage( 'calculate' ) { steps { script { def response = httprequest "http://192.168.99.100:8761/eureka/apps/${env.service_name}" def app = printxml(response.content) def index = 0 env[ "instance_count" ] = app.instance.size() app.instance.each { if (it.status == 'up' ) { def address = "http://${it.ipaddr}:${it.port}" env[ "instance_${index++}" ] = address } } } } } @noncps def printxml(string text) { return new xmlslurper( false , false ).parsetext(text) } |
下面是eureka api對我們的微服務(wù)的示例響應(yīng)。響應(yīng)content-type是xml。
使用spring boot actuator整合jenkins流水線
spring boot actuator使用metric來公開端點(diǎn),這使得我們可以通過名稱和選擇性地使用標(biāo)簽找到metric。在下面可見的流水線片段中,我試圖找到metric低于或高于閾值的實(shí)例。如果有這樣的實(shí)例,我們就停止循環(huán),以便進(jìn)入下一個階段,它執(zhí)行向下或向上的伸縮。應(yīng)用程序的ip地址是從帶有instance_前綴的流水線環(huán)境變量獲取的,這是在前一階段中被保存了下來的。
1
2
3
4
5
6
7
8
9
10
11
|
stage( 'metrics' ) { steps { script { def count = env.instance_count for (def i= 0 ;i 100 ) return "up" else if (value.tointeger() < 20 ) return "down" else return "none" } |
關(guān)閉應(yīng)用程序?qū)嵗?/strong>
在流水線的最后一個階段,我們將關(guān)閉運(yùn)行的實(shí)例,或者根據(jù)在前一階段保存的結(jié)果啟動新的實(shí)例。通過調(diào)用spring boot actuator端點(diǎn)可以很容易執(zhí)行停止操作。在接下來的流水線片段中,首先選擇了eureka實(shí)例。然后我們將發(fā)送post請求到那個ip地址。
如果需要擴(kuò)展應(yīng)用程序,我們將調(diào)用另一個流水線,它負(fù)責(zé)構(gòu)建fat jar并讓這個應(yīng)用程序在機(jī)器上跑起來。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
stage( 'scaling' ) { steps { script { if (env.scale_type == 'down' ) { def ip = env[ "instance_0" ] + env.shutdown_endpoint httprequest url: ip, contenttype: 'application_json' , httpmode: 'post' } else if (env.scale_type == 'up' ) { build job: 'spring-boot-run-pipeline' } currentbuild.description = env.scale_type } } } |
下面是spring-boot-run-pipeline流水線的完整定義,它負(fù)責(zé)啟動應(yīng)用程序的新實(shí)例。它先從git倉庫中拉取源代碼,然后使用maven命令編譯并構(gòu)建二進(jìn)制的jar文件,最后通過在java -jar命令中添加eureka服務(wù)器地址來運(yùn)行應(yīng)用程序。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
pipeline { agent any tools { maven 'm3' } stages { stage( 'checkout' ) { steps { git url: 'https://github.com/piomin/sample-spring-boot-autoscaler.git' , credentialsid: 'github-piomin' , branch: 'master' } } stage( 'build' ) { steps { dir( 'example-service' ) { sh 'mvn clean package' } } } stage( 'run' ) { steps { dir( 'example-service' ) { sh 'nohup java -jar -deureka_url=http://192.168.99.100:8761/eureka target/example-service-1.0-snapshot.jar 1>/dev/null 2>logs/runlog &' } } } } } |
擴(kuò)展到多個機(jī)器
在前幾節(jié)中討論的算法只適用于在單個機(jī)器上啟動的微服務(wù)。如果希望將它擴(kuò)展到更多的機(jī)器上,我們將不得不修改我們的架構(gòu),如下所示。每臺機(jī)器都有jenkins代理運(yùn)行并與jenkins master通信。如果想在選定的機(jī)器上啟動一個微服務(wù)的新實(shí)例,我們就必須使用運(yùn)行在該機(jī)器上的代理來運(yùn)行流水線。此代理僅負(fù)責(zé)從源代碼構(gòu)建應(yīng)用程序并將其啟動到目標(biāo)機(jī)器上。這個實(shí)例的關(guān)閉仍然是通過調(diào)用http端點(diǎn)來完成。
假設(shè)我們已經(jīng)成功地在目標(biāo)機(jī)器上啟動了一些代理,我們需要對流水線進(jìn)行參數(shù)化,以便能夠動態(tài)地選擇代理(以及目標(biāo)機(jī)器)。
當(dāng)擴(kuò)容應(yīng)用程序時,我們必須將代理標(biāo)簽傳遞給下游流水線。
1
|
build job: 'spring-boot-run-pipeline' , parameters:[string(name: 'agent' , value: "slave-1" )] |
調(diào)用流水線具體由那個標(biāo)簽下的代理運(yùn)行,是由"${params.agent}"決定的。
1
2
3
4
5
6
|
pipeline { agent { label "${params.agent}" } stages { ... } } |
如果有一個以上的代理連接到主節(jié)點(diǎn),我們就可以將它們的地址映射到標(biāo)簽中。由于這一點(diǎn),我們能夠?qū)膃ureka服務(wù)器獲取的微服務(wù)實(shí)例的ip地址映射到與jenkins代理的目標(biāo)機(jī)器上。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
pipeline { agent any triggers { cron( '* * * * *' ) } environment { service_name = "example-service" metrics_endpoint = "/actuator/metrics/tomcat.threads.busy?tag=name:http-nio-auto-1" shutdown_endpoint = "/actuator/shutdown" agent_192. 168.99 . 102 = "slave-1" agent_192. 168.99 . 103 = "slave-2" } stages { ... } } |
總結(jié)
在本文中,我演示了如何使用spring boot actuato metric來自動伸縮spring boot應(yīng)用程序。使用spring boot提供的特性以及spring cloud netflix eureka和jenkins,您就可以實(shí)現(xiàn)系統(tǒng)的自動伸縮,而無需借助于任何其他第三方工具。本文也假設(shè)遠(yuǎn)程服務(wù)器上也是使用jenkins代理來啟動新的實(shí)例,但是您也可以使用ansible這樣的工具來啟動。如果您決定從jenkins運(yùn)行ansible腳本,那么將不需要在遠(yuǎn)程機(jī)器上啟動jenkins代理。
好了,以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對服務(wù)器之家的支持。
原文鏈接:http://blog.51cto.com/14010829/2299257