SpringCloud學習筆記2
一、Ribbon實現客戶端的負載均衡【只支持2.3版本即以下、過渡性知識點】
Ribbon:一個服務集群多個、Ribbon來決定選擇調用哪一個(負載均衡)、使用了Feign就不用使用Ribbon了
1、創建項目選擇以下組件:SpringBoot DevTools、Eureka Discovery Client、Ribbon、Spring Web
2、添加配置信息
# 設置服務端口
server.port=8881
# 設置服務名 可以相同、相同則為后面的負載均衡準備
spring.application.name=hello-service
# 設置注冊中心地址
eureka.client.service-url.defaultZone=http://localhost:7777/eureka
3、在啟動類配置RestTemplate
//@EnableDiscoveryClient向服務中心注冊,并且注冊了一個叫restTemplate的bean。
@SpringBootApplication
@EnableDiscoveryClient
public class ServiceRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceRibbonApplication.class, args);
}
//@LoadBalanced注冊表明,這個restRemplate是需要做負載均衡的。
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
4、Controller使用RestTemplate調用其他服務實現負載均衡
@RestController
@RequestMapping("/classes")
public class ClassesController {
//restTemplate對象自帶負載均衡的調用客戶端對象
@Autowired RestTemplate restTemplate;
@RequestMapping("/index001")
public String index001() {
return "client002-classes-index001"+restTemplate.getForObject("http://service001/student/index001",String.class);
}
}
二、Hystrix斷路器(熔斷)
1、Hystrix介紹:有3個服務、當Feign在三個中選一個時、假如選得2號、但2號這時候恰好宕機了、此時就屬于斷路
是什么?hystrix對應的中文名字是“豪豬”,豪豬周身長滿了刺,能保護自己不受天敵的傷害,代表了一種防御機制,這與hystrix本身的功能不謀而合,因此Netflix團隊將該框架命名為Hystrix,并使用了對應的卡通形象做作為logo。
為什么要使用?在一個分布式系統里,許多依賴不可避免的會調用失敗,比如超時、異常等,如何能夠保證在一個依賴出問題的情況下,不會導致整體服務失敗,這個就是Hystrix需要做的事情。Hystrix提供了熔斷、隔離、Fallback、cache、監控等功能,能夠在一個、或多個依賴同時出現問題時保證系統依然可用。
2、添加依賴jar包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.4.0.RELEASE</version>
</dependency>
3、Controller配置斷熔
@RestController
@RequestMapping("/hystrix")
public class HystrixController {
//restTemplate對象自帶負載均衡的調用客戶端對象、或者直接使用Feign
@Autowired RestTemplate restTemplate;
@RequestMapping("/test")
//當請求的地址出現錯誤、執行error方法、如果test執行超過2秒或拋出異常皆會被斷路
@HystrixCommand(fallbackMethod = "error")
public String test(String name) throws Exception{
if (1==1)
throw new Exception("error");
return restTemplate.postForObject("http://server001/student/method002",null,String.class);
}
//異常也會被斷路
public String error(String name,Throwable e) {
return e.getMessage()+name;
}
}
4、啟動類添加斷熔注解
@SpringBootApplication
@EnableDiscoveryClient
//允許斷路器
@EnableCircuitBreaker
public class RibbonApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}
}
三、Zuul網關配置、新版本請使用gateway
1、作用:反向代理+負載均衡+限流
限流需要單獨添加jar包
2、添加依賴jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<!--限流的jar包、需要手動導入-->
<dependency>
<groupId>com.marcosbarbero.cloud</groupId>
<artifactId>spring-cloud-zuul-ratelimit</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
3、配置網關
#注冊中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:7777/eureka/
#服務端口
server.port=8888
#我在注冊中心的網關名稱
spring.application.name=zuul001
# 如:server001有個服務:http://localhost:8001/student/method001
# 配置下面網關后用戶訪問 http://localhost:8888/zuul/student/method001、和上面的路徑一摸一樣
# 網關配置 zuul.routes.任意名[亦可位服務名].path:、如果service-id相同則帶負載均衡1
zuul.routes.abc.path= /abc/**
zuul.routes.abc.service-id= CLIENT002
# 上面可簡寫zuul.routes.servere001.path=/aaa/** 第一個字不能是zuul
# 如果是zuul.routes.servere001= 等同于 zuul.routes.clie.path=/servere001/**
## 開啟限流、針對單個服務
zuul.ratelimit.enabled=true
# 60秒內請求超過3次、服務端就拋出異常、60s后可以恢復正常請求
zuul.ratelimit.policies.abc.limit=3
zuul.ratelimit.policies.abc.refresh-interval=60
## 針對某個IP進行限流、不影響其他IP
## zuul.ratelimit.policies.abc.type=ip地址
# 開啟全局限流
# zuul.ratelimit.enabled=true
# zuul.ratelimit.default-policy.limit=3
# zuul.ratelimit.default-policy.refresh-interval=60
# zuul.ratelimit.default-policy.type=origin
4、啟動類添加注解
@EnableZuulProxy
@EnableEurekaClient
@SpringBootApplication
public class SpringcloudzuulApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudzuulApplication.class, args);
}
}
浙公網安備 33010602011771號