Professional Documents
Culture Documents
BÀI TH C HÀNH
H C PH N: H PHÂN TÁN
1: T NG QUAN VÀ KI N TRÚC HPT
Trên l ch t r ng t t c các
d ch v m u s d ng lý thuy t c a H Phân Tán. bài th c hành này các b n s
th tri n khai d ch v WWW. C th , các b n s t 1 web server.
1.2. Yêu c u
1.2.1. Lý thuy t
Làm ch Unix OS
Ki n th n c a M ng máy tính
1.2.2. Thi t b
PC ho c VM
1.2.3. Ph n m m
1.3. c th c hành
1.3.1. t web server apache2
1
H PHÂN TÁN
i quy b ng l nh sau:
Vi t n nh s a n i dung c
m c public_html mà b n v a t
<html>
<head>
<title>Welcome to Example.com!</title>
</head>
<body>
<h1>Success! The example.com virtual host is working!</h1>
</body>
</html>
/etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
ServerAdmin admin@example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerAdmin admin@test.com
ServerName test.com
ServerAlias www.test.com
DocumentRoot /var/www/test.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
2
H PHÂN TÁN
Kh ng l i d ch v apache:
sudo service apache2 restart
127.0.0.1 example.com
127.0.0.1 test.com
Câu h i 5: Th truy c p t các máy tính khác trong cùng m ng LAN vào 2 trang web
2.1. N i dung
2.2. Yêu c u
2.2.1. Lý thuy t
L p trình Java
2.2.2. Thi t b
PC
2.2.3. Ph n m m
Eclipse IDE
JDK/JRE
2.3. c th c hành
3
H PHÂN TÁN
2.3.2. Xây d
u tiên, m Eclipse và t o m t java project m i v i vi c ch n File New Java
project. Hãy t
Sau khi t o xong, b n s nhìn th y m c tên là src ch a
mã ngu n.
T ng cách n ph i chu c src
ch n New Package
t tên 2
com.hust.soict.your_name.client_server
com.hust.soict.your_name.helper
ng tên b n)
2.3.2.1. Client
Hãy m file Client.java ra và b u l p trình.
y tiên chúng ta ph i import các l p c a b n Java:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
B n có th thay th a ch IP và s hi u c n mu a
ch IP và c ng mà server s l ng nghe các yêu c u mà Client s g i lên.
Bây gi chúng ta s kh i t o 2 th c th c a 2 l p BufferedReader và PrintWriter
g i và nh n d li u:
Kh i t o 1 th c th cho l p Scanner:
System.out.println(in.readLine());
Scanner scanner = new Scanner(System.in);
4
H PHÂN TÁN
Cu socket và scanner l i:
socket.close();
scanner.close();
2.3.2.2. Server
Bây gi hãy m và ch nh s a file Server.java. M ng m
lu nh n các s i dùng g i lên và s p x p nó theo th t n (ho c
nh d n) và g i tr l i cho Client.
u tiên hãy import m t s n Java c n thi t:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import com.hust.soict.haianh.helper.*;
import java.util.Arrays;
c main, hãy vi
5
H PHÂN TÁN
// Get messages from the client, line by line; Each line has several numbers separated by a space
character
while (true) {
String input = in.readLine();
if (input == null || input.isEmpty()) {
break;
}
//Put it in a string array
String[] nums = input.split(" ");
int i = 0;
n có th th c g i thu c l p
SelectionSort. Bây gi chúng ta s xây d ng m t giao di n (interface) và khai báo
c sort bên trong. L p SelectionSort là m t trong nh ng l p tri n khai
giao di n này.
c sort:
V om tl t tên là SelectionSort.
6
H PHÂN TÁN
2.3.2.4. Ch
Bây gi hãy ch y th ng quên là ph i ch c khi ch y
client.
Bubble sort
Insertion sort
Shell sort
3. Ki n trúc Microservices
3.1. N i dung
bài th c hành này chúng ta s xây d ng m t ki n
b ng cách s d ng Kubernetes, m t n n t ng m qu n lý nh ng d ch v d ng
container. Chúng ta s s d t o ra các containers.
7
H PHÂN TÁN
3.2. Yêu c u
3.2.1. Lý thuy t
Microservices
Kube fundamentals: Pods, Services, Deployments et al.
Docker
3.2.2. Ph n c ng
Laptop/PC on Windows
3.2.3. Ph n m m
VirtualBox
Docker
The kubernetes command line tool kubectl
The minikube binary
Git bash
3.3. c th c hành
t
- t VirtualBox: https://www.virtualbox.org/wiki/Downloads
- t kubectl trên Windows, chúng ta s c n ph t Chocolatey c:
https://chocolatey.org/docs/installation#installing-chocolatey
- Bây gi t công c kubernetes v i l nh sau:
>choco install kubernetes-cli
Ki m tra l ng l nh sau:
>kubectl version
- Bây gi hãy cài minikube-windows-amd64.exe:
https://github.com/kubernetes/minikube/releases
t tên l i cho file v a t i là minikube.exe và thêm nó vào Path. (N
bi t thêm bi ng vào Path th ng d n link sau:
https://www.java.com/en/download/help/path.xml)
- i v i b n nào dùng Windows thì s b v khi cài song song Docker và
VirtualBox là t i vì Docker trên Windows thì c n Hyper-V, còn VirtualBox thì l i
không ho c v i Hyper-V. Vì v y, gi d ng
ng thì hãy s d ng Docker Toolbox:
https://docs.docker.com/toolbox/overview/
S d ng ki xây d ng ng d ng web
8
H PHÂN TÁN
S d c mà b n mu t ct iv :
c microservices-demo, ch y l nh sau:
./mvnw clean package -Dmaven.test.skip=true
ho c n u dùng Windows thì ch y l nh:
mvnw.cmd clean package -Dmaven.test.skip=true
Nh ng l build/re-build 3 d ch v trên.
n c n ph i lên t o tài kho n trên Docker Hub: https://hub.docker.com/
9
H PHÂN TÁN
>docker login
i v i d ch v apache:
>docker build --tag=microservice-kubernetes-demo-apache apache
>minikube start
10
H PHÂN TÁN
11
H PHÂN TÁN
và t t cluster:
>minikube stop
12