From 32c9af1511879f4c6c76888968763dff234d8e1a Mon Sep 17 00:00:00 2001 From: deepcloudlabs Date: Wed, 24 Jun 2020 10:22:56 +0300 Subject: [PATCH] initial commit --- .../com/example/ConfigServiceApplication.java | 15 ++++ .../src/main/resources/application.properties | 10 +++ lottery-microservice/pom.xml | 74 +++++++++++++++++++ .../controller/LotteryRestController.java | 27 +++++++ .../example/lottery/dto/LotteryResponse.java | 28 +++++++ .../lottery/service/LotteryService.java | 9 +++ .../business/SimpleLotteryService.java | 38 ++++++++++ .../src/main/resources/application.properties | 11 +++ .../src/main/resources/bootstrap.properties | 4 + 9 files changed, 216 insertions(+) create mode 100644 config-service/src/main/java/com/example/ConfigServiceApplication.java create mode 100644 config-service/src/main/resources/application.properties create mode 100644 lottery-microservice/pom.xml create mode 100644 lottery-microservice/src/main/java/com/example/lottery/controller/LotteryRestController.java create mode 100644 lottery-microservice/src/main/java/com/example/lottery/dto/LotteryResponse.java create mode 100644 lottery-microservice/src/main/java/com/example/lottery/service/LotteryService.java create mode 100644 lottery-microservice/src/main/java/com/example/lottery/service/business/SimpleLotteryService.java create mode 100644 lottery-microservice/src/main/resources/application.properties create mode 100644 lottery-microservice/src/main/resources/bootstrap.properties diff --git a/config-service/src/main/java/com/example/ConfigServiceApplication.java b/config-service/src/main/java/com/example/ConfigServiceApplication.java new file mode 100644 index 0000000..f26e07a --- /dev/null +++ b/config-service/src/main/java/com/example/ConfigServiceApplication.java @@ -0,0 +1,15 @@ +package com.example; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.config.server.EnableConfigServer; + +@SpringBootApplication +@EnableConfigServer +public class ConfigServiceApplication { + + public static void main(String[] args) { + SpringApplication.run(ConfigServiceApplication.class, args); + } + +} diff --git a/config-service/src/main/resources/application.properties b/config-service/src/main/resources/application.properties new file mode 100644 index 0000000..512dea4 --- /dev/null +++ b/config-service/src/main/resources/application.properties @@ -0,0 +1,10 @@ +server.address=localhost +server.port=3030 + +#spring cloud config configuration +spring.cloud.config.server.git.uri=c:/DEVEL/stage/tmp/config-repo +spring.cloud.config.server.git.clone-on-start=true + +#authentication +spring.security.user.name=root +spring.security.user.password=secret \ No newline at end of file diff --git a/lottery-microservice/pom.xml b/lottery-microservice/pom.xml new file mode 100644 index 0000000..c11a53c --- /dev/null +++ b/lottery-microservice/pom.xml @@ -0,0 +1,74 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.3.1.RELEASE + + + com.example + lottery-microservice + 0.0.1-SNAPSHOT + lottery-microservice + Spring Boot Project for Lottery MicroService + + + 11 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-devtools + runtime + true + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.cloud + spring-cloud-starter-config + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + + + org.springframework.cloud + spring-cloud-dependencies + Hoxton.SR5 + pom + import + + + + diff --git a/lottery-microservice/src/main/java/com/example/lottery/controller/LotteryRestController.java b/lottery-microservice/src/main/java/com/example/lottery/controller/LotteryRestController.java new file mode 100644 index 0000000..0da832e --- /dev/null +++ b/lottery-microservice/src/main/java/com/example/lottery/controller/LotteryRestController.java @@ -0,0 +1,27 @@ +package com.example.lottery.controller; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.context.annotation.RequestScope; + +import com.example.lottery.dto.LotteryResponse; +import com.example.lottery.service.LotteryService; + +@RestController +@RequestScope +@RequestMapping("numbers") +@CrossOrigin +public class LotteryRestController { + @Autowired + private LotteryService lotteryService; + + // GET http://localhost:8001/lottery/api/v1/numbers?column=10 + @GetMapping(params = {"column"}) + public LotteryResponse getNumbers(@RequestParam int column){ + return new LotteryResponse(lotteryService.draw(column)); + } +} diff --git a/lottery-microservice/src/main/java/com/example/lottery/dto/LotteryResponse.java b/lottery-microservice/src/main/java/com/example/lottery/dto/LotteryResponse.java new file mode 100644 index 0000000..40c85ef --- /dev/null +++ b/lottery-microservice/src/main/java/com/example/lottery/dto/LotteryResponse.java @@ -0,0 +1,28 @@ +package com.example.lottery.dto; + +import java.util.List; + +public class LotteryResponse { + private List> numbers; + + public LotteryResponse() { + } + + public LotteryResponse(List> numbers) { + this.numbers = numbers; + } + + public List> getNumbers() { + return numbers; + } + + public void setNumbers(List> numbers) { + this.numbers = numbers; + } + + @Override + public String toString() { + return "LotteryResponse [numbers=" + numbers + "]"; + } + +} diff --git a/lottery-microservice/src/main/java/com/example/lottery/service/LotteryService.java b/lottery-microservice/src/main/java/com/example/lottery/service/LotteryService.java new file mode 100644 index 0000000..a56f0c2 --- /dev/null +++ b/lottery-microservice/src/main/java/com/example/lottery/service/LotteryService.java @@ -0,0 +1,9 @@ +package com.example.lottery.service; + +import java.util.List; + +public interface LotteryService { + + List> draw(int column); + +} diff --git a/lottery-microservice/src/main/java/com/example/lottery/service/business/SimpleLotteryService.java b/lottery-microservice/src/main/java/com/example/lottery/service/business/SimpleLotteryService.java new file mode 100644 index 0000000..609a09b --- /dev/null +++ b/lottery-microservice/src/main/java/com/example/lottery/service/business/SimpleLotteryService.java @@ -0,0 +1,38 @@ +package com.example.lottery.service.business; + +import java.util.List; +import java.util.concurrent.ThreadLocalRandom; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +import com.example.lottery.service.LotteryService; + +@Service +public class SimpleLotteryService implements LotteryService{ + @Value("${lottery.min}") + private int lotteryMin; + @Value("${lottery.max}") + private int lotteryMax; + @Value("${lottery.size}") + private int lotterySize; + + public List draw() { + return ThreadLocalRandom.current().ints(lotteryMin, lotteryMax) + .distinct() + .limit(lotterySize) + .sorted() + .boxed() + .collect(Collectors.toList()); + } + + @Override + public List> draw(int column) { + return IntStream.range(0, column) + .mapToObj(i -> this.draw()) + .collect(Collectors.toList()); + } + +} diff --git a/lottery-microservice/src/main/resources/application.properties b/lottery-microservice/src/main/resources/application.properties new file mode 100644 index 0000000..7a91bea --- /dev/null +++ b/lottery-microservice/src/main/resources/application.properties @@ -0,0 +1,11 @@ +#Rest API URL Design +server.address=localhost +server.port=8001 +server.servlet.context-path=/lottery +spring.mvc.servlet.path=/api/v1 + +spring.application.name=lottery +spring.profiles.active=development + +# actuator +management.endpoints.web.exposure.include=* \ No newline at end of file diff --git a/lottery-microservice/src/main/resources/bootstrap.properties b/lottery-microservice/src/main/resources/bootstrap.properties new file mode 100644 index 0000000..8e96483 --- /dev/null +++ b/lottery-microservice/src/main/resources/bootstrap.properties @@ -0,0 +1,4 @@ +# config server +spring.cloud.config.uri=http://localhost:3030 +spring.cloud.config.username=root +spring.cloud.config.password=secret \ No newline at end of file