Skip to content

Commit

Permalink
Merge pull request #276 from sixwaaaay/client-v
Browse files Browse the repository at this point in the history
feat: update client, tests
  • Loading branch information
sixwaaaay authored Oct 4, 2024
2 parents 13b2cca + d542c7f commit 5abdc9a
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 219 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/comment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ jobs:
DB_OPTIONS: serverTimezone=Asia/Shanghai
TRACING_PROBABILITY: 0.1
OTLP_ENDPOINT: http://localhost:4318/v1/traces
VOTE_SERVICE_BASE_URL: http://localhost:8081
VOTE_SERVICE_BASE_URL: http://localhost:5000
USER_SERVICE_BASE_URL: http://localhost:5000
REDIS_HOST: localhost

Expand Down
8 changes: 1 addition & 7 deletions graal/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,4 @@ services:
shauser:
image: 'sixwaaaay/shauser:latest-test'
ports:
- '5000:5000'
graph:
image: 'sixwaaaay/graph:0.6.0'
ports:
- '8081:8081'
environment:
- 'APP_PORT=8081'
- '5000:5000'
14 changes: 13 additions & 1 deletion graal/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.3</version>
<version>3.3.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>io.sixwaaaay</groupId>
Expand Down Expand Up @@ -55,6 +55,17 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!-- Exclude the Tomcat dependency -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
Expand Down Expand Up @@ -86,6 +97,7 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.34</version>
<optional>true</optional>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@
package io.sixwaaaay.sharingcomment.client;


import io.sixwaaaay.sharingcomment.transmission.GetMultipleUserReply;
import io.sixwaaaay.sharingcomment.transmission.GetUserReply;
import io.sixwaaaay.sharingcomment.domain.User;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.service.annotation.GetExchange;

import java.util.Collection;
import java.util.List;


