
I. Khái quát:
Spring cloud là một framework để xây dựng một ứng dụng trên nền tảng đám mây rất mạnh mẽ. Khi chuyển qua môi trường phân tán (distributed env ) các framework thường đối mặt với các vấn đề chung. và framework này cung các các giải pháp để giải quyết những vấn đề đó.
Các ứng dụng chạy với kiến trúc microservices thường mong muốn cho việc phát triển, triển khai và bảo trì một cách đơn giản nhất. Việc phân tích, phân thành mảng nhỏ các ứng dụng cho phép lập trình viên tập trung giải quyết một vấn đề trong một thời điểm. và không ảnh hưởng đến các phần khác của hệ thống.
Mặt khác những thử thách khác nhau khi chúng ta tiến tới sử dụng kiến trúc microservices:
- Những cấu hình bên ngoài là linh động và không cần build lại dịch vụ.
- Service discovery (dịch vụ quan sát và phát hiện )
- Ẩn các dịch vụ phức tạp được deploy lên các host khác nhau.
II. Config server:
Để thuận tiện cho việc lấy file pom.xml chúng ta có thể vào : start.spring.io
Và generate một file.
Hoặc bạn có thề dùng các tool khác để generate ra một file pom.xml như sau:
<
parent
>
<
groupId
>org.springframework.boot</
groupId
>
<
artifactId
>spring-boot-starter-parent</
artifactId
>
<
version
>1.4.0.RELEASE</
version
>
<
relativePath
/>
</
parent
>
<
dependencies
>
<
dependency
>
<
groupId
>org.springframework.boot</
groupId
>
<
artifactId
>spring-boot-starter-test</
artifactId
>
<
scope
>test</
scope
>
</
dependency
>
</
dependencies
>
<
dependencyManagement
>
<
dependencies
>
<
dependency
>
<
groupId
>org.springframework.cloud</
groupId
>
<
artifactId
>spring-cloud-dependencies</
artifactId
>
<
version
>Brixton.SR5</
version
>
<
type
>pom</
type
>
<
scope
>import</
scope
>
</
dependency
>
</
dependencies
>
</
dependencyManagement
>
<
build
>
<
plugins
>
<
plugin
>
<
groupId
>org.springframework.boot</
groupId
>
<
artifactId
>spring-boot-maven-plugin</
artifactId
>
</
plugin
>
</
plugins
>
</
build
>
Thêm dependency cho configserver vào file pom.
<
dependency
>
<
groupId
>org.springframework.cloud</
groupId
>
<
artifactId
>spring-cloud-config-server</
artifactId
>
</
dependency
>
Để bật cấu hình configserver trong code bạn thêm vào main application class:
@SpringBootApplication
@EnableConfigServer
public
class
ConfigApplication {...}
@EnableConfigServer : sẽ bật tính năng configserver cho ứng dụng.
Sau đó thêm tính năng vào trong file :application.properties trong /src/main/resources
server.port=8081
spring.application.name=config
spring.cloud.config.server.git.uri=file://${user.home}/application-config
Khởi tạo một repo git, cd vào folder application-config sau đó : git init
Để chạy config server bạn chạy lệnh sau :
mvn spring-boot:runNếu chạy thành công config server bạn nhận được output :
Tomcat started on port(s): 8081 (http)
Bootstrapping configuration:
Chúng ta mong muốn là config server sẽ quản lý các thông số properties của ứng dụng.
Đó gọi là bootstrap.properties hay bootstrap.yml.
File bootstrap.yml sẽ được đọc đầu tiên là sau đó configserver sẽ được chạy để lấy application.properties từ git về .
ConfigServer cũng có chức năng encrypt và decrypt các thông số cấu hình cần bảo mật.
III:Discovery:
Discovery service dùng để tập trung các server nói chuyện với nhau và service đó chúng ta đang sử dũng là Eureka.
Discovery service này còn có thể sử dụng trong kiến trúc load- balancing cho ứng dụng.
III.1 : Cấu hình ứng dụng springboot :
Bạn có thể vào trang start.spring.iođể tạo, còn cách khác bạn có thể tạo fiele pom.xml như sau:
<
dependency
>
<
groupId
>org.springframework.cloud</
groupId
>
<
artifactId
>spring-cloud-starter-config</
artifactId
>
</
dependency
>
<
dependency
>
<
groupId
>org.springframework.cloud</
groupId
>
<
artifactId
>spring-cloud-starter-eureka-server</
artifactId
>
</
dependency
>
III.2 Spring Config:
Thêm vào ứng dụng java main class của eureka:
@SpringBootApplication
@EnableEurekaServer
public
class
DiscoveryApplication {...}
Trong file bootstrap.properties trong src/main/resources
thêm vào :
spring.cloud.config.name=discovery
spring.cloud.config.uri=http://localhost:8081
Trên git repo ta tạo file discovery.properties có nội dung:
spring.application.name=discovery
server.port=8082
eureka.instance.hostname=localhost
eureka.client.serviceUrl.defaultZone=http://localhost:8082/eureka/
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
Chú ý là spring.application.name phải giống với tên file discovery
Thêm Dependency vào configserver:
<
dependency
>
<
groupId
>org.springframework.cloud</
groupId
>
<
artifactId
>spring-cloud-starter-eureka</
artifactId
>
</
dependency
>
Trong application.properties add thông tin vào :
eureka.client.region = default
eureka.client.registryFetchIntervalSeconds = 5 eureka.client.serviceUrl.defaultZone=http://localhost:8082/eureka/
Chạy Eureka dùng lệnh : mvn spring-boot:run
Output khi chạy thành công:
Nếu các service register vào eureka thành công bạn sẽ thấy log:
IV: Gateway:DiscoveryClient_CONFIG/10.1.10.235:config:8081: registering service...
Tomcat started on port(s): 8081 (http)
DiscoveryClient_CONFIG/10.1.10.235:config:8081 - registration status: 204
Như chúng ta đã biết thì chúng ta xây dựng trên hệ thống phân tán, để các client truy cập vào tất cả các ứng dụng thì ta nên xây một gateway.
Chúng ta phải sẽ quản lý các CORS header phức tạp cho phép những cross-origin từ phía client truy cập vào.
Một gateway server đáp ứng được nhu cầu trên và ngoài ra cung cấp khả năng chứng thực cho các request thông qua authentication.
IV.1 Cài đặt:
Bạn có thể vào : start.spring.io chọn artifact là "gateway", search là "zuul", "config client" và "eureka discovery" để tạo một ứng dụng Spring boot.
Hoặc bạn có thể tạo với file pom.xml là :
<
dependency
>
<
groupId
>org.springframework.cloud</
groupId
>
<
artifactId
>spring-cloud-starter-config</
artifactId
>
</
dependency
>
<
dependency
>
<
groupId
>org.springframework.cloud</
groupId
>
<
artifactId
>spring-cloud-starter-eureka</
artifactId
>
</
dependency
>
<
dependency
>
<
groupId
>org.springframework.cloud</
groupId
>
<
artifactId
>spring-cloud-starter-zuul</
artifactId
>
</
dependency
>
IV.2 Spring config: Thêm vào trong main class của ứng dụng gateway.
@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
public
class
GatewayApplication {...}
IV.3 Các properties.
File boottrap chúng ta thêm các thông số:
spring.cloud.config.name=gateway
spring.cloud.config.discovery.service-id=config
spring.cloud.config.discovery.enabled=true
eureka.client.serviceUrl.defaultZone=http://localhost:8082/eureka/
Và gateway.properties chúng ta thêm :
spring.application.name=gateway
server.port=8080
eureka.client.region = default
eureka.client.registryFetchIntervalSeconds = 5
zuul.routes.book-service.path=/book-service/**
zuul.routes.book-service.sensitive-headers=Set-Cookie,Authorization
hystrix.command.book-service.execution.isolation.thread.timeoutInMilliseconds=600000
zuul.routes.rating-service.path=/rating-service/**
zuul.routes.rating-service.sensitive-headers=Set-Cookie,Authorization
hystrix.command.rating-service.execution.isolation.thread.timeoutInMilliseconds=600000
zuul.routes.discovery.path=/discovery/**
zuul.routes.discovery.sensitive-headers=Set-Cookie,Authorization
zuul.routes.discovery.url=http://localhost:8082
hystrix.command.discovery.execution.isolation.thread.timeoutInMilliseconds=600000
Trong gateway.properties chúng ta định nghĩa các uri cho các API gọi vào như API cho book-service và rating-service
IV.4 :Chạy ứng dụng:
Sau khi chạy ứng dụng gateway ta thấy được output ra ngoài:
Fetching config from server at: http://10.1.10.235:8081/
...
DiscoveryClient_GATEWAY/10.1.10.235:gateway:8080: registering service...
DiscoveryClient_GATEWAY/10.1.10.235:gateway:8080 - registration status: 204
Tomcat started on port(s): 8080 (http)
Ứng dụng chạy trên port 8080
------còn tiếp-----
Nhận xét
Đăng nhận xét