├── .gitignore ├── README.md ├── build.gradle ├── display ├── build.gradle └── src │ └── main │ ├── java │ └── com │ │ └── elevenst │ │ ├── DisplayApplication.java │ │ ├── api │ │ └── DisplayController.java │ │ └── service │ │ ├── FeignProductRemoteService.java │ │ ├── FeignProductRemoteServiceFallbackFactory.java │ │ ├── FeignProductRemoteServiceFallbackImpl.java │ │ ├── ProductRemoteService.java │ │ └── ProductRemoteServiceImpl.java │ └── resources │ └── application.yml ├── eureka-server ├── build.gradle └── src │ └── main │ ├── java │ └── com │ │ └── elevenst │ │ └── eurekaserver │ │ └── EurekaServerApplication.java │ └── resources │ └── application.yml ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── product ├── build.gradle └── src │ └── main │ ├── java │ └── com │ │ └── elevenst │ │ ├── ProductApplication.java │ │ └── api │ │ └── ProductController.java │ └── resources │ └── application.yml ├── settings.gradle └── zuul ├── build.gradle └── src └── main ├── java └── com │ └── elevenst │ └── zuul │ └── ZuulApplication.java └── resources └── application.yml /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | /build/ 3 | !gradle/wrapper/gradle-wrapper.jar 4 | 5 | ### STS ### 6 | .apt_generated 7 | .classpath 8 | .factorypath 9 | .project 10 | .settings 11 | .springBeans 12 | .sts4-cache 13 | 14 | ### IntelliJ IDEA ### 15 | .idea 16 | *.iws 17 | *.iml 18 | *.ipr 19 | /out/ 20 | **/out/ 21 | ### NetBeans ### 22 | /nbproject/private/ 23 | /nbbuild/ 24 | /dist/ 25 | /nbdist/ 26 | /.nb-gradle/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Spring Cloud Workshop 2 | 3 | ## How to start 4 | 5 | ### Repository Clone 받기 6 | 7 | ``` 8 | git clone https://github.com/Gunju-Ko/spring-cloud-workshop.git 9 | ``` 10 | 11 | ### 실습 시작 Branch로 이동 12 | ``` 13 | git checkout my-step-0 14 | ``` 15 | 16 | ### 실습을 건너뛰고 막바로 특정 단계에서 시작하기 17 | ``` 18 | git checkout tags/ 19 | ``` 20 | 혹은 21 | ``` 22 | git checkout tags/ -b <새로운 브랜치 이름> 23 | ``` 24 | 25 | ## 주요 단계별 Tag 설명 26 | 27 | - `step-0` : 실습 시작 28 | - `step-1` : RestTemplate을 이용한 서버 호출 실습 29 | - `step-2-hystrix-baseline` : Hystrix 실습 초기 상태 30 | - `step-2-hystrix-basic` : Hystrix 기본 적용 31 | - `step-2-hystrix-fallback` : Hystrix Fallback 적용 32 | - `step-2-hystrix-fallback2` : Hystrix Fallback에서 Throwable 받기 33 | - `step-2-hystrix-tiemout` : Hystrix Timeout 실습 34 | - `step-2-hystrix-circuit-open` : Hystrix Circuit Open 실험 35 | - `step-2-hystrix-command-key` : Hystrix Command 에 command key 부여하기 36 | - `step-3-baseline` : Ribbon 실습 초기 상태 37 | - `step-3-ribbon-loadbalanced` : @LoadBalanced RestTemplate 사용하기 38 | - `step-3-ribbon-retry` : Ribbon retry 설정 및 실험 39 | - `step-4-baseline` : Eureka 실습 초기 상 - Eureka Server 추가되어 있음 40 | - `step-4-eureka-client` : Product, Display 서버에 Eureka Client 추가 41 | - `step-4-eureka-completed` : 불필요한 설정 삭제 42 | - `step-5-baseline` : Feign 실습 초기 상태 43 | - `step-5-feign-url` : URL과 함께 Feign 사용하기 44 | - `step-5-feign-eureka` : Feign을 Eureka + Ribbon과 함께 사용하기 45 | - `step-5-feign-hystrix` : Feign에 Hystrix 적용하기 (Fallback 포함) 46 | - `step-5-feign-fallbackfactory` : FallbackFactory를 이용하여 Fallback 적용 47 | - `step-5-feign-hystrix-properties` : Feign의 Hystrix에 Properties 추가하는 법 48 | - `step-6-zuul-baseline` : Zuul 실습 초기 상태 49 | - `step-6-zuul-proxy-with-eureka` : Eureka를 이용한 Zuul Proxy 사용 50 | - `step-6-zuul-isolation-thread-pool` : ThreadPool을 이용한 Isolation 사용 51 | - `step-6-zuul-ribbon-config` : serviceId별 Ribbon 설정하기 52 | - `step-ended` : 실습 완료 -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | } 4 | 5 | dependencies { 6 | compile project(':display') 7 | compile project(':product') 8 | compile project(':eureka-server') 9 | } -------------------------------------------------------------------------------- /display/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | springBootVersion = '2.0.7.RELEASE' 4 | } 5 | repositories { 6 | mavenCentral() 7 | } 8 | dependencies { 9 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 10 | } 11 | } 12 | 13 | ext['springCloudVersion'] = 'Finchley.SR2' 14 | 15 | apply plugin: 'java' 16 | apply plugin: 'eclipse' 17 | apply plugin: 'org.springframework.boot' 18 | apply plugin: 'io.spring.dependency-management' 19 | 20 | group = 'com.elevenst' 21 | version = '0.0.1-SNAPSHOT' 22 | sourceCompatibility = 1.8 23 | 24 | repositories { 25 | mavenCentral() 26 | } 27 | 28 | dependencies { 29 | compile('org.springframework.retry:spring-retry:1.2.2.RELEASE') // spring cloud requires spring-retry for auto-retry 30 | compile('org.springframework.cloud:spring-cloud-starter-openfeign') // To use Feign 31 | compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client') // 3. To use Eureka client 32 | compile('org.springframework.cloud:spring-cloud-starter-netflix-ribbon') // 2. To use ribbon 33 | compile('org.springframework.cloud:spring-cloud-starter-netflix-hystrix') // 1. To use spring-cloud-hystrix 34 | compile('org.springframework.boot:spring-boot-starter-web') 35 | testCompile('org.springframework.boot:spring-boot-starter-test') 36 | } 37 | 38 | dependencyManagement { 39 | imports { 40 | mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /display/src/main/java/com/elevenst/DisplayApplication.java: -------------------------------------------------------------------------------- 1 | package com.elevenst; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; 6 | import org.springframework.cloud.client.loadbalancer.LoadBalanced; 7 | import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 8 | import org.springframework.cloud.openfeign.EnableFeignClients; 9 | import org.springframework.context.annotation.Bean; 10 | import org.springframework.web.client.RestTemplate; 11 | 12 | @SpringBootApplication 13 | @EnableCircuitBreaker 14 | @EnableEurekaClient 15 | @EnableFeignClients 16 | public class DisplayApplication { 17 | 18 | @Bean 19 | @LoadBalanced 20 | public RestTemplate restTemplate() { 21 | return new RestTemplate(); 22 | } 23 | 24 | public static void main(String[] args) { 25 | SpringApplication.run(DisplayApplication.class); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /display/src/main/java/com/elevenst/api/DisplayController.java: -------------------------------------------------------------------------------- 1 | package com.elevenst.api; 2 | 3 | import com.elevenst.service.FeignProductRemoteService; 4 | import com.elevenst.service.ProductRemoteService; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.PathVariable; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | @RestController 11 | @RequestMapping(path = "/displays") 12 | public class DisplayController { 13 | 14 | private final ProductRemoteService productRemoteService; 15 | private final FeignProductRemoteService feignProductRemoteService; 16 | 17 | public DisplayController(ProductRemoteService productRemoteService, 18 | FeignProductRemoteService feignProductRemoteService) { 19 | this.productRemoteService = productRemoteService; 20 | this.feignProductRemoteService = feignProductRemoteService; 21 | } 22 | 23 | @GetMapping(path = "/{displayId}") 24 | public String getDisplayDetail(@PathVariable String displayId) { 25 | String productInfo = getProductInfo(); 26 | return String.format("[display id = %s at %s %s ]", displayId, System.currentTimeMillis(), productInfo); 27 | } 28 | 29 | private String getProductInfo() { 30 | return feignProductRemoteService.getProductInfo("12345"); 31 | } 32 | } -------------------------------------------------------------------------------- /display/src/main/java/com/elevenst/service/FeignProductRemoteService.java: -------------------------------------------------------------------------------- 1 | package com.elevenst.service; 2 | 3 | import org.springframework.cloud.openfeign.FeignClient; 4 | import org.springframework.web.bind.annotation.PathVariable; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | 7 | @FeignClient(name = "product", fallbackFactory = FeignProductRemoteServiceFallbackFactory.class) 8 | public interface FeignProductRemoteService { 9 | @RequestMapping(path = "/products/{productId}") 10 | String getProductInfo(@PathVariable("productId") String productId); 11 | } -------------------------------------------------------------------------------- /display/src/main/java/com/elevenst/service/FeignProductRemoteServiceFallbackFactory.java: -------------------------------------------------------------------------------- 1 | package com.elevenst.service; 2 | 3 | import feign.hystrix.FallbackFactory; 4 | import org.springframework.stereotype.Component; 5 | 6 | @Component 7 | public class FeignProductRemoteServiceFallbackFactory implements FallbackFactory { 8 | 9 | @Override 10 | public FeignProductRemoteService create(Throwable cause) { 11 | System.out.println("t = " + cause); 12 | return productId -> "[ this product is sold out ]"; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /display/src/main/java/com/elevenst/service/FeignProductRemoteServiceFallbackImpl.java: -------------------------------------------------------------------------------- 1 | package com.elevenst.service; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | @Component 6 | public class FeignProductRemoteServiceFallbackImpl implements FeignProductRemoteService { 7 | @Override 8 | public String getProductInfo(String productId) { 9 | return "[ this product is sold out ]"; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /display/src/main/java/com/elevenst/service/ProductRemoteService.java: -------------------------------------------------------------------------------- 1 | package com.elevenst.service; 2 | 3 | public interface ProductRemoteService { 4 | String getProductInfo(String productId); 5 | } 6 | -------------------------------------------------------------------------------- /display/src/main/java/com/elevenst/service/ProductRemoteServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.elevenst.service; 2 | 3 | import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; 4 | import org.springframework.stereotype.Service; 5 | import org.springframework.web.client.RestTemplate; 6 | 7 | @Service 8 | public class ProductRemoteServiceImpl implements ProductRemoteService { 9 | 10 | private static final String url = "http://product/products/"; 11 | private final RestTemplate restTemplate; 12 | 13 | public ProductRemoteServiceImpl(RestTemplate restTemplate) { 14 | this.restTemplate = restTemplate; 15 | } 16 | 17 | @Override 18 | @HystrixCommand(commandKey = "productInfo", fallbackMethod = "getProductInfoFallback") 19 | public String getProductInfo(String productId) { 20 | return this.restTemplate.getForObject(url + productId, String.class); 21 | } 22 | 23 | public String getProductInfoFallback(String productId, Throwable t) { 24 | System.out.println("t = " + t); 25 | return "[ this product is sold out ]"; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /display/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8081 3 | 4 | spring: 5 | application: 6 | name: display 7 | 8 | hystrix: 9 | command: 10 | productInfo: # command key. use 'default' for global setting. 11 | execution: 12 | isolation: 13 | thread: 14 | timeoutInMilliseconds: 3000 15 | circuitBreaker: 16 | requestVolumeThreshold: 1 # Minimum number of request to calculate circuit breaker's health. default 20 17 | errorThresholdPercentage: 50 # Error percentage to open circuit. default 50 18 | FeignProductRemoteService#getProductInfo(String): 19 | execution: 20 | isolation: 21 | thread: 22 | timeoutInMilliseconds: 3000 # default 1,000ms 23 | circuitBreaker: 24 | requestVolumeThreshold: 1 # Minimum number of request to calculate circuit breaker's health. default 20 25 | errorThresholdPercentage: 50 # Error percentage to open circuit. default 50 26 | 27 | product: 28 | ribbon: 29 | MaxAutoRetries: 0 30 | MaxAutoRetriesNextServer: 1 31 | 32 | eureka: 33 | instance: 34 | prefer-ip-address: true 35 | client: 36 | service-url: 37 | defaultZone: http://127.0.0.1:8761/eureka # default address 38 | 39 | feign: 40 | hystrix: 41 | enabled: true 42 | -------------------------------------------------------------------------------- /eureka-server/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | springBootVersion = '2.0.7.RELEASE' 4 | } 5 | repositories { 6 | mavenCentral() 7 | } 8 | dependencies { 9 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 10 | } 11 | } 12 | 13 | ext['springCloudVersion'] = 'Finchley.SR2' 14 | 15 | apply plugin: 'java' 16 | apply plugin: 'eclipse' 17 | apply plugin: 'org.springframework.boot' 18 | apply plugin: 'io.spring.dependency-management' 19 | 20 | group = 'com.elevenst' 21 | version = '0.0.1-SNAPSHOT' 22 | sourceCompatibility = 1.8 23 | 24 | repositories { 25 | mavenCentral() 26 | } 27 | 28 | dependencies { 29 | compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-server') 30 | testCompile('org.springframework.boot:spring-boot-starter-test') 31 | } 32 | 33 | dependencyManagement { 34 | imports { 35 | mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" 36 | } 37 | } -------------------------------------------------------------------------------- /eureka-server/src/main/java/com/elevenst/eurekaserver/EurekaServerApplication.java: -------------------------------------------------------------------------------- 1 | package com.elevenst.eurekaserver; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; 6 | 7 | @SpringBootApplication 8 | @EnableEurekaServer 9 | public class EurekaServerApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(EurekaServerApplication.class, args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /eureka-server/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8761 # Default : 8761 3 | 4 | spring: 5 | application: 6 | name: eureka-server 7 | 8 | eureka: 9 | server: 10 | response-cache-update-interval-ms: 1000 # Eureka Server's Response Cache. Default 30,000ms 11 | enableSelfPreservation: false # Just for demo 12 | client: 13 | register-with-eureka: false # Only for local stand-alone development 14 | fetch-registry: false # Only for local stand-alone development 15 | service-url: 16 | defaultZone: http://localhost:8761/eureka # Default Value. Just for demo 17 | instance: 18 | prefer-ip-address: true # Use ip address instead of hostname from OS when reporting myself to eureka server -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Gunju-Ko/spring-cloud-workshop/4bd300eb2533fd1e0afcec025ab80e46621c1f3e/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Dec 10 15:17:03 KST 2018 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /product/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | springBootVersion = '2.0.7.RELEASE' 4 | } 5 | repositories { 6 | mavenCentral() 7 | } 8 | dependencies { 9 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 10 | } 11 | } 12 | 13 | ext['springCloudVersion'] = 'Finchley.SR2' 14 | 15 | apply plugin: 'java' 16 | apply plugin: 'eclipse' 17 | apply plugin: 'org.springframework.boot' 18 | apply plugin: 'io.spring.dependency-management' 19 | 20 | group = 'com.elevenst' 21 | version = '0.0.1-SNAPSHOT' 22 | sourceCompatibility = 1.8 23 | 24 | repositories { 25 | mavenCentral() 26 | } 27 | 28 | dependencies { 29 | compile('org.springframework.boot:spring-boot-starter-web') 30 | compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client') // To use Eureka client 31 | testCompile('org.springframework.boot:spring-boot-starter-test') 32 | } 33 | 34 | dependencyManagement { 35 | imports { 36 | mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" 37 | } 38 | } 39 | 40 | -------------------------------------------------------------------------------- /product/src/main/java/com/elevenst/ProductApplication.java: -------------------------------------------------------------------------------- 1 | package com.elevenst; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 6 | 7 | @SpringBootApplication 8 | @EnableEurekaClient 9 | public class ProductApplication { 10 | public static void main(String[] args) { 11 | SpringApplication.run(ProductApplication.class); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /product/src/main/java/com/elevenst/api/ProductController.java: -------------------------------------------------------------------------------- 1 | package com.elevenst.api; 2 | 3 | import org.springframework.web.bind.annotation.GetMapping; 4 | import org.springframework.web.bind.annotation.PathVariable; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | @RestController 9 | @RequestMapping("/products") 10 | public class ProductController { 11 | 12 | @GetMapping(path = "{productId}") 13 | public String getProductInfo(@PathVariable String productId) { 14 | // try { 15 | // Thread.sleep(2000); 16 | // } catch (InterruptedException e) { 17 | // e.printStackTrace(); 18 | // } 19 | 20 | return "[product id = " + productId + " at " + System.currentTimeMillis() + "]"; 21 | // throw new RuntimeException("I/O Exception"); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /product/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8082 3 | 4 | spring: 5 | application: 6 | name: product 7 | 8 | eureka: 9 | instance: 10 | prefer-ip-address: true 11 | client: 12 | service-url: 13 | defaultZone: http://127.0.0.1:8761/eureka # default address -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'spring-cloud-workshop' 2 | include 'product' 3 | include 'display' 4 | include 'eureka-server' 5 | include 'zuul' 6 | 7 | -------------------------------------------------------------------------------- /zuul/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | springBootVersion = '2.0.7.RELEASE' 4 | } 5 | repositories { 6 | mavenCentral() 7 | } 8 | dependencies { 9 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 10 | } 11 | } 12 | 13 | ext['springCloudVersion'] = 'Finchley.SR2' 14 | 15 | apply plugin: 'java' 16 | apply plugin: 'eclipse' 17 | apply plugin: 'org.springframework.boot' 18 | apply plugin: 'io.spring.dependency-management' 19 | 20 | group = 'com.elevenst' 21 | version = '0.0.1-SNAPSHOT' 22 | sourceCompatibility = 1.8 23 | 24 | repositories { 25 | mavenCentral() 26 | } 27 | 28 | dependencies { 29 | compile('org.springframework.retry:spring-retry:1.2.2.RELEASE') 30 | compile('org.springframework.cloud:spring-cloud-starter-netflix-zuul') 31 | compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client') 32 | testCompile('org.springframework.boot:spring-boot-starter-test') 33 | } 34 | 35 | dependencyManagement { 36 | imports { 37 | mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" 38 | } 39 | } -------------------------------------------------------------------------------- /zuul/src/main/java/com/elevenst/zuul/ZuulApplication.java: -------------------------------------------------------------------------------- 1 | package com.elevenst.zuul; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | import org.springframework.cloud.netflix.zuul.EnableZuulProxy; 7 | 8 | @EnableZuulProxy 9 | @EnableDiscoveryClient 10 | @SpringBootApplication 11 | public class ZuulApplication { 12 | 13 | public static void main(String[] args) { 14 | SpringApplication.run(ZuulApplication.class, args); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /zuul/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: zuul 4 | 5 | server: 6 | port: 8765 7 | 8 | zuul: 9 | routes: 10 | product: 11 | path: /products/** 12 | serviceId: product 13 | stripPrefix: false 14 | display: 15 | path: /displays/** 16 | serviceId: display 17 | stripPrefix: false 18 | ribbon-isolation-strategy: thread 19 | thread-pool: 20 | use-separate-thread-pools: true 21 | thread-pool-key-prefix: zuul- 22 | 23 | eureka: 24 | instance: 25 | non-secure-port: ${server.port} 26 | prefer-ip-address: true 27 | client: 28 | serviceUrl: 29 | defaultZone: http://localhost:8761/eureka/ 30 | 31 | hystrix: 32 | command: 33 | default: 34 | execution: 35 | isolation: 36 | thread: 37 | timeoutInMilliseconds: 1000 38 | product: 39 | execution: 40 | isolation: 41 | thread: 42 | timeoutInMilliseconds: 10000 43 | threadpool: 44 | zuul-product: 45 | coreSize: 30 46 | maximumSize: 100 47 | allowMaximumSizeToDivergeFromCoreSize: true 48 | zuul-display: 49 | coreSize: 30 50 | maximumSize: 100 51 | allowMaximumSizeToDivergeFromCoreSize: true 52 | 53 | product: 54 | ribbon: 55 | MaxAutoRetriesNextServer: 1 56 | ReadTimeout: 3000 57 | ConnectTimeout: 1000 58 | MaxTotalConnections: 300 59 | MaxConnectionsPerHost: 100 60 | 61 | display: 62 | ribbon: 63 | MaxAutoRetriesNextServer: 1 64 | ReadTimeout: 3000 65 | ConnectTimeout: 1000 66 | MaxTotalConnections: 300 67 | MaxConnectionsPerHost: 100 --------------------------------------------------------------------------------