-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7bad1ed
commit 92f8ec3
Showing
15 changed files
with
414 additions
and
3 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
lottery-client-java-se/src/com/example/lottery/client/MarketApiHttpClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
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; | ||
|
||
public class MarketApiHttpClient { | ||
|
||
private static final String URL = "https://api.binance.com/api/v3/ticker/price?symbol=BTCUSDT"; | ||
|
||
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(); | ||
while(true) { | ||
var response = client.send(request, HttpResponse.BodyHandlers.ofString()).body(); | ||
System.out.println(response); | ||
TimeUnit.SECONDS.sleep(1); | ||
} | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
lottery-client-java-se/src/com/example/lottery/client/MarketApiWebsocketClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.example.lottery.client; | ||
|
||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.WebSocket; | ||
import java.net.http.WebSocket.Listener; | ||
import java.util.concurrent.CompletionStage; | ||
|
||
public class MarketApiWebsocketClient { | ||
private static final String URL = "wss://stream.binance.com:9443/ws/btcusdt@trade"; | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
Listener listener = new MarketWebsocketListener(); | ||
HttpClient.newHttpClient().newWebSocketBuilder().buildAsync(URI.create(URL),listener); | ||
Thread.sleep(60_000); | ||
} | ||
} | ||
|
||
class MarketWebsocketListener implements Listener { | ||
|
||
@Override | ||
public void onOpen(WebSocket webSocket) { | ||
System.err.println("Connected to the market."); | ||
webSocket.request(1); | ||
} | ||
|
||
@Override | ||
public CompletionStage<?> onText(WebSocket webSocket, CharSequence data, boolean last) { | ||
System.err.println(data); | ||
webSocket.request(1); | ||
return null; | ||
} | ||
|
||
@Override | ||
public CompletionStage<?> onClose(WebSocket webSocket, int statusCode, String reason) { | ||
System.err.println("Connection is closed!"); | ||
return null; | ||
} | ||
|
||
@Override | ||
public void onError(WebSocket webSocket, Throwable error) { | ||
System.err.println("An error has occured: "+error.getMessage()+" at session "+webSocket); | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
...vice/src/main/java/com/example/lottery/service/LotteryConsumerServiceWithRateLimiter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.example.lottery.service; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import com.example.lottery.dto.LotteryResponse; | ||
|
||
import io.github.resilience4j.ratelimiter.annotation.RateLimiter; | ||
|
||
/** | ||
* | ||
* @author Binnur Kurt <binnur.kurt@gmail.com> | ||
* | ||
*/ | ||
@Service | ||
public class LotteryConsumerServiceWithRateLimiter { | ||
|
||
@RateLimiter(name = "lotterySrvLimiter") | ||
public LotteryResponse getLotteryNumbers() { | ||
System.err.println("Calling lottery service from LotteryConsumerServiceWithRateLimiter..."); | ||
var rt = new RestTemplate(); | ||
var response = rt.getForObject("http://localhost:8001/lottery/api/v1/numbers?column=10", LotteryResponse.class); | ||
System.err.println(response); | ||
return response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
lottery-consumer-service/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
spring.main.banner-mode=off | ||
|
||
server.address=localhost | ||
server.port=5001 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?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>market-websocket-client-spring-boot</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>market-websocket-client-spring-boot</name> | ||
<description>Spring Boot Project for Market Client</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.boot</groupId> | ||
<artifactId>spring-boot-starter-websocket</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.boot</groupId> | ||
<artifactId>spring-boot-starter-data-mongodb</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
14 changes: 14 additions & 0 deletions
14
...-websocket-client-spring-boot/src/main/java/com/example/config/WebsocketClientConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.example.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.socket.client.WebSocketClient; | ||
import org.springframework.web.socket.client.standard.StandardWebSocketClient; | ||
|
||
@Configuration | ||
public class WebsocketClientConfig { | ||
@Bean | ||
public WebSocketClient webSocketClient() { | ||
return new StandardWebSocketClient(); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
market-websocket-client-spring-boot/src/main/java/com/example/config/WebsocketConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.example.config; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.messaging.simp.config.MessageBrokerRegistry; | ||
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; | ||
import org.springframework.web.socket.config.annotation.StompEndpointRegistry; | ||
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; | ||
|
||
@Configuration | ||
@EnableWebSocketMessageBroker | ||
public class WebsocketConfig implements WebSocketMessageBrokerConfigurer { | ||
|
||
@Override | ||
public void configureMessageBroker(MessageBrokerRegistry registry) { | ||
registry.enableSimpleBroker("/topic"); | ||
registry.setApplicationDestinationPrefixes("/ws"); | ||
} | ||
|
||
@Override | ||
public void registerStompEndpoints(StompEndpointRegistry registry) { | ||
registry.addEndpoint("/changes").setAllowedOrigins("*").withSockJS(); | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
market-websocket-client-spring-boot/src/main/java/com/example/event/TradeEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package com.example.event; | ||
|
||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.mongodb.core.mapping.Document; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
@Document(collection = "trades") | ||
public class TradeEvent { | ||
@Id | ||
private String id; | ||
@JsonProperty("s") | ||
private String symbol; | ||
@JsonProperty("p") | ||
private String price; | ||
@JsonProperty("q") | ||
private String quantity; | ||
@JsonProperty("T") | ||
private long timestamp; | ||
@JsonProperty("t") | ||
private long eventId; | ||
@JsonProperty("a") | ||
private long askId; | ||
@JsonProperty("b") | ||
private long bidId; | ||
|
||
public TradeEvent() { | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
|
||
public String getSymbol() { | ||
return symbol; | ||
} | ||
|
||
public void setSymbol(String symbol) { | ||
this.symbol = symbol; | ||
} | ||
|
||
public String getPrice() { | ||
return price; | ||
} | ||
|
||
public void setPrice(String price) { | ||
this.price = price; | ||
} | ||
|
||
public String getQuantity() { | ||
return quantity; | ||
} | ||
|
||
public void setQuantity(String quantity) { | ||
this.quantity = quantity; | ||
} | ||
|
||
public long getTimestamp() { | ||
return timestamp; | ||
} | ||
|
||
public void setTimestamp(long timestamp) { | ||
this.timestamp = timestamp; | ||
} | ||
|
||
public long getEventId() { | ||
return eventId; | ||
} | ||
|
||
public void setEventId(long eventId) { | ||
this.eventId = eventId; | ||
} | ||
|
||
public long getAskId() { | ||
return askId; | ||
} | ||
|
||
public void setAskId(long askId) { | ||
this.askId = askId; | ||
} | ||
|
||
public long getBidId() { | ||
return bidId; | ||
} | ||
|
||
public void setBidId(long bidId) { | ||
this.bidId = bidId; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "TradeEvent [id=" + id + ", symbol=" + symbol + ", price=" + price + ", quantity=" + quantity | ||
+ ", timestamp=" + timestamp + ", eventId=" + eventId + ", askId=" + askId + ", bidId=" + bidId + "]"; | ||
} | ||
|
||
|
||
|
||
} |
9 changes: 9 additions & 0 deletions
9
...bsocket-client-spring-boot/src/main/java/com/example/repository/TradeEventRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.example.repository; | ||
|
||
import org.springframework.data.mongodb.repository.MongoRepository; | ||
|
||
import com.example.event.TradeEvent; | ||
|
||
public interface TradeEventRepository extends MongoRepository<TradeEvent, String> { | ||
|
||
} |
Oops, something went wrong.