public interface UserClient {
@GetExchange("/users/{user_id}")
GetUserReply getUser(@PathVariable("user_id") long id, @RequestHeader(value = "Authorization", required = false) String token);
User getUser(@PathVariable("user_id") long id, @RequestHeader(value = "Authorization", required = false) String token);

@GetExchange("/users")
GetMultipleUserReply getManyUser(@RequestParam("ids") Collection<Long> ids, @RequestHeader(value = "Authorization", required = false) String token);
List<User> getManyUser(@RequestParam("ids") Collection<Long> ids, @RequestHeader(value = "Authorization", required = false) String token);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
import io.github.resilience4j.decorators.Decorators;
import io.github.resilience4j.retry.Retry;
import io.github.resilience4j.retry.RetryConfig;
import io.sixwaaaay.sharingcomment.transmission.GetMultipleUserReply;
import io.sixwaaaay.sharingcomment.transmission.GetUserReply;
import io.sixwaaaay.sharingcomment.domain.User;
import org.springframework.web.client.HttpClientErrorException;

import java.time.Duration;
import java.util.Collection;
import java.util.List;

public class UserClientWrapper implements UserClient {
private final UserClient userClient;
Expand All @@ -36,7 +36,8 @@ public UserClientWrapper(UserClient userClient) {

this.userClient = userClient;
var name = "userClient";
var retryConfig = RetryConfig.custom()
var retryConfig = RetryConfig
.custom()
.maxAttempts(3)
.waitDuration(Duration.ofMillis(1000))
.ignoreExceptions(
Expand All @@ -46,7 +47,8 @@ public UserClientWrapper(UserClient userClient) {
HttpClientErrorException.Unauthorized.class
).build();
retry = Retry.of(name, retryConfig);
var circuitBreakerConfig = CircuitBreakerConfig.custom()
var circuitBreakerConfig = CircuitBreakerConfig
.custom()
.failureRateThreshold(50)
.ignoreExceptions(
HttpClientErrorException.NotFound.class,
Expand All @@ -62,8 +64,9 @@ public UserClientWrapper(UserClient userClient) {
}

@Override
public GetUserReply getUser(long id, String token) {
var getUserReplySupplier = Decorators.ofSupplier(() -> userClient.getUser(id, token))
public User getUser(long id, String token) {
var getUserReplySupplier = Decorators
.ofSupplier(() -> userClient.getUser(id, token))
.withCircuitBreaker(circuitBreaker)
.withRetry(retry)
.decorate();
Expand All @@ -72,8 +75,9 @@ public GetUserReply getUser(long id, String token) {


@Override
public GetMultipleUserReply getManyUser(Collection<Long> ids, String token) {
var getManyUserReplySupplier = Decorators.ofSupplier(() -> userClient.getManyUser(ids, token))
public List<User> getManyUser(Collection<Long> ids, String token) {
var getManyUserReplySupplier = Decorators
.ofSupplier(() -> userClient.getManyUser(ids, token))
.withCircuitBreaker(circuitBreaker)
.withRetry(retry)
.decorate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,21 @@
package io.sixwaaaay.sharingcomment.client;


import io.sixwaaaay.sharingcomment.transmission.*;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.service.annotation.PostExchange;

import java.util.List;

public interface VoteClient {
@PostExchange("/item/add")
VoteReply itemAdd(@Valid @RequestBody VoteReq req);

@PostExchange("/item/delete")
VoteReply itemDelete(@Valid @RequestBody VoteReq req);

@PostExchange("/item/exists")
VoteExistsReply exists(@Valid @RequestBody VoteExistsReq req);
public interface VoteClient {

@PostExchange("/item/scan")
ScanVotedReply scan(@RequestBody ScanVotedReq req);
/**
* query whether the objectIds(aka commentIds) is liked.
* @param objectIds the id list of the object(aka comment)
* @param token the token of the user
* @return the list of the objectIds which is liked
*/
@PostExchange("/graph/comments/likes")
List<Long> queryInLikes(@RequestBody List<Long> objectIds, @RequestHeader(value = "Authorization", required = false) String token);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@
import io.github.resilience4j.decorators.Decorators;
import io.github.resilience4j.retry.Retry;
import io.github.resilience4j.retry.RetryConfig;
import io.sixwaaaay.sharingcomment.transmission.*;

import java.time.Duration;
import java.util.Set;
import java.util.List;

public class VoteClientWrapper implements VoteClient {

Expand All @@ -47,39 +46,19 @@ public VoteClientWrapper(VoteClient voteClient) {
.waitDuration(Duration.ofMillis(1000))
.build()
);

@Override
public VoteReply itemAdd(VoteReq req) {
return voteClient.itemAdd(req);
}

@Override
public VoteReply itemDelete(VoteReq req) {
return voteClient.itemDelete(req);
}

/**
* wrap the exists method with resilience4j circuit breaker and retry
* fallback with an empty set if the circuit breaker is open
* wrap the queryInLikes method with resilience4j circuit breaker and retry
* @param commentIds list of object ids
* @param token user token
* @return list of object ids that user liked
*/
@Override
public VoteExistsReply exists(VoteExistsReq req) {
var existsReplySupplier = Decorators.ofSupplier(() -> voteClient.exists(req))
public List<Long> queryInLikes(List<Long> commentIds, String token) {
var queryInLikesSupplier = Decorators.ofSupplier(() -> voteClient.queryInLikes(commentIds, token))
.withCircuitBreaker(circuitBreaker)
.withRetry(retry)
.withFallback(this::fallback)
.withFallback((e) -> List.of())
.decorate();
return existsReplySupplier.get();
}

@Override
public ScanVotedReply scan(ScanVotedReq req) {
return voteClient.scan(req);
}

private VoteExistsReply fallback(Throwable e) {
var reply = new VoteExistsReply();
reply.setExists(Set.of());
return reply;
return queryInLikesSupplier.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ public Comment createComment(@Valid @RequestBody CommentRequest request) {
return comment;
}

/**
* delete specified comment
*
* @param id the id of the comment to be deleted
* @param request the request body
*/
@DeleteMapping("/{id}")
public void deleteComment(
@PathVariable("id") Long id,
Expand All @@ -111,27 +117,4 @@ public void deleteComment(
commentService.deleteComment(comment);
}

/**
* vote a comment
*
* @param id the id of comment
*/
@PostMapping("/action/like/{id}")
public void voteComment(
@PathVariable long id) {
var userId = Principal.currentUserId();
commentService.voteComment(userId, id);
}

/**
* cancel vote a comment
*
* @param id the id of comment
*/
@DeleteMapping("/action/like/{id}")
public void cancelVoteComment(
@PathVariable long id) {
var userId = Principal.currentUserId();
commentService.cancelVoteComment(userId, id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import io.sixwaaaay.sharingcomment.domain.*;
import io.sixwaaaay.sharingcomment.repository.CommentRepository;
import io.sixwaaaay.sharingcomment.request.Principal;
import io.sixwaaaay.sharingcomment.transmission.VoteExistsReq;
import io.sixwaaaay.sharingcomment.transmission.VoteReq;
import io.sixwaaaay.sharingcomment.util.DbContext;
import io.sixwaaaay.sharingcomment.util.DbContextEnum;
import org.springframework.beans.factory.annotation.Value;
Expand Down Expand Up @@ -148,26 +146,6 @@ public void deleteComment(Comment comment) {
commentRepo.deleteByIdAndUserId(comment.getId(), comment.getUserId());
}

/**
* vote a comment
*
* @param userId the id of the user who is requesting
* @param commentId the id of the comment to be voted
*/
public void voteComment(long userId, long commentId) {
voteClient.itemAdd(new VoteReq(userId, commentId));
}

/**
* cancel vote a comment
*
* @param userId the id of the user who is requesting
* @param commentId the id of the comment to be unvoted
*/
public void cancelVoteComment(Long userId, Long commentId) {
voteClient.itemDelete(new VoteReq(userId, commentId));
}


/**
* compose the comment, fill the user info and vote status
Expand All @@ -178,7 +156,7 @@ private void composeSingleComment(Comment comment) {
if (enableUser) {
var token = Principal.currentToken();
var user = userClient.getUser(comment.getUserId(), token);
comment.setUser(user.getUser());
comment.setUser(user);
}
}

Expand Down Expand Up @@ -219,7 +197,7 @@ private Map<Long, User> composeCommentAuthor(List<Comment> comments) {
var token = Principal.currentToken();
var users = userClient.getManyUser(userList, token);
// covert to map
return users.getUsers().stream().collect(Collectors.toMap(User::getId, identity()));
return users.stream().collect(Collectors.toMap(User::getId, identity()));
}

/**
Expand All @@ -235,9 +213,9 @@ private Set<Long> composeCommentVoteStatus(List<Comment> comments, Long userId)
}
var commentIdList = flatComments(comments).map(Comment::getId).toList();
// check if voted
var voteExistsReply = voteClient.exists(new VoteExistsReq(userId, commentIdList));
var votedIds = voteClient.queryInLikes(commentIdList, Principal.currentToken());
// convert to set
return voteExistsReply.getExists();
return Set.copyOf(votedIds);
}

/**
Expand Down

This file was deleted.

Loading

0 comments on commit 5abdc9a

Please sign in to comment.