java 在微服务中,使用RestClient如何使用应用程序名称动态调用url?

at0kjp5o  于 2024-01-05  发布在  Java
关注(0)|答案(1)|浏览(175)

我正在开发一个UserService应用程序,它使用Sping Boot 从Rating和Hotel服务中获取数据。我想用应用程序名称替换硬编码的URL以进行动态服务发现。当前方法(使用硬编码的URL):

  1. List<Rating> ratingsOfUser = client.get().uri("http://localhost:8083/ratings/user/" + user.getUserId())
  2. .retrieve()
  3. .body(typeRef);

字符串
所需方法(带应用程序名称):

  1. List<Rating> ratingsOfUser = client.get().uri("http://RATING-SERVICE/ratings/user/" + user.getUserId())
  2. .retrieve()
  3. .body(typeRef);


尝试的解决方案:
1.使用RestClient的@LoadBalanced注解:

  1. @Configuration
  2. public class MyConfig {
  3. @Bean
  4. @LoadBalanced
  5. public RestClient restClient() {
  6. return RestClient.builder()
  7. .build();
  8. }
  9. }


这不管用
1.带有负载平衡的WebClient:

  1. @Bean
  2. public WebClient.Builder loadBalancedWebClientBuilder() {
  3. return WebClient.builder().filter(loadBalancerExchangeFilterFunction());
  4. }


我添加了spring-cloud-starter-loadbalancer依赖项,但我得到了一个“Cannot resolve symbol 'WebClient'”错误。

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-loadbalancer</artifactId>
  4. <version>4.0.4</version>
  5. </dependency>


Sping Boot 版本= 3.2.1
MyConfig.class

  1. @Configuration
  2. public class MyConfig {
  3. @Bean
  4. @LoadBalanced
  5. public RestClient restClient(){
  6. return RestClient.builder()
  7. .build();
  8. }
  9. @Bean
  10. public WebClient.Builder loadBalancedWebClientBuilder() {
  11. return WebClient.builder().filter(loadBalancerExchangeFilterFunction());
  12. }
  13. }


getUser方法

  1. @Override
  2. public User getUser(String userId) {
  3. //get user from the db with the help of user Repo
  4. User user = userRepository.findById(userId)
  5. .orElseThrow(() -> new ResourceNotFoundException("User not found: " + userId));
  6. // fetch Rating of the above user from Rating Service
  7. ParameterizedTypeReference<List<Rating>> typeRef = new ParameterizedTypeReference<List<Rating>>() {};
  8. // List<Rating> ratingsOfUser = client.get().uri("http://RATING-SERVICE/ratings/user/" +user.getUserId())
  9. List<Rating> ratingsOfUser = client.get().uri("http://localhost:8083/ratings/user/" +user.getUserId())
  10. .retrieve()
  11. .body(typeRef);
  12. // for each rating fetch Hotel
  13. List<Rating> ratingList = ratingsOfUser.stream().map(rating -> {
  14. //api call to get hotel service
  15. //Hotel hotels = client.get().uri("http://HOTEL-SERVICE/hotels/"+rating.getHotelId())
  16. Hotel hotels = client.get().uri("http://localhost:8081/hotels/"+rating.getHotelId())
  17. .retrieve()
  18. .body(new ParameterizedTypeReference<Hotel>() {});
  19. //set the hotel to the rating
  20. rating.setHotel(hotels);
  21. // return the rating
  22. return rating;
  23. }).collect(Collectors.toList());
  24. user.setRatings(ratingList);
  25. logger.info("{} ",ratingsOfUser);
  26. return user;
  27. }


如果需要任何额外的信息,请告诉我。谢谢

kadbb459

kadbb4591#

如果你想使用restclient与应用程序名称,你必须使用服务发现(领事,Eureka btwEureka 是不推荐的)和注册所有应用程序调用对方。

相关问题