Spring Cloud - 27. Micro Service간 통신

목차

User Service

@SpringBootApplication
@EnableDiscoveryClient
@Slf4j
public class UserServiceApplication {

public static void main(String[] args){
SpringApplication.run(UserServiceApplication.class, args);
}

@Bean
public BCryptPasswordEncoder passwordEncoder() throws UnknownHostException {
return new BCryptPasswordEncoder();
}

@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
@Override
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<>();
// userDto.setOrders(orders);

String orderUrl = String.format(env.getProperty("order_service.url"), userId);
ResponseEntity<List<ResponseOrder>> orderListResponse =
restTemplate.exchange(orderUrl
, HttpMethod.GET
, null
, new ParameterizedTypeReference<List<ResponseOrder>>() {});
List<ResponseOrder> orderList = orderListResponse.getBody();
userDto.setOrders(orderList);

return userDto;
}

application.yml

token:
expiration_time: 84600000
secret: user_token_native_application_changed

gateway:
ip: 192.168.123.106

order_service:
url: http://127.0.0.1:8080/order-service/%s/orders
token:
expiration_time: 84600000
secret: user_token_native_application_changed

gateway:
ip: 192.168.123.106

order_service:
url: http://ORDER-SERVICE/order-service/%s/orders
@SpringBootApplication
@EnableDiscoveryClient
@Slf4j
public class UserServiceApplication {

public static void main(String[] args){
SpringApplication.run(UserServiceApplication.class, args);
}

@Bean
public BCryptPasswordEncoder passwordEncoder() throws UnknownHostException {
return new BCryptPasswordEncoder();
}

@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
Share