Chuyển đến nội dung chính

Spring cloud - bootstrapping là gì ? Xây dựng một ứng dụng sử dụng spring cloud cơ bản.



Kết quả hình ảnh cho spring boot eureka
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. 
Trong bài viết này, chúng ta sẽ build 5 microservcie, 1 config server và 1 discovery server, 1 gateway server, 1 book service và một rating service.


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:run 
Nế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:


Fetching config from server at: http://localhost:8081
...
Tomcat started on port(s): 8082 (http)

Nếu các service register vào eureka thành công bạn sẽ thấy log:

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
IV: Gateway:

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

Bài đăng phổ biến từ blog này

Cách sử dụng sys.argv trong python.

Cách sử dụng sys.argv trong python. sys.argv là môt danh sách [list] trong python, nó được sư dụng khi bạn chạy một lệnh command-line nào đó trên hệ thống. Và argument này được đẩy vào script python để thực thi khi chạy câu lệnh. Ví dụ: python sys.argv arg1 arg2 Trước tiên bạn phải import mô đun sys trong script. import sys print "This is the name of the script: " , sys . argv [ 0 ] print "Number of arguments: " , len ( sys . argv ) print "The arguments are: " , str ( sys . argv ) Tên của script này : sysargv.py Số lượng arg là : 1 Arg là : ['sysargv.py'] python test1020.py 111 This is the name of the script:  test1020.py Number of arguments:  2 The arguments are:  ['test1020.py', '111']

GitHub Actions là gì ? Làm quen với GitHub Actions.

  Như ta đã biết github là nới chứa source code nổi tiếng thế giới hiện nay, ngoài github còn có gitlab, bitbucket, codecommit, ... Cơ bản github miễn phí cho người dùng developer, nếu nhu cầu sử dụng nhiều repo cũng như project có nhiều thanh viên developer tham gia thì bạn có thể mua bản nâng cao.  Về tiến trình CI/CD process, chúng ta có thể biết tới như Jenkins, Team City, Codepipeline trên AWS, ...  GitHub Actions mới ra đời gần đây , ngày 13 tháng 11 năm 2019, GitHub Actions ra phiên bản đầu tiên, trước đó khoảng 1 năm bản beta ra đời.  Tham khảo tại đây : https://github.blog/2019-08-08-github-actions-now-supports-ci-cd/ GitHub Actions khá đơn giản, khi tiến trình build, test và deploy được viết trong một file có định dạng yaml nằm trong thư mục .github/workflows/ trong chính source code của bạn.  GitHub Actions là một event-driven nghĩa là chương trình chạy một loạt các dòng lệnh khi bạn nhận được 1 event, ví dụ mỗi lần một developer nào đó tạo một pull request cho một repositor

Trang web medium.com chết, vì sao ?

 Medium.com là trang web có những bài viết về IT, phần mềm và công nghệ nói chung rất phổ biến và chất lượng.  Nhưng khoảng 1 tuần nay không vào được bằng cả mạng viettel và 4G của Mobi phone.  Dân It thường tham khảo các bài viết trên trang này. Nhiều bài viết hay và chất lượng. là một cơ sở dữ liệu lớn cho dân IT nhưng đã bị chặn.  Các bài viết này thường thì có tính quy chuẩn và dài hơn, các topic có chất lượng hơn so với stackoverflow.  Nhưng các subdomain thì vẫn truy cập được như:  https://about.medium.com Bạn có thể tham khảo lý do vì sao medium.com bị chặn ở Việt nam. tại trang tinh tế:  Nói chung mình không thích điều này.  https://tinhte.vn/thread/website-medium-khong-truy-cap-duoc-la-do-website-chet-hay-chan-ip-viet-nam-nhi.3231608/