歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> 學習Linux >> 利用Docker輕松實現雲原生應用

利用Docker輕松實現雲原生應用

日期:2017/3/3 17:36:37   编辑:學習Linux

利用Docker輕松實現雲原生應用


熱度4 評論 249 www.BkJia.Com 網友分享於: 2017-02-10 07:02:48 浏覽數43120次

利用Docker輕松實現雲原生應用


導讀前文談到了雲原生應用在部署架構中需要考慮的重要問題。文本將介紹一個常見的應用架構模式來提升應用的可用性和可伸縮性 - 分布式會話管理。並利用Docker和阿裡雲容器服務在本地和雲端進行了示例應用的部署和驗證。

利用Docker輕松實現雲原生應用利用Docker輕松實現雲原生應用

前文談到了雲原生應用在部署架構中需要考慮的重要問題。文本將介紹一個常見的應用架構模式來提升應用的可用性和可伸縮性 - 分布式會話管理。

隨著業務增長,Web應用也從單節點部署演變為集群部署。這時候除了需要為應用服務器增加負載均衡之外,也要解決會話(session)管理的問題。Session在應用中常被用於存儲用戶相關的數據。在高可用系統中,如果一台應用服務宕機,其他服務器需要能夠接管當前活躍的會話,繼續為用戶提供服務。所以我們必須提供分布式的會話管理能力。

Spring/Spring Boot應用中利用Spring Session配合Redis是一個流行的分布式的會話管理方案,它有如下幾個優點:

  1. 將session所持久化狀態從應用服務器本地內存卸載到外部Redis服務中,可以提升應用的可用性
  2. 應用服務器是無狀態的,滿足 12-factor 的要求,可以更好地支持應用水平擴展

關於 Spring Session 的信息可以從官方文檔獲得

利用Docker來本地測試Spring Boot + Redis會話存儲

首先從Github獲得示例代碼

class="hljs stata" data-language="">git clone https://github.com/denverdino/docker-spring-boot-sample-session-rediscd docker-spring-boot-sample-session-redis

本工程基於Spring Boot使用Redis會話存儲的官方示例,並做簡單調整如下:

其中 /src/main/resources/application.properties 內容:

class="hljs stylus" data-language="">spring.session.store-type=redisserver.session.timeout=5spring.redis.host=redisspring.redis.port=6379

它會配置基於Redis的分布式會話存儲支持,會話超時時間為“5”秒,Redis服務域名為“redis”,端口 “6379”。

我們再查看項目的Maven配置文件pom.xml

class="hljs dust" data-language=""><?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <parent>        <!-- Your own application should inherit from spring-boot-starter-parent -->        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.4.1.RELEASE</version>    </parent>    <artifactId>spring-boot-sample-session-redis</artifactId>    <name>Spring Boot Session Redis Sample</name>    <description>Spring Boot Session Redis Sample</description>    <url>http://projects.spring.io/spring-boot/</url>    <organization>        <name>Pivotal Software, Inc.</name>        <url>http://www.spring.io</url>    </organization>    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-data-redis</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.session</groupId>            <artifactId>spring-session</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>            <plugin>                <groupId>com.spotify</groupId>                <artifactId>docker-maven-plugin</artifactId>                <version>0.4.13</version>                <configuration>                    <imageName>registry.cn-hangzhou.aliyuncs.com/denverdino/spring-boot-session-redis</imageName>                    <baseImage>openjdk:8-jre</baseImage>                    <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>                    <resources>                       <resource>                         <targetPath>/</targetPath>                         <directory>${project.build.directory}</directory>                         <include>${project.build.finalName}.jar</include>                       </resource>                    </resources>                </configuration>            </plugin>        </plugins>    </build></project>

其中它除了聲明了對Spring Boot框架的支持,還添加了來自Spotify的

docker-maven-plugin

構建應用的Docker鏡像

我們可以來用maven命令來構建Spring Boot示例應用(參數是為了跳過單元測試)

class="hljs stylus" data-language="">mvn package -Dmaven.test.skip=true

也可以利用maven命令構建示例應用的Docker鏡像

class="hljs armasm" data-language="">mvn package docker:build -Dmaven.test.skip=true

注:關於“docker-maven-plugin”插件的更多信息,可以參考容器化Spring Boot應用一文

構建鏡像完成,可以使用如下docker-compose.yml模板來在本地部署和測試應用。其中web服務會啟動Spring Boot測試應用,並利用容器鏈接通過“redis”別名來訪問redis服務中的Redis容器

class="hljs vim" data-language="">web:  image: registry.cn-hangzhou.aliyuncs.com/denverdino/spring-boot-session-redis  ports:    - 8080:8080  links:    - redis:redisredis:  image: redis:3

執行docker-compose up 命令之後,我們就會看到“redis”容器和“web”應用容器相繼啟動

我們可以通過浏覽器來訪問http://localhost:8080/測試應用。如果會話不存在或會話超時(5秒鐘),應用會生成一個UUID保存在會話中並返回;如果會話存在,則會直接返回會話中保存的UUID。

利用容器服務來部署分布式會話管理應用

阿裡雲容器服務提供了對分布式的Docker應用的部署和管理能力。我們可以簡單地擴展之前的Docker Compose模板來在雲端部署應用。

class="hljs vim" data-language="">web:  image: registry.cn-hangzhou.aliyuncs.com/denverdino/spring-boot-session-redis  labels:    aliyun.scale: "3"    aliyun.routing.port_8080: spring-boot  links:    - redis:redisredis:  image: redis:3

注:

  1. 通過aliyun.xxx標簽聲明了,web應用由3個容器組成應用集群,並可以通過虛擬域名“spring-boot”進行訪問
  2. 通過路由服務來訪問應用是無需使用ports對容器端口進行宿主機端口映射的部署之後,web服務在控制台的截圖如下,可以直接點擊訪問端點來驗證應用。

利用Docker輕松實現雲原生應用利用Docker輕松實現雲原生應用

總結

在雲原生應用中,分布式會話管理是一個重要的應用模式。本文利用Docker和阿裡雲容器服務在本地和雲端進行了示例應用的部署和驗證。

如果大家有興趣,還可以將容器模板中的Redis鏡像替換成阿裡雲提供的Redis服務,這樣可以在生產系統中提供更好的可用性和和可伸縮性。可以參考在阿裡雲容器服務中使用RDS

原文來自:https://yq.aliyun.com/articles/65272?utm_campaign=wenzhang&utm_medium=article&utm_source=QQ-qun&utm_content=m_8036

本文地址:http://www.linuxprobe.com/docker-cloud-native.html


http://www.bkjia.com/Linuxjc/1191928.html TechArticle

Copyright © Linux教程網 All Rights Reserved