Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
deepcloudlabs committed Jun 24, 2020
1 parent f8fa6ec commit 05455a5
Show file tree
Hide file tree
Showing 24 changed files with 648 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.example.lottery.client;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
*
* @author Binnur Kurt <binnur.kurt@gmail.com>
*
*/
public class LotteryAsyncClient {

private static final String URL = "http://localhost:8001/lottery/api/v1/numbers?column=10";

public static void main(String[] args) throws IOException, InterruptedException {
var counter = new AtomicInteger(0);
var client = HttpClient.newHttpClient();
var request = HttpRequest.newBuilder().uri(URI.create(URL)).header("Accept", "application/json").build();

var start = System.currentTimeMillis();
for (var i = 0; i < 1_000; ++i) {
// asynchronous call
client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenApply(HttpResponse::body)
.thenAccept(numbers -> {
// System.out.println(Thread.currentThread().getName());
var value = counter.incrementAndGet();
if (value == 1_000) {
var stop = System.currentTimeMillis();
System.err.println("Duration: " + (stop - start) + " ms.");
}
}); // functional programming
// System.out.println(numbers);
}
TimeUnit.SECONDS.sleep(10);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.example.lottery.client;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.stream.IntStream;

/**
*
* @author Binnur Kurt <binnur.kurt@gmail.com>
*
*/
public class LotterySyncClient {

private static final String URL = "http://localhost:8001/lottery/api/v1/numbers?column=10";

public static void main(String[] args) throws IOException, InterruptedException {
var client = HttpClient.newHttpClient();
var request = HttpRequest.newBuilder().uri(URI.create(URL)).header("Accept", "application/json").build();
var start = System.currentTimeMillis();
IntStream.range(0, 1_000).parallel().forEach(i -> {
// synchronous call
try {
// System.out.println(Thread.currentThread().getName());
client.send(request, HttpResponse.BodyHandlers.ofString()).body();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
// System.out.println(numbers);
});
var stop = System.currentTimeMillis();
System.err.println("Duration: " + (stop - start) + " ms.");
}

}
99 changes: 99 additions & 0 deletions lottery-consumer-service/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>lottery-consumer-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>lottery-consumer-service</name>
<description>Spring Boot Project for Lottery Consumer MicroService </description>

<properties>
<java.version>11</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-spring-boot2</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>
spring-cloud-starter-netflix-eureka-client
</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>
spring-cloud-starter-netflix-hystrix
</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.example.lottery;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
*
* @author Binnur Kurt <binnur.kurt@gmail.com>
*
*/
@SpringBootApplication
@EnableScheduling
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrix
@EnableRetry
public class LotteryConsumerServiceApplication {

public static void main(String[] args) {
SpringApplication.run(LotteryConsumerServiceApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.lottery.client;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import org.springframework.stereotype.Service;

import com.example.lottery.dto.LotteryResponse;

@Service
public class FallbackLotteryService implements LotteryService {

@Override
public LotteryResponse getLotteryNumbers(int column) {
return new LotteryResponse(IntStream.range(0, column).mapToObj(i -> List.of(1,2,3,4,5,6)).collect(Collectors.toList()));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.lottery.client;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.example.lottery.dto.LotteryResponse;

@FeignClient(name="lottery",fallback = FallbackLotteryService.class)
public interface LotteryService {
@GetMapping("/lottery/api/v1/numbers")
LotteryResponse getLotteryNumbers(@RequestParam int column);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example.lottery.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
*
* @author Binnur Kurt <binnur.kurt@gmail.com>
*
*/
@Configuration
public class RestTemplateConfig {

@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.example.lottery.dto;

import java.util.List;

/**
*
* @author Binnur Kurt <binnur.kurt@gmail.com>
*
*/
public class LotteryResponse {
private List<List<Integer>> numbers;

public LotteryResponse() {
}

public LotteryResponse(List<List<Integer>> numbers) {
this.numbers = numbers;
}

public List<List<Integer>> getNumbers() {
return numbers;
}

public void setNumbers(List<List<Integer>> numbers) {
this.numbers = numbers;
}

@Override
public String toString() {
return "LotteryResponse [numbers=" + numbers + "]";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.example.lottery.service;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.example.lottery.dto.LotteryResponse;

/**
*
* @author Binnur Kurt <binnur.kurt@gmail.com>
*
*/
@Service
public class LotteryConsumerServiceLoadBalancing1 {
private static final String LOTTERY_SERVICE_URL = "http://%s:%d/lottery/api/v1/numbers?column=10";

@Autowired
private DiscoveryClient discoveryClient;
private List<ServiceInstance> servers;
private AtomicInteger counter = new AtomicInteger(0);
private RestTemplate restTemplate;

@PostConstruct
public void loadInstanceList() {
servers = discoveryClient.getInstances("lottery");
servers.forEach(System.out::println);
this.restTemplate = new RestTemplate();
}

@Scheduled(fixedRate = 3_000)
public void callLotteryService() {
var index = counter.getAndIncrement() % servers.size();
var server = servers.get(index);
var url = String.format(LOTTERY_SERVICE_URL, server.getHost(), server.getPort());
var response = restTemplate.getForObject(url, LotteryResponse.class);
System.out.println(response);
}
}
Loading

0 comments on commit 05455a5

Please sign in to comment.