├── .gitignore ├── LICENSE ├── README.md ├── lab01 ├── README.md └── hystrix-demo │ ├── pom.xml │ └── src │ ├── main │ └── java │ │ └── io │ │ └── spring2go │ │ └── hystrix │ │ ├── command │ │ ├── CommandGetTodaysRaces.java │ │ └── caching │ │ │ └── CommandGetHorsesInRaceWithCaching.java │ │ ├── domain │ │ ├── Horse.java │ │ └── RaceCourse.java │ │ ├── exception │ │ └── RemoteServiceException.java │ │ └── service │ │ └── BettingService.java │ └── test │ └── java │ └── io │ └── spring2go │ └── hystrix │ └── BettingServiceTest.java ├── lab02 ├── README.md └── curl.sh ├── lab03 ├── README.md ├── school-service │ ├── .gitignore │ ├── .mvn │ │ └── wrapper │ │ │ ├── maven-wrapper.jar │ │ │ └── maven-wrapper.properties │ ├── mvnw │ ├── mvnw.cmd │ ├── pom.xml │ └── src │ │ ├── main │ │ ├── java │ │ │ ├── META-INF │ │ │ │ └── MANIFEST.MF │ │ │ └── io │ │ │ │ └── spring2go │ │ │ │ └── hystrixlab │ │ │ │ └── schoolservice │ │ │ │ ├── SchoolServiceApplication.java │ │ │ │ ├── controller │ │ │ │ └── SchoolServiceController.java │ │ │ │ └── delegate │ │ │ │ └── StudentServiceDelegate.java │ │ └── resources │ │ │ └── application.properties │ │ └── test │ │ └── java │ │ └── io │ │ └── spring2go │ │ └── hystrixlab │ │ └── schoolservice │ │ └── SchoolServiceApplicationTests.java └── student-service │ ├── .gitignore │ ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties │ ├── mvnw │ ├── mvnw.cmd │ ├── pom.xml │ └── src │ ├── main │ ├── java │ │ ├── META-INF │ │ │ └── MANIFEST.MF │ │ └── io │ │ │ └── spring2go │ │ │ └── hystrixlab │ │ │ └── studentservice │ │ │ ├── StudentServiceApplication.java │ │ │ ├── controller │ │ │ └── StudentServiceController.java │ │ │ └── domain │ │ │ └── Student.java │ └── resources │ │ └── application.properties │ └── test │ └── java │ └── io │ └── spring2go │ └── hystrixlab │ └── studentservice │ └── StudentServiceApplicationTests.java └── ppt └── 微服务容错限流Hystrix架构和实践.pdf /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | 22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 23 | hs_err_pid* 24 | 25 | target/ 26 | !.mvn/wrapper/maven-wrapper.jar 27 | 28 | ### STS ### 29 | .classpath 30 | .project 31 | .settings 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 spring2go.com 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # hystrix_lab 2 | 微服务容错限流Hystrix实验,极客时间微服务架构实践课程 3 | 4 | # 课程ppt 5 | 1. [微服务容错限流Hystrix架构和实践](ppt/微服务容错限流Hystrix架构和实践.pdf) 6 | 7 | # 实验软件需求 8 | 1. [JDK 1.8+](http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html) 9 | 2. [Git for Windows](https://gitforwindows.org/) 10 | 3. Eclipse STS 3.9.5下载1[网盘](https://pan.baidu.com/s/1xqy4G_r9N24WODBBuGlIog) 下载2[国外](https://spring.io/tools) 11 | 4. [Postman](https://www.getpostman.com/) 12 | 13 | # 实验目录 14 | 1. [lab01](lab01)~Hystrix基础实验 15 | 2. [lab02](lab02)~Hystrix+Dashboard实验 16 | 3. [lab03](lab03)~Spring Cloud Hystrix实验 17 | 18 | # 注意 19 | 1. 所有实验仅供学习参考,不是生产级 20 | 2. 实验和ppt采用[Mit license](LICENSE) 21 | 22 | # 极客时间课程 23 | 24 | 《微服务架构实战160讲》 25 | 26 | ![微服务架构实战160讲](https://github.com/spring2go/oauth2lab/blob/master/image/course_ad.jpg) 27 | -------------------------------------------------------------------------------- /lab01/README.md: -------------------------------------------------------------------------------- 1 | 实验一、Hystrix基础实验 2 | ====== 3 | 4 | ### 实验步骤 5 | 6 | #### 1. 导入hystrix-demo项目到IDE 7 | 8 | ``` 9 | https://github.com/spring2go/hystrix-lab/lab01/hystrix_demo 10 | ``` 11 | 12 | #### 2. 运行测试 13 | 14 | ``` 15 | io.spring2go.hystrix.BettingServiceTest.java 16 | ``` 17 | 18 | 实验样例: 19 | 20 | 1. 同步调用testSynchronous 21 | 2. 同步安静失败testSynchronousFailSilently 22 | 3. 同步快速失败testSynchronousFailFast 23 | 4. 异步调用testAsynchronous 24 | 5. 响应式调用testObservable 25 | 6. 缓存调用testWithCacheHist 26 | 27 | -------------------------------------------------------------------------------- /lab01/hystrix-demo/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | io.spring2go 5 | hystrix_demo 6 | 0.0.1-SNAPSHOT 7 | Hystrix Demo 8 | 9 | 10 | 11 | 12 | com.netflix.hystrix 13 | hystrix-core 14 | 1.4.6 15 | 16 | 17 | io.reactivex 18 | rxjava 19 | 1.0.12 20 | 21 | 22 | com.netflix.hystrix 23 | hystrix-javanica 24 | 1.4.12 25 | 26 | 27 | 28 | commons-lang 29 | commons-lang 30 | 2.6 31 | 32 | 33 | 34 | 35 | org.mockito 36 | mockito-all 37 | 1.8.0 38 | test 39 | 40 | 41 | org.springframework 42 | spring-test 43 | 4.1.3.RELEASE 44 | test 45 | 46 | 47 | 48 | junit 49 | junit 50 | 4.7 51 | test 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /lab01/hystrix-demo/src/main/java/io/spring2go/hystrix/command/CommandGetTodaysRaces.java: -------------------------------------------------------------------------------- 1 | package io.spring2go.hystrix.command; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import io.spring2go.hystrix.domain.RaceCourse; 7 | import io.spring2go.hystrix.exception.RemoteServiceException; 8 | import io.spring2go.hystrix.service.BettingService; 9 | import com.netflix.hystrix.HystrixCommand; 10 | import com.netflix.hystrix.HystrixCommandGroupKey; 11 | import com.netflix.hystrix.HystrixThreadPoolKey; 12 | 13 | /** 14 | * Get a list of all Race courses with races on today. 15 | * 16 | */ 17 | public class CommandGetTodaysRaces extends HystrixCommand> { 18 | 19 | private final BettingService service; 20 | private final boolean failSilently; 21 | 22 | /** 23 | * CommandGetTodaysRaces 24 | * 25 | * @param service 26 | * Remote Broker Service 27 | * @param failSilently 28 | * If true will return an empty list if a remote service exception is thrown, if 29 | * false will throw a BettingServiceException. 30 | */ 31 | public CommandGetTodaysRaces(BettingService service, boolean failSilently) { 32 | super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("BettingServiceGroup")) 33 | .andThreadPoolKey( 34 | HystrixThreadPoolKey.Factory.asKey("BettingServicePool"))); 35 | 36 | this.service = service; 37 | this.failSilently = failSilently; 38 | } 39 | 40 | public CommandGetTodaysRaces(BettingService service) { 41 | this(service, true); 42 | } 43 | 44 | @Override 45 | protected List run() { 46 | return service.getTodaysRaces(); 47 | } 48 | 49 | @Override 50 | protected List getFallback() { 51 | // can log here, throw exception or return default 52 | if (failSilently) { 53 | return new ArrayList(); 54 | } 55 | 56 | throw new RemoteServiceException("Unexpected error retrieving todays races"); 57 | 58 | } 59 | 60 | } -------------------------------------------------------------------------------- /lab01/hystrix-demo/src/main/java/io/spring2go/hystrix/command/caching/CommandGetHorsesInRaceWithCaching.java: -------------------------------------------------------------------------------- 1 | package io.spring2go.hystrix.command.caching; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import io.spring2go.hystrix.domain.Horse; 7 | import io.spring2go.hystrix.service.BettingService; 8 | import com.netflix.hystrix.HystrixCommand; 9 | import com.netflix.hystrix.HystrixCommandGroupKey; 10 | import com.netflix.hystrix.HystrixThreadPoolKey; 11 | 12 | /** 13 | * Get List of Horses in a specific race. Note, calls via this command are cached. 14 | * 15 | */ 16 | public class CommandGetHorsesInRaceWithCaching extends HystrixCommand> { 17 | 18 | private final BettingService service; 19 | private final String raceCourseId; 20 | 21 | /** 22 | * CommandGetHorsesInRaceWithCaching. 23 | * @param service 24 | * Remote Broker Service 25 | * @param raceCourseId 26 | * Id of race course 27 | */ 28 | public CommandGetHorsesInRaceWithCaching(BettingService service, String raceCourseId) { 29 | super(Setter.withGroupKey( 30 | HystrixCommandGroupKey.Factory.asKey("BettingServiceGroup")) 31 | .andThreadPoolKey( 32 | HystrixThreadPoolKey.Factory.asKey("BettingServicePool"))); 33 | 34 | this.service = service; 35 | this.raceCourseId = raceCourseId; 36 | } 37 | 38 | @Override 39 | protected List run() { 40 | return service.getHorsesInRace(raceCourseId); 41 | } 42 | 43 | @Override 44 | protected List getFallback() { 45 | // can log here, throw exception or return default 46 | return new ArrayList(); 47 | } 48 | 49 | @Override 50 | protected String getCacheKey() { 51 | return raceCourseId; 52 | } 53 | 54 | } -------------------------------------------------------------------------------- /lab01/hystrix-demo/src/main/java/io/spring2go/hystrix/domain/Horse.java: -------------------------------------------------------------------------------- 1 | package io.spring2go.hystrix.domain; 2 | 3 | import org.apache.commons.lang.builder.EqualsBuilder; 4 | import org.apache.commons.lang.builder.HashCodeBuilder; 5 | 6 | public class Horse { 7 | 8 | private String id; 9 | private String name; 10 | 11 | public Horse(String id, String name) { 12 | super(); 13 | this.id = id; 14 | this.name = name; 15 | } 16 | 17 | public String getId() { 18 | return id; 19 | } 20 | 21 | public void setId(String id) { 22 | this.id = id; 23 | } 24 | 25 | public String getName() { 26 | return name; 27 | } 28 | 29 | public void setName(String name) { 30 | this.name = name; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return "Horse [id=" + id + ", name=" + name + "]"; 36 | } 37 | 38 | @Override 39 | public int hashCode() { 40 | return HashCodeBuilder.reflectionHashCode(this); 41 | } 42 | 43 | @Override 44 | public boolean equals(Object obj) { 45 | return EqualsBuilder.reflectionEquals(this, obj); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /lab01/hystrix-demo/src/main/java/io/spring2go/hystrix/domain/RaceCourse.java: -------------------------------------------------------------------------------- 1 | package io.spring2go.hystrix.domain; 2 | 3 | import org.apache.commons.lang.builder.EqualsBuilder; 4 | import org.apache.commons.lang.builder.HashCodeBuilder; 5 | 6 | public class RaceCourse { 7 | 8 | private String id; 9 | private String name; 10 | 11 | public RaceCourse(String id, String name) { 12 | super(); 13 | this.id = id; 14 | this.name = name; 15 | } 16 | 17 | public String getId() { 18 | return id; 19 | } 20 | 21 | public void setId(String id) { 22 | this.id = id; 23 | } 24 | 25 | public String getName() { 26 | return name; 27 | } 28 | 29 | public void setName(String name) { 30 | this.name = name; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return "RaceCourse [id=" + id + ", name=" + name + "]"; 36 | } 37 | 38 | @Override 39 | public int hashCode() { 40 | return HashCodeBuilder.reflectionHashCode(this); 41 | } 42 | 43 | @Override 44 | public boolean equals(Object obj) { 45 | return EqualsBuilder.reflectionEquals(this, obj); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /lab01/hystrix-demo/src/main/java/io/spring2go/hystrix/exception/RemoteServiceException.java: -------------------------------------------------------------------------------- 1 | package io.spring2go.hystrix.exception; 2 | 3 | public class RemoteServiceException extends RuntimeException { 4 | 5 | private static final long serialVersionUID = 1L; 6 | 7 | public RemoteServiceException(String message){ 8 | super(message); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /lab01/hystrix-demo/src/main/java/io/spring2go/hystrix/service/BettingService.java: -------------------------------------------------------------------------------- 1 | package io.spring2go.hystrix.service; 2 | 3 | import java.util.List; 4 | 5 | import io.spring2go.hystrix.domain.Horse; 6 | import io.spring2go.hystrix.domain.RaceCourse; 7 | 8 | /** 9 | * Simulates the interface for a remote betting service. 10 | * 11 | */ 12 | public interface BettingService { 13 | 14 | /** 15 | * Get a list the names of all Race courses with races on today. 16 | * @return List of race course names 17 | */ 18 | List getTodaysRaces(); 19 | 20 | /** 21 | * Get a list of all Horses running in a particular race. 22 | * @param race Name of race course 23 | * @return List of the names of all horses running in the specified race 24 | */ 25 | List getHorsesInRace(String raceCourseId); 26 | 27 | /** 28 | * Get current odds for a particular horse in a specific race today. 29 | * @param race Name of race course 30 | * @param horse Name of horse 31 | * @return Current odds as a string (e.g. "10/1") 32 | */ 33 | String getOddsForHorse(String raceCourseId, String horseId); 34 | } 35 | -------------------------------------------------------------------------------- /lab01/hystrix-demo/src/test/java/io/spring2go/hystrix/BettingServiceTest.java: -------------------------------------------------------------------------------- 1 | package io.spring2go.hystrix; 2 | 3 | import static org.junit.Assert.*; 4 | import static org.mockito.Mockito.mock; 5 | import static org.mockito.Mockito.when; 6 | import static org.mockito.Mockito.verify; 7 | import static org.mockito.Mockito.verifyNoMoreInteractions; 8 | 9 | import java.util.ArrayList; 10 | import java.util.Arrays; 11 | import java.util.List; 12 | import java.util.concurrent.Future; 13 | 14 | import org.junit.Before; 15 | import org.junit.Test; 16 | 17 | import rx.Observable; 18 | 19 | import io.spring2go.hystrix.command.CommandGetTodaysRaces; 20 | import io.spring2go.hystrix.command.caching.CommandGetHorsesInRaceWithCaching; 21 | import io.spring2go.hystrix.domain.Horse; 22 | import io.spring2go.hystrix.domain.RaceCourse; 23 | import io.spring2go.hystrix.exception.RemoteServiceException; 24 | import io.spring2go.hystrix.service.BettingService; 25 | import com.netflix.hystrix.HystrixCommand; 26 | import com.netflix.hystrix.HystrixCommandKey; 27 | import com.netflix.hystrix.HystrixEventType; 28 | import com.netflix.hystrix.HystrixRequestCache; 29 | import com.netflix.hystrix.HystrixRequestLog; 30 | import com.netflix.hystrix.exception.HystrixRuntimeException; 31 | import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategyDefault; 32 | import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext; 33 | 34 | /** 35 | * Unit Test Suite that illustrates various approaches in using Hystrix Commands to access remote services. 36 | * 37 | * Uses Mockito to wire in a mock remote betting service. 38 | * 39 | */ 40 | public class BettingServiceTest { 41 | 42 | private static final String RACE_1 = "course_france"; 43 | private static final String HORSE_1 = "horse_white"; 44 | private static final String HORSE_2 = "horse_black"; 45 | 46 | private static final String ODDS_RACE_1_HORSE_1 = "10/1"; 47 | private static final String ODDS_RACE_1_HORSE_2 = "100/1"; 48 | 49 | private static final HystrixCommandKey GETTER_KEY = HystrixCommandKey.Factory.asKey("GetterCommand"); 50 | 51 | private BettingService mockService; 52 | 53 | /** 54 | * Set up the shared Unit Test environment 55 | */ 56 | @Before 57 | public void setUp() { 58 | mockService = mock(BettingService.class); 59 | when(mockService.getTodaysRaces()).thenReturn(getRaceCourses()); 60 | when(mockService.getHorsesInRace(RACE_1)).thenReturn(getHorsesAtFrance()); 61 | when(mockService.getOddsForHorse(RACE_1, HORSE_1)).thenReturn(ODDS_RACE_1_HORSE_1); 62 | when(mockService.getOddsForHorse(RACE_1, HORSE_2)).thenReturn(ODDS_RACE_1_HORSE_2); 63 | } 64 | 65 | /** 66 | * Command GetRaces - Execute (synchronous call). 67 | */ 68 | @Test 69 | public void testSynchronous() { 70 | 71 | CommandGetTodaysRaces commandGetRaces = new CommandGetTodaysRaces(mockService); 72 | assertEquals(getRaceCourses(), commandGetRaces.execute()); 73 | 74 | verify(mockService).getTodaysRaces(); 75 | verifyNoMoreInteractions(mockService); 76 | } 77 | 78 | /** 79 | * Command GetRaces - Execute and Fail Silently. 80 | * Swallows remote server error and returns an empty list. 81 | */ 82 | @Test 83 | public void testSynchronousFailSilently() { 84 | 85 | CommandGetTodaysRaces commandGetRacesFailure = new CommandGetTodaysRaces(mockService); 86 | // override mock to mimic an error being thrown for this test 87 | when(mockService.getTodaysRaces()).thenThrow(new RuntimeException("Error!!")); 88 | assertEquals(new ArrayList(), commandGetRacesFailure.execute()); 89 | 90 | // Verify 91 | verify(mockService).getTodaysRaces(); 92 | verifyNoMoreInteractions(mockService); 93 | } 94 | 95 | /** 96 | * Command GetRaces - Execute and Fail Fast. 97 | * Catches remote server error and throws a new Exception. 98 | */ 99 | @Test 100 | public void testSynchronousFailFast() { 101 | CommandGetTodaysRaces commandGetRacesFailure = new CommandGetTodaysRaces(mockService, false); 102 | // override mock to mimic an error being thrown for this test 103 | when(mockService.getTodaysRaces()).thenThrow(new RuntimeException("Error!!")); 104 | try{ 105 | commandGetRacesFailure.execute(); 106 | }catch(HystrixRuntimeException hre){ 107 | assertEquals(RemoteServiceException.class, hre.getFallbackException().getClass()); 108 | } 109 | 110 | verify(mockService).getTodaysRaces(); 111 | verifyNoMoreInteractions(mockService); 112 | } 113 | 114 | /** 115 | * Command GetRaces - Queue (Asynchronous) 116 | */ 117 | @Test 118 | public void testAsynchronous() throws Exception { 119 | CommandGetTodaysRaces commandGetRaces = new CommandGetTodaysRaces(mockService); 120 | Future> future = commandGetRaces.queue(); 121 | assertEquals(getRaceCourses(), future.get()); 122 | 123 | verify(mockService).getTodaysRaces(); 124 | verifyNoMoreInteractions(mockService); 125 | } 126 | 127 | /** 128 | * Command - Observe (Hot Observable) 129 | */ 130 | @Test 131 | public void testObservable() throws Exception { 132 | CommandGetTodaysRaces commandGetRaces = new CommandGetTodaysRaces(mockService); 133 | Observable> observable = commandGetRaces.observe(); 134 | // blocking observable 135 | assertEquals(getRaceCourses(), observable.toBlocking().single()); 136 | 137 | verify(mockService).getTodaysRaces(); 138 | verifyNoMoreInteractions(mockService); 139 | } 140 | 141 | /** 142 | * Test - GetHorsesInRace - Uses Caching 143 | */ 144 | @Test 145 | public void testWithCacheHits() { 146 | 147 | HystrixRequestContext context = HystrixRequestContext.initializeContext(); 148 | 149 | try { 150 | CommandGetHorsesInRaceWithCaching commandFirst = new CommandGetHorsesInRaceWithCaching(mockService, RACE_1); 151 | CommandGetHorsesInRaceWithCaching commandSecond = new CommandGetHorsesInRaceWithCaching(mockService, RACE_1); 152 | 153 | commandFirst.execute(); 154 | // this is the first time we've executed this command with 155 | // the value of "2" so it should not be from cache 156 | assertFalse(commandFirst.isResponseFromCache()); 157 | 158 | verify(mockService).getHorsesInRace(RACE_1); 159 | verifyNoMoreInteractions(mockService); 160 | 161 | commandSecond.execute(); 162 | // this is the second time we've executed this command with 163 | // the same value so it should return from cache 164 | assertTrue(commandSecond.isResponseFromCache()); 165 | 166 | } finally { 167 | context.shutdown(); 168 | } 169 | 170 | // start a new request context 171 | context = HystrixRequestContext.initializeContext(); 172 | try { 173 | CommandGetHorsesInRaceWithCaching commandThree = new CommandGetHorsesInRaceWithCaching(mockService, RACE_1); 174 | commandThree.execute(); 175 | // this is a new request context so this 176 | // should not come from cache 177 | assertFalse(commandThree.isResponseFromCache()); 178 | 179 | // Flush the cache 180 | HystrixRequestCache.getInstance(GETTER_KEY, HystrixConcurrencyStrategyDefault.getInstance()).clear(RACE_1); 181 | 182 | } finally { 183 | context.shutdown(); 184 | } 185 | } 186 | 187 | private List getRaceCourses(){ 188 | RaceCourse course1 = new RaceCourse(RACE_1, "France"); 189 | return Arrays.asList(course1); 190 | } 191 | 192 | private List getHorsesAtFrance(){ 193 | Horse horse1 = new Horse(HORSE_1, "White"); 194 | Horse horse2 = new Horse(HORSE_2, "Black"); 195 | return Arrays.asList(horse1, horse2); 196 | } 197 | } 198 | -------------------------------------------------------------------------------- /lab02/README.md: -------------------------------------------------------------------------------- 1 | 实验二、Hystrix+Dashboard实验 2 | ====== 3 | 4 | ### 实验步骤 5 | 6 | #### 1. 克隆Netflix Hystrix和Hystrix-Dashboard项目到本地 7 | 8 | ``` 9 | https://github.com/Netflix/Hystrix.git 10 | https://github.com/Netflix-Skunkworks/hystrix-dashboard 11 | ``` 12 | 13 | **注意**:hystrix-dashboard缺失一个`gradle-wrapper.jar`文件,可以将Hystrix中的`wrapper`文件夹复制并覆盖hystrix-dashboard下的`wrapper`文件夹。 14 | 15 | 16 | #### 2. 运行hystrix-example-webapp应用 17 | 18 | ``` 19 | cd hystrix-examples-webapp 20 | ../gradlew appRun 21 | ``` 22 | 23 | 打开`http://localhost:8989/hystrix-examples-webapp`校验 24 | 25 | #### 3. 运行hystrix-dashboard应用 26 | 27 | ``` 28 | cd hystrix-dashboard 29 | ./gradlew appRun 30 | ``` 31 | 32 | 打开`http://localhost:7979/hystrix-dashboard`校验 33 | 34 | 添加`http://localhost:8989/hystrix-examples-webapp/hystrix.stream`,并启动hystrix流监控 35 | 36 | ##### 4. 运行curl.sh脚本触发hystrix流 37 | 38 | ```sheel 39 | while true; 40 | do curl "http://localhost:8989/hystrix-examples-webapp/"; 41 | done 42 | ``` 43 | 44 | ##### 5. 观察hystrix-dashboard实时流 45 | 46 | 47 | -------------------------------------------------------------------------------- /lab02/curl.sh: -------------------------------------------------------------------------------- 1 | while true; 2 | do curl "http://localhost:8989/hystrix-examples-webapp/"; 3 | done -------------------------------------------------------------------------------- /lab03/README.md: -------------------------------------------------------------------------------- 1 | 实验三、Spring Cloud Hystrix实验 2 | ====== 3 | 4 | ### 实验步骤 5 | 6 | #### 1. 将student和school微服务项目导入Eclipse IDE 7 | 8 | 9 | **注意**:school项目依赖 10 | 11 | ```xml 12 | 13 | org.springframework.boot 14 | spring-boot-starter-web 15 | 16 | 17 | org.springframework.cloud 18 | spring-cloud-starter-hystrix 19 | 20 | 21 | org.springframework.cloud 22 | spring-cloud-starter-hystrix-dashboard 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter-actuator 27 | 28 | ``` 29 | 30 | 31 | #### 2. 运行student-springboot微服务项目 32 | 33 | #### 3. 运行school-springboot微服务项目 34 | 35 | 通过postman调用school微服务 36 | 37 | ``` 38 | http://localhost:8088/getSchoolDetails/abcschool 39 | ``` 40 | 41 | 修改`io.spring2go.hystrixlab.schoolservice.delegate.StudentServiceDelegate`中`StudentServiceDelegate`方法上的标注,依次实验各种Hystrix Command标注用法: 42 | 43 | 1. 简单用法 44 | ```java 45 | @HystrixCommand 46 | ``` 47 | 48 | 2. 定制超时 49 | ```java 50 | @HystrixCommand(commandProperties = { 51 | @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "4000") }) 52 | ``` 53 | 54 | 3. 定制降级方法 55 | ```java 56 | @HystrixCommand(fallbackMethod = "callStudentService_Fallback") 57 | ``` 58 | 59 | 4. 定制线程池隔离策略 60 | ```java 61 | @HystrixCommand(fallbackMethod = "callStudentService_Fallback", 62 | threadPoolKey = "studentServiceThreadPool", 63 | threadPoolProperties = { 64 | @HystrixProperty(name="coreSize", value="30"), 65 | @HystrixProperty(name="maxQueueSize", value="10") 66 | } 67 | ) 68 | ``` 69 | 70 | 5. 查看hystrix stream和dashboard 71 | 72 | hystrix stream: 73 | ``` 74 | http://localhost:8088/hystrix.stream 75 | ``` 76 | 77 | hystrix dashboard 78 | ``` 79 | http://localhost:8088/hystrix 80 | ``` 81 | 82 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /lab03/school-service/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | .sts4-cache 12 | 13 | ### IntelliJ IDEA ### 14 | .idea 15 | *.iws 16 | *.iml 17 | *.ipr 18 | 19 | ### NetBeans ### 20 | /nbproject/private/ 21 | /build/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ -------------------------------------------------------------------------------- /lab03/school-service/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spring2go/hystrix_lab/bed9bbe8ed4102b1b997d06332dadf7dc6800d01/lab03/school-service/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /lab03/school-service/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.3/apache-maven-3.5.3-bin.zip 2 | -------------------------------------------------------------------------------- /lab03/school-service/mvnw: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ---------------------------------------------------------------------------- 3 | # Licensed to the Apache Software Foundation (ASF) under one 4 | # or more contributor license agreements. See the NOTICE file 5 | # distributed with this work for additional information 6 | # regarding copyright ownership. The ASF licenses this file 7 | # to you under the Apache License, Version 2.0 (the 8 | # "License"); you may not use this file except in compliance 9 | # with the License. You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, 14 | # software distributed under the License is distributed on an 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | # KIND, either express or implied. See the License for the 17 | # specific language governing permissions and limitations 18 | # under the License. 19 | # ---------------------------------------------------------------------------- 20 | 21 | # ---------------------------------------------------------------------------- 22 | # Maven2 Start Up Batch script 23 | # 24 | # Required ENV vars: 25 | # ------------------ 26 | # JAVA_HOME - location of a JDK home dir 27 | # 28 | # Optional ENV vars 29 | # ----------------- 30 | # M2_HOME - location of maven2's installed home dir 31 | # MAVEN_OPTS - parameters passed to the Java VM when running Maven 32 | # e.g. to debug Maven itself, use 33 | # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 34 | # MAVEN_SKIP_RC - flag to disable loading of mavenrc files 35 | # ---------------------------------------------------------------------------- 36 | 37 | if [ -z "$MAVEN_SKIP_RC" ] ; then 38 | 39 | if [ -f /etc/mavenrc ] ; then 40 | . /etc/mavenrc 41 | fi 42 | 43 | if [ -f "$HOME/.mavenrc" ] ; then 44 | . "$HOME/.mavenrc" 45 | fi 46 | 47 | fi 48 | 49 | # OS specific support. $var _must_ be set to either true or false. 50 | cygwin=false; 51 | darwin=false; 52 | mingw=false 53 | case "`uname`" in 54 | CYGWIN*) cygwin=true ;; 55 | MINGW*) mingw=true;; 56 | Darwin*) darwin=true 57 | # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home 58 | # See https://developer.apple.com/library/mac/qa/qa1170/_index.html 59 | if [ -z "$JAVA_HOME" ]; then 60 | if [ -x "/usr/libexec/java_home" ]; then 61 | export JAVA_HOME="`/usr/libexec/java_home`" 62 | else 63 | export JAVA_HOME="/Library/Java/Home" 64 | fi 65 | fi 66 | ;; 67 | esac 68 | 69 | if [ -z "$JAVA_HOME" ] ; then 70 | if [ -r /etc/gentoo-release ] ; then 71 | JAVA_HOME=`java-config --jre-home` 72 | fi 73 | fi 74 | 75 | if [ -z "$M2_HOME" ] ; then 76 | ## resolve links - $0 may be a link to maven's home 77 | PRG="$0" 78 | 79 | # need this for relative symlinks 80 | while [ -h "$PRG" ] ; do 81 | ls=`ls -ld "$PRG"` 82 | link=`expr "$ls" : '.*-> \(.*\)$'` 83 | if expr "$link" : '/.*' > /dev/null; then 84 | PRG="$link" 85 | else 86 | PRG="`dirname "$PRG"`/$link" 87 | fi 88 | done 89 | 90 | saveddir=`pwd` 91 | 92 | M2_HOME=`dirname "$PRG"`/.. 93 | 94 | # make it fully qualified 95 | M2_HOME=`cd "$M2_HOME" && pwd` 96 | 97 | cd "$saveddir" 98 | # echo Using m2 at $M2_HOME 99 | fi 100 | 101 | # For Cygwin, ensure paths are in UNIX format before anything is touched 102 | if $cygwin ; then 103 | [ -n "$M2_HOME" ] && 104 | M2_HOME=`cygpath --unix "$M2_HOME"` 105 | [ -n "$JAVA_HOME" ] && 106 | JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 107 | [ -n "$CLASSPATH" ] && 108 | CLASSPATH=`cygpath --path --unix "$CLASSPATH"` 109 | fi 110 | 111 | # For Migwn, ensure paths are in UNIX format before anything is touched 112 | if $mingw ; then 113 | [ -n "$M2_HOME" ] && 114 | M2_HOME="`(cd "$M2_HOME"; pwd)`" 115 | [ -n "$JAVA_HOME" ] && 116 | JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" 117 | # TODO classpath? 118 | fi 119 | 120 | if [ -z "$JAVA_HOME" ]; then 121 | javaExecutable="`which javac`" 122 | if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then 123 | # readlink(1) is not available as standard on Solaris 10. 124 | readLink=`which readlink` 125 | if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then 126 | if $darwin ; then 127 | javaHome="`dirname \"$javaExecutable\"`" 128 | javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" 129 | else 130 | javaExecutable="`readlink -f \"$javaExecutable\"`" 131 | fi 132 | javaHome="`dirname \"$javaExecutable\"`" 133 | javaHome=`expr "$javaHome" : '\(.*\)/bin'` 134 | JAVA_HOME="$javaHome" 135 | export JAVA_HOME 136 | fi 137 | fi 138 | fi 139 | 140 | if [ -z "$JAVACMD" ] ; then 141 | if [ -n "$JAVA_HOME" ] ; then 142 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 143 | # IBM's JDK on AIX uses strange locations for the executables 144 | JAVACMD="$JAVA_HOME/jre/sh/java" 145 | else 146 | JAVACMD="$JAVA_HOME/bin/java" 147 | fi 148 | else 149 | JAVACMD="`which java`" 150 | fi 151 | fi 152 | 153 | if [ ! -x "$JAVACMD" ] ; then 154 | echo "Error: JAVA_HOME is not defined correctly." >&2 155 | echo " We cannot execute $JAVACMD" >&2 156 | exit 1 157 | fi 158 | 159 | if [ -z "$JAVA_HOME" ] ; then 160 | echo "Warning: JAVA_HOME environment variable is not set." 161 | fi 162 | 163 | CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher 164 | 165 | # traverses directory structure from process work directory to filesystem root 166 | # first directory with .mvn subdirectory is considered project base directory 167 | find_maven_basedir() { 168 | 169 | if [ -z "$1" ] 170 | then 171 | echo "Path not specified to find_maven_basedir" 172 | return 1 173 | fi 174 | 175 | basedir="$1" 176 | wdir="$1" 177 | while [ "$wdir" != '/' ] ; do 178 | if [ -d "$wdir"/.mvn ] ; then 179 | basedir=$wdir 180 | break 181 | fi 182 | # workaround for JBEAP-8937 (on Solaris 10/Sparc) 183 | if [ -d "${wdir}" ]; then 184 | wdir=`cd "$wdir/.."; pwd` 185 | fi 186 | # end of workaround 187 | done 188 | echo "${basedir}" 189 | } 190 | 191 | # concatenates all lines of a file 192 | concat_lines() { 193 | if [ -f "$1" ]; then 194 | echo "$(tr -s '\n' ' ' < "$1")" 195 | fi 196 | } 197 | 198 | BASE_DIR=`find_maven_basedir "$(pwd)"` 199 | if [ -z "$BASE_DIR" ]; then 200 | exit 1; 201 | fi 202 | 203 | export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} 204 | echo $MAVEN_PROJECTBASEDIR 205 | MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" 206 | 207 | # For Cygwin, switch paths to Windows format before running java 208 | if $cygwin; then 209 | [ -n "$M2_HOME" ] && 210 | M2_HOME=`cygpath --path --windows "$M2_HOME"` 211 | [ -n "$JAVA_HOME" ] && 212 | JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` 213 | [ -n "$CLASSPATH" ] && 214 | CLASSPATH=`cygpath --path --windows "$CLASSPATH"` 215 | [ -n "$MAVEN_PROJECTBASEDIR" ] && 216 | MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` 217 | fi 218 | 219 | WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 220 | 221 | exec "$JAVACMD" \ 222 | $MAVEN_OPTS \ 223 | -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ 224 | "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ 225 | ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" 226 | -------------------------------------------------------------------------------- /lab03/school-service/mvnw.cmd: -------------------------------------------------------------------------------- 1 | @REM ---------------------------------------------------------------------------- 2 | @REM Licensed to the Apache Software Foundation (ASF) under one 3 | @REM or more contributor license agreements. See the NOTICE file 4 | @REM distributed with this work for additional information 5 | @REM regarding copyright ownership. The ASF licenses this file 6 | @REM to you under the Apache License, Version 2.0 (the 7 | @REM "License"); you may not use this file except in compliance 8 | @REM with the License. You may obtain a copy of the License at 9 | @REM 10 | @REM http://www.apache.org/licenses/LICENSE-2.0 11 | @REM 12 | @REM Unless required by applicable law or agreed to in writing, 13 | @REM software distributed under the License is distributed on an 14 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | @REM KIND, either express or implied. See the License for the 16 | @REM specific language governing permissions and limitations 17 | @REM under the License. 18 | @REM ---------------------------------------------------------------------------- 19 | 20 | @REM ---------------------------------------------------------------------------- 21 | @REM Maven2 Start Up Batch script 22 | @REM 23 | @REM Required ENV vars: 24 | @REM JAVA_HOME - location of a JDK home dir 25 | @REM 26 | @REM Optional ENV vars 27 | @REM M2_HOME - location of maven2's installed home dir 28 | @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands 29 | @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending 30 | @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven 31 | @REM e.g. to debug Maven itself, use 32 | @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 33 | @REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files 34 | @REM ---------------------------------------------------------------------------- 35 | 36 | @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' 37 | @echo off 38 | @REM enable echoing my setting MAVEN_BATCH_ECHO to 'on' 39 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% 40 | 41 | @REM set %HOME% to equivalent of $HOME 42 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") 43 | 44 | @REM Execute a user defined script before this one 45 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre 46 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending 47 | if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" 48 | if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" 49 | :skipRcPre 50 | 51 | @setlocal 52 | 53 | set ERROR_CODE=0 54 | 55 | @REM To isolate internal variables from possible post scripts, we use another setlocal 56 | @setlocal 57 | 58 | @REM ==== START VALIDATION ==== 59 | if not "%JAVA_HOME%" == "" goto OkJHome 60 | 61 | echo. 62 | echo Error: JAVA_HOME not found in your environment. >&2 63 | echo Please set the JAVA_HOME variable in your environment to match the >&2 64 | echo location of your Java installation. >&2 65 | echo. 66 | goto error 67 | 68 | :OkJHome 69 | if exist "%JAVA_HOME%\bin\java.exe" goto init 70 | 71 | echo. 72 | echo Error: JAVA_HOME is set to an invalid directory. >&2 73 | echo JAVA_HOME = "%JAVA_HOME%" >&2 74 | echo Please set the JAVA_HOME variable in your environment to match the >&2 75 | echo location of your Java installation. >&2 76 | echo. 77 | goto error 78 | 79 | @REM ==== END VALIDATION ==== 80 | 81 | :init 82 | 83 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn". 84 | @REM Fallback to current working directory if not found. 85 | 86 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% 87 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir 88 | 89 | set EXEC_DIR=%CD% 90 | set WDIR=%EXEC_DIR% 91 | :findBaseDir 92 | IF EXIST "%WDIR%"\.mvn goto baseDirFound 93 | cd .. 94 | IF "%WDIR%"=="%CD%" goto baseDirNotFound 95 | set WDIR=%CD% 96 | goto findBaseDir 97 | 98 | :baseDirFound 99 | set MAVEN_PROJECTBASEDIR=%WDIR% 100 | cd "%EXEC_DIR%" 101 | goto endDetectBaseDir 102 | 103 | :baseDirNotFound 104 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR% 105 | cd "%EXEC_DIR%" 106 | 107 | :endDetectBaseDir 108 | 109 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig 110 | 111 | @setlocal EnableExtensions EnableDelayedExpansion 112 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a 113 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% 114 | 115 | :endReadAdditionalConfig 116 | 117 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" 118 | 119 | set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" 120 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 121 | 122 | %MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* 123 | if ERRORLEVEL 1 goto error 124 | goto end 125 | 126 | :error 127 | set ERROR_CODE=1 128 | 129 | :end 130 | @endlocal & set ERROR_CODE=%ERROR_CODE% 131 | 132 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost 133 | @REM check for post script, once with legacy .bat ending and once with .cmd ending 134 | if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" 135 | if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" 136 | :skipRcPost 137 | 138 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' 139 | if "%MAVEN_BATCH_PAUSE%" == "on" pause 140 | 141 | if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% 142 | 143 | exit /B %ERROR_CODE% 144 | -------------------------------------------------------------------------------- /lab03/school-service/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | io.spring2go.hystrixlab 7 | school-service 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | school-service 12 | Demo project for Spring Boot 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 1.5.15.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 1.8 25 | Edgware.SR4 26 | 27 | 28 | 29 | 30 | org.springframework.boot 31 | spring-boot-starter-web 32 | 33 | 34 | org.springframework.cloud 35 | spring-cloud-starter-hystrix 36 | 37 | 38 | org.springframework.cloud 39 | spring-cloud-starter-hystrix-dashboard 40 | 41 | 42 | org.springframework.boot 43 | spring-boot-starter-actuator 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-starter-test 49 | test 50 | 51 | 52 | 53 | 54 | 55 | 56 | org.springframework.cloud 57 | spring-cloud-dependencies 58 | ${spring-cloud.version} 59 | pom 60 | import 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | org.springframework.boot 69 | spring-boot-maven-plugin 70 | 71 | 72 | 73 | 74 | 75 | 76 | -------------------------------------------------------------------------------- /lab03/school-service/src/main/java/META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Class-Path: 3 | 4 | -------------------------------------------------------------------------------- /lab03/school-service/src/main/java/io/spring2go/hystrixlab/schoolservice/SchoolServiceApplication.java: -------------------------------------------------------------------------------- 1 | package io.spring2go.hystrixlab.schoolservice; 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.netflix.hystrix.dashboard.EnableHystrixDashboard; 7 | 8 | @SpringBootApplication 9 | @EnableHystrixDashboard 10 | @EnableCircuitBreaker 11 | public class SchoolServiceApplication { 12 | 13 | public static void main(String[] args) { 14 | SpringApplication.run(SchoolServiceApplication.class, args); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /lab03/school-service/src/main/java/io/spring2go/hystrixlab/schoolservice/controller/SchoolServiceController.java: -------------------------------------------------------------------------------- 1 | package io.spring2go.hystrixlab.schoolservice.controller; 2 | 3 | 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.web.bind.annotation.PathVariable; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RequestMethod; 8 | import org.springframework.web.bind.annotation.RestController; 9 | 10 | import io.spring2go.hystrixlab.schoolservice.delegate.StudentServiceDelegate; 11 | 12 | @RestController 13 | public class SchoolServiceController { 14 | 15 | @Autowired 16 | StudentServiceDelegate studentServiceDelegate; 17 | 18 | @RequestMapping(value = "/getSchoolDetails/{schoolName}", method = RequestMethod.GET) 19 | public String getSchoolDetails(@PathVariable String schoolName) { 20 | System.out.println("Going to call student service to get data!"); 21 | return studentServiceDelegate.callStudentService(schoolName); 22 | } 23 | } -------------------------------------------------------------------------------- /lab03/school-service/src/main/java/io/spring2go/hystrixlab/schoolservice/delegate/StudentServiceDelegate.java: -------------------------------------------------------------------------------- 1 | package io.spring2go.hystrixlab.schoolservice.delegate; 2 | 3 | import java.util.Date; 4 | 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.context.annotation.Bean; 7 | import org.springframework.core.ParameterizedTypeReference; 8 | import org.springframework.http.HttpMethod; 9 | import org.springframework.stereotype.Service; 10 | import org.springframework.web.client.RestTemplate; 11 | 12 | import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; 13 | import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty; 14 | 15 | @Service 16 | public class StudentServiceDelegate { 17 | 18 | @Autowired 19 | RestTemplate restTemplate; 20 | 21 | @HystrixCommand 22 | // @HystrixCommand(commandProperties = { 23 | // @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "4000") }) 24 | // @HystrixCommand(fallbackMethod = "callStudentService_Fallback") 25 | // @HystrixCommand(fallbackMethod = "callStudentService_Fallback", 26 | // threadPoolKey = "studentServiceThreadPool", 27 | // threadPoolProperties = { 28 | // @HystrixProperty(name="coreSize", value="30"), 29 | // @HystrixProperty(name="maxQueueSize", value="10") 30 | // } 31 | // ) 32 | public String callStudentService(String schoolName) { 33 | 34 | System.out.println("Getting School details for " + schoolName); 35 | 36 | String response = restTemplate.exchange("http://localhost:8090/getStudentBySchool/{schoolName}", HttpMethod.GET, 37 | null, new ParameterizedTypeReference() { 38 | 39 | }, schoolName).getBody(); 40 | 41 | System.out.println("Response Received as " + response + " - " + new Date()); 42 | 43 | return "NORMAL FLOW !!! - School Name - " + schoolName + " ::: " + " Student Details " + response + " - " 44 | + new Date(); 45 | } 46 | 47 | @SuppressWarnings("unused") 48 | private String callStudentService_Fallback(String schoolName) { 49 | 50 | System.out.println("Student Service is down!!! fallback route enabled..."); 51 | 52 | return "CIRCUIT BREAKER ENABLED!!! NO Response From Student Service ath this moment. " 53 | + " Service will be back shortly - " + new Date(); 54 | } 55 | 56 | @Bean 57 | public RestTemplate restTemplate() { 58 | return new RestTemplate(); 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /lab03/school-service/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port = 8088 -------------------------------------------------------------------------------- /lab03/school-service/src/test/java/io/spring2go/hystrixlab/schoolservice/SchoolServiceApplicationTests.java: -------------------------------------------------------------------------------- 1 | package io.spring2go.hystrixlab.schoolservice; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class SchoolServiceApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /lab03/student-service/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | .sts4-cache 12 | 13 | ### IntelliJ IDEA ### 14 | .idea 15 | *.iws 16 | *.iml 17 | *.ipr 18 | 19 | ### NetBeans ### 20 | /nbproject/private/ 21 | /build/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ -------------------------------------------------------------------------------- /lab03/student-service/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spring2go/hystrix_lab/bed9bbe8ed4102b1b997d06332dadf7dc6800d01/lab03/student-service/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /lab03/student-service/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.3/apache-maven-3.5.3-bin.zip 2 | -------------------------------------------------------------------------------- /lab03/student-service/mvnw: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ---------------------------------------------------------------------------- 3 | # Licensed to the Apache Software Foundation (ASF) under one 4 | # or more contributor license agreements. See the NOTICE file 5 | # distributed with this work for additional information 6 | # regarding copyright ownership. The ASF licenses this file 7 | # to you under the Apache License, Version 2.0 (the 8 | # "License"); you may not use this file except in compliance 9 | # with the License. You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, 14 | # software distributed under the License is distributed on an 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | # KIND, either express or implied. See the License for the 17 | # specific language governing permissions and limitations 18 | # under the License. 19 | # ---------------------------------------------------------------------------- 20 | 21 | # ---------------------------------------------------------------------------- 22 | # Maven2 Start Up Batch script 23 | # 24 | # Required ENV vars: 25 | # ------------------ 26 | # JAVA_HOME - location of a JDK home dir 27 | # 28 | # Optional ENV vars 29 | # ----------------- 30 | # M2_HOME - location of maven2's installed home dir 31 | # MAVEN_OPTS - parameters passed to the Java VM when running Maven 32 | # e.g. to debug Maven itself, use 33 | # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 34 | # MAVEN_SKIP_RC - flag to disable loading of mavenrc files 35 | # ---------------------------------------------------------------------------- 36 | 37 | if [ -z "$MAVEN_SKIP_RC" ] ; then 38 | 39 | if [ -f /etc/mavenrc ] ; then 40 | . /etc/mavenrc 41 | fi 42 | 43 | if [ -f "$HOME/.mavenrc" ] ; then 44 | . "$HOME/.mavenrc" 45 | fi 46 | 47 | fi 48 | 49 | # OS specific support. $var _must_ be set to either true or false. 50 | cygwin=false; 51 | darwin=false; 52 | mingw=false 53 | case "`uname`" in 54 | CYGWIN*) cygwin=true ;; 55 | MINGW*) mingw=true;; 56 | Darwin*) darwin=true 57 | # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home 58 | # See https://developer.apple.com/library/mac/qa/qa1170/_index.html 59 | if [ -z "$JAVA_HOME" ]; then 60 | if [ -x "/usr/libexec/java_home" ]; then 61 | export JAVA_HOME="`/usr/libexec/java_home`" 62 | else 63 | export JAVA_HOME="/Library/Java/Home" 64 | fi 65 | fi 66 | ;; 67 | esac 68 | 69 | if [ -z "$JAVA_HOME" ] ; then 70 | if [ -r /etc/gentoo-release ] ; then 71 | JAVA_HOME=`java-config --jre-home` 72 | fi 73 | fi 74 | 75 | if [ -z "$M2_HOME" ] ; then 76 | ## resolve links - $0 may be a link to maven's home 77 | PRG="$0" 78 | 79 | # need this for relative symlinks 80 | while [ -h "$PRG" ] ; do 81 | ls=`ls -ld "$PRG"` 82 | link=`expr "$ls" : '.*-> \(.*\)$'` 83 | if expr "$link" : '/.*' > /dev/null; then 84 | PRG="$link" 85 | else 86 | PRG="`dirname "$PRG"`/$link" 87 | fi 88 | done 89 | 90 | saveddir=`pwd` 91 | 92 | M2_HOME=`dirname "$PRG"`/.. 93 | 94 | # make it fully qualified 95 | M2_HOME=`cd "$M2_HOME" && pwd` 96 | 97 | cd "$saveddir" 98 | # echo Using m2 at $M2_HOME 99 | fi 100 | 101 | # For Cygwin, ensure paths are in UNIX format before anything is touched 102 | if $cygwin ; then 103 | [ -n "$M2_HOME" ] && 104 | M2_HOME=`cygpath --unix "$M2_HOME"` 105 | [ -n "$JAVA_HOME" ] && 106 | JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 107 | [ -n "$CLASSPATH" ] && 108 | CLASSPATH=`cygpath --path --unix "$CLASSPATH"` 109 | fi 110 | 111 | # For Migwn, ensure paths are in UNIX format before anything is touched 112 | if $mingw ; then 113 | [ -n "$M2_HOME" ] && 114 | M2_HOME="`(cd "$M2_HOME"; pwd)`" 115 | [ -n "$JAVA_HOME" ] && 116 | JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" 117 | # TODO classpath? 118 | fi 119 | 120 | if [ -z "$JAVA_HOME" ]; then 121 | javaExecutable="`which javac`" 122 | if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then 123 | # readlink(1) is not available as standard on Solaris 10. 124 | readLink=`which readlink` 125 | if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then 126 | if $darwin ; then 127 | javaHome="`dirname \"$javaExecutable\"`" 128 | javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" 129 | else 130 | javaExecutable="`readlink -f \"$javaExecutable\"`" 131 | fi 132 | javaHome="`dirname \"$javaExecutable\"`" 133 | javaHome=`expr "$javaHome" : '\(.*\)/bin'` 134 | JAVA_HOME="$javaHome" 135 | export JAVA_HOME 136 | fi 137 | fi 138 | fi 139 | 140 | if [ -z "$JAVACMD" ] ; then 141 | if [ -n "$JAVA_HOME" ] ; then 142 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 143 | # IBM's JDK on AIX uses strange locations for the executables 144 | JAVACMD="$JAVA_HOME/jre/sh/java" 145 | else 146 | JAVACMD="$JAVA_HOME/bin/java" 147 | fi 148 | else 149 | JAVACMD="`which java`" 150 | fi 151 | fi 152 | 153 | if [ ! -x "$JAVACMD" ] ; then 154 | echo "Error: JAVA_HOME is not defined correctly." >&2 155 | echo " We cannot execute $JAVACMD" >&2 156 | exit 1 157 | fi 158 | 159 | if [ -z "$JAVA_HOME" ] ; then 160 | echo "Warning: JAVA_HOME environment variable is not set." 161 | fi 162 | 163 | CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher 164 | 165 | # traverses directory structure from process work directory to filesystem root 166 | # first directory with .mvn subdirectory is considered project base directory 167 | find_maven_basedir() { 168 | 169 | if [ -z "$1" ] 170 | then 171 | echo "Path not specified to find_maven_basedir" 172 | return 1 173 | fi 174 | 175 | basedir="$1" 176 | wdir="$1" 177 | while [ "$wdir" != '/' ] ; do 178 | if [ -d "$wdir"/.mvn ] ; then 179 | basedir=$wdir 180 | break 181 | fi 182 | # workaround for JBEAP-8937 (on Solaris 10/Sparc) 183 | if [ -d "${wdir}" ]; then 184 | wdir=`cd "$wdir/.."; pwd` 185 | fi 186 | # end of workaround 187 | done 188 | echo "${basedir}" 189 | } 190 | 191 | # concatenates all lines of a file 192 | concat_lines() { 193 | if [ -f "$1" ]; then 194 | echo "$(tr -s '\n' ' ' < "$1")" 195 | fi 196 | } 197 | 198 | BASE_DIR=`find_maven_basedir "$(pwd)"` 199 | if [ -z "$BASE_DIR" ]; then 200 | exit 1; 201 | fi 202 | 203 | export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} 204 | echo $MAVEN_PROJECTBASEDIR 205 | MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" 206 | 207 | # For Cygwin, switch paths to Windows format before running java 208 | if $cygwin; then 209 | [ -n "$M2_HOME" ] && 210 | M2_HOME=`cygpath --path --windows "$M2_HOME"` 211 | [ -n "$JAVA_HOME" ] && 212 | JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` 213 | [ -n "$CLASSPATH" ] && 214 | CLASSPATH=`cygpath --path --windows "$CLASSPATH"` 215 | [ -n "$MAVEN_PROJECTBASEDIR" ] && 216 | MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` 217 | fi 218 | 219 | WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 220 | 221 | exec "$JAVACMD" \ 222 | $MAVEN_OPTS \ 223 | -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ 224 | "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ 225 | ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" 226 | -------------------------------------------------------------------------------- /lab03/student-service/mvnw.cmd: -------------------------------------------------------------------------------- 1 | @REM ---------------------------------------------------------------------------- 2 | @REM Licensed to the Apache Software Foundation (ASF) under one 3 | @REM or more contributor license agreements. See the NOTICE file 4 | @REM distributed with this work for additional information 5 | @REM regarding copyright ownership. The ASF licenses this file 6 | @REM to you under the Apache License, Version 2.0 (the 7 | @REM "License"); you may not use this file except in compliance 8 | @REM with the License. You may obtain a copy of the License at 9 | @REM 10 | @REM http://www.apache.org/licenses/LICENSE-2.0 11 | @REM 12 | @REM Unless required by applicable law or agreed to in writing, 13 | @REM software distributed under the License is distributed on an 14 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | @REM KIND, either express or implied. See the License for the 16 | @REM specific language governing permissions and limitations 17 | @REM under the License. 18 | @REM ---------------------------------------------------------------------------- 19 | 20 | @REM ---------------------------------------------------------------------------- 21 | @REM Maven2 Start Up Batch script 22 | @REM 23 | @REM Required ENV vars: 24 | @REM JAVA_HOME - location of a JDK home dir 25 | @REM 26 | @REM Optional ENV vars 27 | @REM M2_HOME - location of maven2's installed home dir 28 | @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands 29 | @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending 30 | @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven 31 | @REM e.g. to debug Maven itself, use 32 | @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 33 | @REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files 34 | @REM ---------------------------------------------------------------------------- 35 | 36 | @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' 37 | @echo off 38 | @REM enable echoing my setting MAVEN_BATCH_ECHO to 'on' 39 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% 40 | 41 | @REM set %HOME% to equivalent of $HOME 42 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") 43 | 44 | @REM Execute a user defined script before this one 45 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre 46 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending 47 | if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" 48 | if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" 49 | :skipRcPre 50 | 51 | @setlocal 52 | 53 | set ERROR_CODE=0 54 | 55 | @REM To isolate internal variables from possible post scripts, we use another setlocal 56 | @setlocal 57 | 58 | @REM ==== START VALIDATION ==== 59 | if not "%JAVA_HOME%" == "" goto OkJHome 60 | 61 | echo. 62 | echo Error: JAVA_HOME not found in your environment. >&2 63 | echo Please set the JAVA_HOME variable in your environment to match the >&2 64 | echo location of your Java installation. >&2 65 | echo. 66 | goto error 67 | 68 | :OkJHome 69 | if exist "%JAVA_HOME%\bin\java.exe" goto init 70 | 71 | echo. 72 | echo Error: JAVA_HOME is set to an invalid directory. >&2 73 | echo JAVA_HOME = "%JAVA_HOME%" >&2 74 | echo Please set the JAVA_HOME variable in your environment to match the >&2 75 | echo location of your Java installation. >&2 76 | echo. 77 | goto error 78 | 79 | @REM ==== END VALIDATION ==== 80 | 81 | :init 82 | 83 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn". 84 | @REM Fallback to current working directory if not found. 85 | 86 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% 87 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir 88 | 89 | set EXEC_DIR=%CD% 90 | set WDIR=%EXEC_DIR% 91 | :findBaseDir 92 | IF EXIST "%WDIR%"\.mvn goto baseDirFound 93 | cd .. 94 | IF "%WDIR%"=="%CD%" goto baseDirNotFound 95 | set WDIR=%CD% 96 | goto findBaseDir 97 | 98 | :baseDirFound 99 | set MAVEN_PROJECTBASEDIR=%WDIR% 100 | cd "%EXEC_DIR%" 101 | goto endDetectBaseDir 102 | 103 | :baseDirNotFound 104 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR% 105 | cd "%EXEC_DIR%" 106 | 107 | :endDetectBaseDir 108 | 109 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig 110 | 111 | @setlocal EnableExtensions EnableDelayedExpansion 112 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a 113 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% 114 | 115 | :endReadAdditionalConfig 116 | 117 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" 118 | 119 | set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" 120 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 121 | 122 | %MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* 123 | if ERRORLEVEL 1 goto error 124 | goto end 125 | 126 | :error 127 | set ERROR_CODE=1 128 | 129 | :end 130 | @endlocal & set ERROR_CODE=%ERROR_CODE% 131 | 132 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost 133 | @REM check for post script, once with legacy .bat ending and once with .cmd ending 134 | if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" 135 | if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" 136 | :skipRcPost 137 | 138 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' 139 | if "%MAVEN_BATCH_PAUSE%" == "on" pause 140 | 141 | if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% 142 | 143 | exit /B %ERROR_CODE% 144 | -------------------------------------------------------------------------------- /lab03/student-service/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | io.spring2go.hystrixlab 7 | student-service 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | student-service 12 | Demo project for Spring Boot 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 1.5.15.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 1.8 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-web 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-test 36 | test 37 | 38 | 39 | 40 | 41 | 42 | 43 | org.springframework.boot 44 | spring-boot-maven-plugin 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /lab03/student-service/src/main/java/META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Class-Path: 3 | 4 | -------------------------------------------------------------------------------- /lab03/student-service/src/main/java/io/spring2go/hystrixlab/studentservice/StudentServiceApplication.java: -------------------------------------------------------------------------------- 1 | package io.spring2go.hystrixlab.studentservice; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class StudentServiceApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(StudentServiceApplication.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /lab03/student-service/src/main/java/io/spring2go/hystrixlab/studentservice/controller/StudentServiceController.java: -------------------------------------------------------------------------------- 1 | package io.spring2go.hystrixlab.studentservice.controller; 2 | 3 | import java.util.ArrayList; 4 | import java.util.HashMap; 5 | import java.util.List; 6 | import java.util.Map; 7 | import java.util.Random; 8 | 9 | import org.springframework.web.bind.annotation.PathVariable; 10 | import org.springframework.web.bind.annotation.RequestMapping; 11 | import org.springframework.web.bind.annotation.RequestMethod; 12 | import org.springframework.web.bind.annotation.RestController; 13 | 14 | import io.spring2go.hystrixlab.studentservice.domain.Student; 15 | 16 | @RestController 17 | public class StudentServiceController { 18 | 19 | private static Map> schoolDB = new HashMap>(); 20 | 21 | static { 22 | schoolDB = new HashMap>(); 23 | 24 | List students = new ArrayList(); 25 | Student student = new Student("BoBo", "Class I"); 26 | students.add(student); 27 | student = new Student("Jack", "Class II"); 28 | students.add(student); 29 | 30 | schoolDB.put("abcschool", students); 31 | 32 | students = new ArrayList(); 33 | student = new Student("Daniel", "Class III"); 34 | students.add(student); 35 | student = new Student("William", "Class V"); 36 | students.add(student); 37 | 38 | schoolDB.put("xyzschool", students); 39 | } 40 | 41 | @RequestMapping(value = "/getStudentBySchool/{schoolName}", method = RequestMethod.GET) 42 | public List getStudents(@PathVariable String schoolName) { 43 | System.out.println("Getting student for " + schoolName); 44 | 45 | randomlyRunLong(); 46 | 47 | List studentList = schoolDB.get(schoolName); 48 | if (studentList == null) { 49 | studentList = new ArrayList(); 50 | Student student = new Student("Not Found", "N/A"); 51 | studentList.add(student); 52 | } 53 | 54 | return studentList; 55 | } 56 | 57 | private void randomlyRunLong() { 58 | Random rand = new Random(); 59 | 60 | int randomNum = rand.nextInt(3) + 1; 61 | 62 | if (randomNum == 3) sleep(); 63 | } 64 | 65 | private void sleep() { 66 | try { 67 | Thread.sleep(3000); 68 | } catch (InterruptedException e) { 69 | e.printStackTrace(); 70 | } 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /lab03/student-service/src/main/java/io/spring2go/hystrixlab/studentservice/domain/Student.java: -------------------------------------------------------------------------------- 1 | package io.spring2go.hystrixlab.studentservice.domain; 2 | 3 | public class Student { 4 | 5 | private String name; 6 | private String className; 7 | 8 | public Student(String name, String className) { 9 | this.name = name; 10 | this.className = className; 11 | } 12 | 13 | public String getName() { 14 | return name; 15 | } 16 | public void setName(String name) { 17 | this.name = name; 18 | } 19 | public String getClassName() { 20 | return className; 21 | } 22 | public void setClassName(String className) { 23 | this.className = className; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /lab03/student-service/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port = 8090 -------------------------------------------------------------------------------- /lab03/student-service/src/test/java/io/spring2go/hystrixlab/studentservice/StudentServiceApplicationTests.java: -------------------------------------------------------------------------------- 1 | package io.spring2go.hystrixlab.studentservice; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class StudentServiceApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /ppt/微服务容错限流Hystrix架构和实践.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/spring2go/hystrix_lab/bed9bbe8ed4102b1b997d06332dadf7dc6800d01/ppt/微服务容错限流Hystrix架构和实践.pdf --------------------------------------------------------------------------------