21. RestTemplate
- 📚 Spring/Spring Cloud
- 2023. 5. 27. 23:59
User-Service > UserServiceApplication.java
@LoadBalaned를 붙이면 Microservice의 name으로 사용할 수 있다. 그러면 url이 바꿔도 따로 바꿔줄 필요가 없다
@SpringBootApplication
@EnableDiscoveryClient
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class, args);
}
@Bean
public BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
user-service > UserServiceImpl.java
public UserDto getUserByUserId(String userId) {
UserEntity userEntity = userRepository.findByUserId(userId);
if (userEntity == null) {
throw new UsernameNotFoundException("User not found");
}
UserDto userDto = new ModelMapper().map(userEntity, UserDto.class);
//List<ResponseOrder> orders = new ArrayList<>();
String orderUrl = String.format(env.getProperty("order_service.url"), userId); //"http://localhost:8000/order-service/%s/orders";
ResponseEntity<List<ResponseOrder>> orderListResponse =
restTemplate.exchange(orderUrl, HttpMethod.GET, null,
new ParameterizedTypeReference<List<ResponseOrder>>() {
});
List<ResponseOrder> orderList = orderListResponse.getBody();
userDto.setOrders(orderList);
return userDto;
}
native-file-repo > user-service.yml에 url정보 추가
order_service:
url: http://ORDER-SERVICE/order-service/%s/orders
'📚 Spring > Spring Cloud' 카테고리의 다른 글
22. Feign Client (0) | 2023.05.28 |
---|---|
19. 대칭키를 활용한 DB정보 암호화 (0) | 2023.05.27 |
18.대칭키와 비대칭키 (0) | 2023.05.15 |
16. Spring Cloud Bus (0) | 2023.05.10 |
[Error] JWT 구현 시 javax/xml/bind/DatatypeConverter 에러 대처법 (0) | 2023.05.04 |