간단한 Hystrix Circuit Breaker를 실습하기 위해선 두개의 프로젝트를 생성해야한다.

서버 설정

1. pom.xml

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

2. application.properties에서 server port 설정 (서버, 클라이언트를 한 컴퓨터에서 실행하기 때문에 port를 설정해줘야함. )

server.port=8090

3. Controller 생성

package com.example.circuitbreakerbookstore;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class CircuitBreakerBookstoreApplication {
    @RequestMapping(value = "/recommended")
    public String readingList(){
        return "Spring in ACtion (Manning), Cloud Native Java, Learning Spring Boot";
    }
    public static void main(String[] args){
        SpringApplication.run(CircuitBreakerBookstoreApplication.class, args);
    }
}

 

클라이언트 설정

1. pom.xml

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.M3</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

2. application.properties

server.port=8080

3.Controller

package com.example.circuitbreakerreading;


import com.example.circuitbreakerreading.service.BookService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.web.client.RestTemplate;


@EnableCircuitBreaker
@RestController
@SpringBootApplication
public class CircuitBreakerReadingApplication {

    @Autowired
    private BookService bookService;

    @Bean
    public RestTemplate rest(RestTemplateBuilder builder) {
        return builder.build();
    }

    @RequestMapping("/to-read")
    public String toRead() {
        return bookService.readingList();
    }

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

4. Service

package com.example.circuitbreakerreading.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;

@Service
public class BookService {
    private final WebClient webClient;

    public BookService(){
        this.webClient = WebClient.builder().baseUrl("http://localhost:8090").build();
    }

    /*
        Hystrix Command 설정
        commandKey 별로 Circuit Breaker 생성
        fallbackMethod : 실패시 해당 메소드 실행
     */
    @HystrixCommand(commandKey = "EX", fallbackMethod = "reliable")
    public String readingList(){
        return webClient.get() //get 방식
                .uri("recommended") //endpoint
                .retrieve() // body를 가지고 온다는 뜻
                .bodyToMono(String.class)
                .block()
                ;
    }

    public String reliable(){
        return "Success Fallback";
    }
}

 

참고 :

https://spring.io/guides/gs/circuit-breaker/

'Spring' 카테고리의 다른 글

Springboot ribbon 적용하기  (1) 2020.03.03
Hystrix 란  (0) 2020.03.01

+ Recent posts