පසුගිය ලිපියෙන් අපි ආරම්භ කලා කොහොමද Spring microservices, Azure වෙතට Deploy කරන්නේ කියන කාරණාව පිලිබඳ ඉගෙන ගැනීම, ඒ වගේම සැලකිය යුතු දුරකට අපි එම ලිපියෙන් ඒ ගැන කතා කලා. මෙතනින් ඒ ලිපිය කියවලම ඔබට එහි ඉතුරු ටික අද ලිපියෙන් ඉගෙන ගන්න පුළුවන්.
දැන් තමයි අපි අපේ Spring Boot microservice එක නිර්මාණය කරන්න යන්නේ.මේ සඳහා පහත පියවරවල් අනුගමනය කරන්න.
මේ සඳහා https://start.spring.io/ එක පහත command එක සමගින් අපි භාවිතා කරනවා.
curl https://start.spring.io/starter.tgz -d dependencies=web,mysql,data-jpa,cloud-eureka,cloud-config-client -d baseDir=todo-service -d bootVersion=2.6.4.RELEASE -d javaVersion=11 | tar -xzvf –
මීළඟට DemoApplication class එක සඳහා Todo JPA entity එකක් නිර්මාණය කරගනිමු.
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
package com.example.demo; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Todo { public Todo() { } public Todo(String description, boolean done) { this.description = description; this.done = done; } @Id @GeneratedValue private Long id; private String description; private boolean done; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public boolean isDone() { return done; } public void setDone(boolean done) { this.done = done; } } |
Spring Data JPA repository එකක් පහත ආකාරයට නිර්මාණය කරගන්න
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
package com.example.demo; import org.springframework.data.jpa.repository.JpaRepository; public interface TodoRepository extends JpaRepository<Todo, Long> { } Spring MVC controller එකක් add කරගැනීමෙන් code එක complete කරගනිමු. package com.example.demo; import org.springframework.web.bind.annotation.*; import javax.annotation.PostConstruct; import java.util.Arrays; @RestController public class TodoController { private final TodoRepository todoRepository; public TodoController(TodoRepository todoRepository) { this.todoRepository = todoRepository; } @PostConstruct public void init() { todoRepository.saveAll(Arrays.asList( new Todo("First item", true), new Todo("Second item", true), new Todo("Third item", false))); } @GetMapping("/") public Iterable getTodos() { return todoRepository.findAll(); } } |
අපේ application එක deploy කිරීමෙන් අනතුරුව database tables, automatically generate වෙන්න පහත line එක src/main/resources/application.properties කියන configuration file එකට ඇතුලත් කරගන්න.
දැන් අපිට පුළුවන් අපේ “todo-service” project එක build කරන්න සහ Azure Spring Cloud වෙත මුදාහරින්න.
1 2 3 4 5 6 7 8 |
cd todo-service ./mvnw clean package -DskipTests az spring-cloud app deploy --name todo-service --service "$SPRING_CLOUD_NAME" --resource-group "$RESOURCE_GROUP_NAME" --artifact-path target/demo-0.0.1-SNAPSHOT.jar cd .. |
දැන් අපි බලමු කොහොමද Spring Cloud Gateway එකක් build කරගන්නේ කියලා සහ මේ Gateway අපි භාවිතා කරන්නේ කියලා.
මුලිකවම gateways භාවිතා කරන්නේ public HTTP traffic එක microservices වෙතට හැරවීමටයි.
අපේ Spring Cloud Gateway එක නිර්මාණය කිරීම සඳහා පහත command එක සමගින් https://start.spring.io/ භාවිතා කරමු.
1 2 3 4 5 6 |
curl https://start.spring.io/starter.tgz -d dependencies=cloud-gateway,cloud-eureka,cloud-config-client -d baseDir=todo-gateway -d bootVersion=2.6.4.RELEASE -d javaVersion=11 | tar -xzvf - |
src/main/resources/application.properties කියන configuration file එකට පහත property ඇතුලත් කරන්න
පසුගිය ලිපියේදී කල ආකාරයටම todo-gateway application එකක් ඔබේ Azure Spring Cloud instance එක තුල නිර්මාණය කරගන්න. නමුත් මේ application එක gateway එකක් නිසා –assign-endpoint කියන flag එක යෙදීම අවශ්ය වෙනවා.
1 2 3 4 5 |
az spring-cloud app create --name todo-gateway --service "$SPRING_CLOUD_NAME" --resource-group "$RESOURCE_GROUP_NAME" --runtime-version Java_11 --assign-endpoint |
දැන් අපිට පුළුවන් අපේ “todo-gateway” project එක build කරන්න සහ Azure Spring Cloud වෙත මුදාහරින්න.
1 2 3 4 5 6 7 8 |
cd todo-gateway ./mvnw clean package -DskipTests az spring-cloud app deploy --name todo-gateway --service "$SPRING_CLOUD_NAME" --resource-group "$RESOURCE_GROUP_NAME" --artifact-path target/demo-0.0.1-SNAPSHOT.jar cd .. |
ඉතින් මේ විදියට ඔයාටත් පුළුවන් Microservice එකක් පහසුවෙන්ම හදලා ඒ වගේම පහසුවෙන් Azure Cloud එකට deploy කරන්න. මේ ලිපියේ මගහැරුනු තැන් තියෙන්න පුළුවන්. ඒ නිසා වඩාත් වැඩි විස්තර ඔබට මෙතනින් කියවලා දැනගන්න පුළුවන්.