肝了很久,冰河整理出這份4萬字的SpringCloud與SpringCloudAlibaba學習筆記!!
阿新 • • 發佈:2021-03-02
# 寫在前面
不少小夥伴讓我整理下有關SpringCloud和SpringCloudAlibaba的知識點,經過3天的收集和整理,冰河整理出這份4萬字的SpringCloud與SpringCloudAlibaba學習筆記!!
文章已收錄到:
[https://github.com/sunshinelyz/technology-binghe](https://github.com/sunshinelyz/technology-binghe)
[https://gitee.com/binghe001/technology-binghe](https://gitee.com/binghe001/technology-binghe)
# SpringCloud
## 服務註冊中心
### eureka
ap 高可用 分散式容錯
```yaml
eureka:
instance:
hostname: eureka7003.com #eureka服務端的例項名稱
instance-id: payment8001
prefer-ip-address: true
client:
register-with-eureka: false #false表示不向註冊中心註冊自己。
fetch-registry: false #false表示自己端就是註冊中心,我的職責就是維護服務例項,並不需要去檢索服務
service-url:
#叢集指向其它eureka
#defaultZone: http://eureka7002.com:7002/eureka/
#單機就是7001自己
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
#server:
#關閉自我保護機制,保證不可用服務被及時踢除
#enable-self-preservation: false
#eviction-interval-timer-in-ms: 2000
```
### Ribbon 啟用負載均衡
```java
@EnableEurekaServer
@EnableDiscoveryClient
@LoadBalanced
public RestTemplate getTemp() {
return new RestTemplate();
}
```
### zookepper
cp 強一致 分散式容錯
```xml
spring:
application:
name: cloud-zoo-consumer-order
cloud:
zookeeper:
connect-string: 192.168.10.58:2181
@SpringBootApplication
@EnableDiscoveryClient
```
### consul
cp 強一致 分散式容錯
```yaml
spring:
application:
name: consul-payment-provider
cloud:
consul:
host: 192.168.10.58
port: 8500
discovery:
service-name: ${spring.application.name}
@SpringBootApplication
@EnableDiscoveryClient
```
## 服務呼叫負載均衡
### Ribbon
### Ribbon 切換 負載規則
1. 在springboot 包掃描外層建立 配置
```java
@Configuration
public class Myrule {
@Bean
public IRule initRule() {
return new RandomRule();
}
}
```
1. 啟動類給指定服務載入隨機方法
```java
@RibbonClient(name = "CLOUD-PAYMENT-SERVICE", configuration = Myrule.class)
```
### OpenFeign
1. 新增maven依賴
```xml
```
1. 啟動類啟用Feign
```java
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
```
1. 新建介面 並註冊Feign資訊
```java
@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE") //提供方服務名
public interface Service {
@GetMapping(value = "/payment/get/{id}")