Spring Cloud - 29. Micro Service간 통신 Feign client 예외 처리

목차

User service

server:
port: 0

spring:
application:
name: user-service

h2:
console:
enabled: true
settings:
web-allow-others: true

datasource:
username: sa
password:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:testdb

rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest

eureka:
instance:
instanceId: ${spring.application.name}:${spring.application.instance_id:${random.value}}
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8761/eureka

greeting:
message: Welcome to the Simple E-commerce.

management:
endpoints:
web:
exposure:
include: refresh, health, beans, busrefresh

logging:
level:
com.example.userservice.client: DEBUG

#token:
# expiration_time: 84600000
# secret: user_token
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@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();
}

@Bean
public Logger.Level feignLoggerLevel(){
return Logger.Level.FULL;
}
}
@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> orderList = null;
try {
orderList = orderServiceClient.getOrders(userId);
} catch (FeignException ex) {
log.error(ex.getMessage());
}
userDto.setOrders(orderList);

return userDto;
}
Share