728x90
Feign
다른 서버에서 실행된 결과를 가져옴
Currency Exchange Microservice
CurrencyExchange
@NoArgsConstructor
@Getter
@Setter
@Entity
public class CurrencyExchange
{
@Id
private Long id;
@Column(name = "currency_from")
private String from;
@Column(name = "currency_to")
private String to;
private BigDecimal conversionMultiple;
private String environment;
}
CurrencyExchangeController
@RestController
public class CurrencyExchangeController
{
@Autowired
private CurrencyExchangeRepository currencyExchangeRepository;
@Autowired
private Environment environment;
@GetMapping("/currency-exchange/from/{from}/to/{to}")
public CurrencyExchange retrieveExchangeValue(
@PathVariable String from,
@PathVariable String to
)
{
// CurrencyExchange currencyExchange = new CurrencyExchange();
// currencyExchange.setId(1000L);
// currencyExchange.setFrom(from);
// currencyExchange.setTo(to);
// currencyExchange.setConversionMultiple(BigDecimal.valueOf(50));
CurrencyExchange currencyExchange = currencyExchangeRepository.findByFromAndTo(from, to);
if(currencyExchange == null){
throw new RuntimeException("");
}
currencyExchange.setEnvironment(environment.getProperty("local.server.port"));
return currencyExchange;
}
}
CurrencyExchangeRepository
public interface CurrencyExchangeRepository extends JpaRepository<CurrencyExchange,Long> {
CurrencyExchange findByFromAndTo(String from, String to);
}
application.properties
spring.config.import=optional:configserver:http://localhost:8888
spring.application.name=currency-exchange
server.port=8000
spring.jpa.show-sql=true
spring.datasource.url=jdbc:h2:mem:testdb
spring.h2.console.enabled=true
# data.sql 스크립트가 실행
spring.jpa.defer-datasource-initialization=true
의존성
더보기
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-spring-boot2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
port 2개 사용
IntelliJ VM options 추가 참고
실행 결과
Currency Conversion Microservice
의존성
더보기
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
*중요 : openfeign 추가
CurrencyConversionServiceApplication
@EnableFeignClients
@SpringBootApplication
public class CurrencyConversionServiceApplication {
public static void main(String[] args) {
SpringApplication.run(CurrencyConversionServiceApplication.class, args);
}
}
- @EnableFeignClients : @FeignClient 를 찾기 위해 메인에서 애노테이션 사용
CurrencyConversionController
@RestController
public class CurrencyConversionController
{
private CurrencyExchangeProxy currencyExchangeProxy;
public CurrencyConversionController(CurrencyExchangeProxy currencyExchangeProxy) {
this.currencyExchangeProxy = currencyExchangeProxy;
}
@GetMapping("/currency-converter/from/{from}/to/{to}/quantity/{quantity}")
public CurrencyConversion calculateCurrencyConversion(
@PathVariable String from, @PathVariable String to, @PathVariable BigDecimal quantity
){
HashMap<String, String> uriVariables= new HashMap<>();
uriVariables.put("from",from);
uriVariables.put("to", to);
// 해당 주소에서 데이터값 가져옴
ResponseEntity<CurrencyConversion> responseEntity = new RestTemplate().getForEntity(
"http://localhost:8000/currency-exchange/from/{from}/to/{to}", CurrencyConversion.class, uriVariables
);
CurrencyConversion currencyConversion = responseEntity.getBody();
return new CurrencyConversion(currencyConversion.getId(),from,to,quantity,
currencyConversion.getConversionMultiple(), quantity.multiply(quantity) ,currencyConversion.getEnvironment());
}
@GetMapping("/currency-converter-feign/from/{from}/to/{to}/quantity/{quantity}")
public CurrencyConversion calculateCurrencyConversionFeign(
@PathVariable String from, @PathVariable String to, @PathVariable BigDecimal quantity
){
CurrencyConversion currencyConversion = currencyExchangeProxy.retrieveExchangeValue(from,to);
return new CurrencyConversion(currencyConversion.getId(),from,to,quantity,
currencyConversion.getConversionMultiple(), quantity.multiply(quantity) ,currencyConversion.getEnvironment());
}
}
- CurrencyConversion currencyConversion = currencyExchangeProxy.retrieveExchangeValue(from,to);
- 프록시로 주입 받아 다른 서버 서비스에서 결과를 가져옴
CurrencyExchangeProxy
@FeignClient(name = "currency-exchange", url="localhost:8000") // properties의 name
public interface CurrencyExchangeProxy
{
@GetMapping("/currency-exchange/from/{from}/to/{to}")
public CurrencyConversion retrieveExchangeValue(
@PathVariable String from, @PathVariable String to
);
}
CurrencyConversion
package com.javapp.microservices.currencyconversionservice.data;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.math.BigDecimal;
@AllArgsConstructor
@Getter
public class CurrencyConversion
{
private Long id;
private String from;
private String to;
private BigDecimal quantity;
private BigDecimal conversionMultiple;
private BigDecimal totalCalculatedAmount;
private String environment;
public CurrencyConversion() {
}
}
application.properties
spring.config.import=optional:configserver:http://localhost:8888
spring.application.name=currency-conversion
server.port=8100
결과
네이밍서버 (유레카 서버)
@FeignClient(name = "currency-exchange", url="localhost:8000") // properties의 name
에서 url 이 하드코딩 돼어있음 -> 변경에 대해서 닫혀있어야됨.
다음 포스팅에 ----
'Back-end > Spring Cloud (MSA)' 카테고리의 다른 글
Spring Cloud (MSA) - Resilience4j @Retry @CircuitBreaker(서킷브레이커) (0) | 2022.12.06 |
---|---|
Spring Cloud (MSA) API Gateway - 경로탐색(Exploring Routes), Logging Filter (0) | 2022.12.03 |
Spring Cloud (MSA) - 로드밸런싱 Eureka, Feign (0) | 2022.11.27 |
Spring Cloud (MSA) - Config Server (0) | 2022.11.25 |
Spring Cloud (MSA) - HATEOAS, HAL Explorer, Static Filtering, Actuator (0) | 2022.11.24 |
댓글