-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⚡ (interservice communication) switch to amqp rpc
- Loading branch information
Showing
23 changed files
with
399 additions
and
63 deletions.
There are no files selected for viewing
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
13 changes: 0 additions & 13 deletions
13
feedservice/src/main/java/edu/hm/peslalz/thesis/feedservice/client/FeignClientConfig.java
This file was deleted.
Oops, something went wrong.
58 changes: 47 additions & 11 deletions
58
feedservice/src/main/java/edu/hm/peslalz/thesis/feedservice/client/PostClient.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 |
---|---|---|
@@ -1,21 +1,57 @@ | ||
package edu.hm.peslalz.thesis.feedservice.client; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import edu.hm.peslalz.thesis.feedservice.entity.PagedPostResponse; | ||
import edu.hm.peslalz.thesis.feedservice.entity.PostDTO; | ||
import edu.hm.peslalz.thesis.feedservice.entity.PostsRequestDTO; | ||
import org.springframework.amqp.core.DirectExchange; | ||
import org.springframework.amqp.rabbit.core.RabbitTemplate; | ||
import org.springframework.cache.annotation.Cacheable; | ||
import org.springframework.cloud.openfeign.FeignClient; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.server.ResponseStatusException; | ||
|
||
@Component | ||
public class PostClient { | ||
private final DirectExchange postExchange; | ||
private final RabbitTemplate template; | ||
|
||
public PostClient(DirectExchange postExchange, RabbitTemplate template) { | ||
this.postExchange = postExchange; | ||
this.template = template; | ||
} | ||
|
||
@FeignClient("postservice") | ||
public interface PostClient { | ||
@Cacheable("posts") | ||
@GetMapping("posts") | ||
PagedPostResponse getPosts(@RequestParam(required = false) String category, @RequestParam(required = false) Integer userId, @RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "50") int size); | ||
public PagedPostResponse getPosts(String category, Integer userId, int page, int size) { | ||
PostsRequestDTO postsRequestDTO = new PostsRequestDTO(category, userId, page, size); | ||
ObjectMapper mapper = new ObjectMapper(); | ||
String message; | ||
try { | ||
message = mapper.writeValueAsString(postsRequestDTO); | ||
} catch (JsonProcessingException e) { | ||
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Could not process request to json", e); | ||
} | ||
String response = (String) this.template.convertSendAndReceive(postExchange.getName(), "rpc-posts", message); | ||
PagedPostResponse pagedPostResponse; | ||
try { | ||
pagedPostResponse = mapper.readValue(response, PagedPostResponse.class); | ||
} catch (JsonProcessingException e) { | ||
throw new RuntimeException(e); | ||
} | ||
return pagedPostResponse; | ||
} | ||
|
||
@Cacheable("post") | ||
@GetMapping(value = "posts/{id}") | ||
PostDTO getPost(@PathVariable int id); | ||
public PostDTO getPost(int id) { | ||
String response = (String) this.template.convertSendAndReceive(postExchange.getName(), "rpc-post", id); | ||
ObjectMapper mapper = new ObjectMapper(); | ||
PostDTO postDTO; | ||
try { | ||
postDTO = mapper.readValue(response, PostDTO.class); | ||
} catch (JsonProcessingException e) { | ||
throw new RuntimeException(e); | ||
} | ||
return postDTO; | ||
} | ||
} |
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
47 changes: 36 additions & 11 deletions
47
feedservice/src/main/java/edu/hm/peslalz/thesis/feedservice/client/TrendClient.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 |
---|---|---|
@@ -1,22 +1,47 @@ | ||
package edu.hm.peslalz.thesis.feedservice.client; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import edu.hm.peslalz.thesis.feedservice.entity.PagedTrendResponse; | ||
import org.springframework.amqp.core.DirectExchange; | ||
import org.springframework.amqp.rabbit.core.RabbitTemplate; | ||
import org.springframework.cache.annotation.Cacheable; | ||
import org.springframework.cloud.openfeign.FeignClient; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class TrendClient { | ||
private final DirectExchange trendExchange; | ||
private final RabbitTemplate template; | ||
|
||
public TrendClient(DirectExchange trendExchange, RabbitTemplate template) { | ||
this.trendExchange = trendExchange; | ||
this.template = template; | ||
} | ||
|
||
@FeignClient("statisticservice") | ||
public interface TrendClient { | ||
@Cacheable("trends/categories") | ||
@GetMapping("trends/categories") | ||
PagedTrendResponse getTrendingCategories(@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "50") int size); | ||
public PagedTrendResponse getTrendingCategories(int page) { | ||
return getPagedTrendResponse("rpc-categories", page); | ||
} | ||
|
||
@Cacheable("trends/posts") | ||
@GetMapping("trends/posts") | ||
PagedTrendResponse getTrendingPosts(@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "50") int size); | ||
public PagedTrendResponse getTrendingPosts(int page){ | ||
return getPagedTrendResponse("rpc-posts", page); | ||
} | ||
|
||
@Cacheable("trends/users") | ||
@GetMapping("trends/users") | ||
PagedTrendResponse getTrendingUsers(@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "50") int size); | ||
public PagedTrendResponse getTrendingUsers(int page){ | ||
return getPagedTrendResponse("rpc-users", page); | ||
} | ||
|
||
private PagedTrendResponse getPagedTrendResponse(String routingKey, int page) { | ||
String response = (String) this.template.convertSendAndReceive(trendExchange.getName(), routingKey, page); | ||
ObjectMapper mapper = new ObjectMapper(); | ||
PagedTrendResponse pagedTrendResponse; | ||
try { | ||
pagedTrendResponse = mapper.readValue(response, PagedTrendResponse.class); | ||
} catch (JsonProcessingException e) { | ||
throw new RuntimeException(e); | ||
} | ||
return pagedTrendResponse; | ||
} | ||
} |
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
feedservice/src/main/java/edu/hm/peslalz/thesis/feedservice/entity/PostDTO.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
9 changes: 9 additions & 0 deletions
9
feedservice/src/main/java/edu/hm/peslalz/thesis/feedservice/entity/PostsRequestDTO.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 edu.hm.peslalz.thesis.feedservice.entity; | ||
|
||
import java.io.Serial; | ||
import java.io.Serializable; | ||
|
||
public record PostsRequestDTO(String category, Integer userId, int page, int size) implements Serializable { | ||
@Serial | ||
private static final long serialVersionUID = 1L; | ||
} |
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
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
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
9 changes: 9 additions & 0 deletions
9
postservice/src/main/java/edu/hm/peslalz/thesis/postservice/entity/PostsRequestDTO.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 edu.hm.peslalz.thesis.postservice.entity; | ||
|
||
import java.io.Serial; | ||
import java.io.Serializable; | ||
|
||
public record PostsRequestDTO(String category, Integer userId, int page, int size) implements Serializable { | ||
@Serial | ||
private static final long serialVersionUID = 1L; | ||
} |
Oops, something went wrong.