|
@@ -1,8 +1,9 @@
|
|
|
---
|
|
|
title: 微服务框架
|
|
|
date: 2021-08-07
|
|
|
-tag: springcloud
|
|
|
-abbrlink: springcloud
|
|
|
+tag: [springcloud,java]
|
|
|
+categories: java
|
|
|
+abbrlink: java/springcloud/1
|
|
|
---
|
|
|
|
|
|
# 微服务框架
|
|
@@ -13,7 +14,7 @@ abbrlink: springcloud
|
|
|
|
|
|
### 1.1 微服务架构
|
|
|
|
|
|
-> https://martinfowler.com/articles/microservices.html
|
|
|
+> <https://martinfowler.com/articles/microservices.html>
|
|
|
>
|
|
|
> 微服务架构提出者:马丁福勒
|
|
|
|
|
@@ -25,9 +26,9 @@ abbrlink: springcloud
|
|
|
>
|
|
|
> SpringCloud中的大多数技术都是基于Netflix公司的技术进行第二次开发。
|
|
|
>
|
|
|
-> 1、SpringCloud的中文社区网站:http://springcloud.cn/
|
|
|
+> 1、SpringCloud的中文社区网站:<http://springcloud.cn/>
|
|
|
>
|
|
|
-> 2、SpringCloud的中文网 :http://springcloud.cc/
|
|
|
+> 2、SpringCloud的中文网 :<http://springcloud.cc/>
|
|
|
>
|
|
|
> 八个技术点:
|
|
|
>
|
|
@@ -127,8 +128,6 @@ eureka:
|
|
|
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
|
|
|
```
|
|
|
|
|
|
-
|
|
|
-
|
|
|
#### 2.2.2 创建EurekaClient
|
|
|
|
|
|
> 1、创建Maven工程,修改为SpringBoot
|
|
@@ -332,17 +331,17 @@ eureka:
|
|
|
```yml
|
|
|
eureka:
|
|
|
client:
|
|
|
- #每隔多久去更新一下本地的注册表缓存信息
|
|
|
+ #每隔多久去更新一下本地的注册表缓存信息
|
|
|
registry-fetch-interval-seconds: 30
|
|
|
```
|
|
|
|
|
|
> 5、Eureka的自我保护机制,统计15分钟内,如果一个服务的心跳发送比例低于85%,EurekaServer就会开启自我保护机制
|
|
|
>
|
|
|
-> 1、不会从EurekaServer中去移除长时间没有收到心跳的服务
|
|
|
+> 1、不会从EurekaServer中去移除长时间没有收到心跳的服务
|
|
|
>
|
|
|
-> 2、EurekaServer还是可以正常提供服务的
|
|
|
+> 2、EurekaServer还是可以正常提供服务的
|
|
|
>
|
|
|
-> 3、网络稳定时,EurekaServer才会开始将自己的信息被其他节点同步过去
|
|
|
+> 3、网络稳定时,EurekaServer才会开始将自己的信息被其他节点同步过去
|
|
|
|
|
|
![image-20201111132523595](images/image-20201111132523595.png)
|
|
|
|
|
@@ -350,8 +349,8 @@ eureka:
|
|
|
eureka:
|
|
|
# Eureka保护机制配置
|
|
|
server:
|
|
|
- #true 开启 false关闭
|
|
|
- enable-self-preservation: true
|
|
|
+ #true 开启 false关闭
|
|
|
+ enable-self-preservation: true
|
|
|
```
|
|
|
|
|
|
> 6、CAP定理,C-一致性 A-可用性 P-分区容错性,这三个特新在分布是环境下,只能满足2个,而且分区容错性在分布式环境下,时必须要满足的。只能在AC之间进行权衡。
|
|
@@ -414,10 +413,10 @@ public String customer() {
|
|
|
|
|
|
> 1、负载均衡策略
|
|
|
>
|
|
|
-> 1. RandomRule:随机策略
|
|
|
-> 2. RoundRobbinRule:轮询策略
|
|
|
-> 3. WeightedResponseTimeRule:默认会采用轮询的策略,后续会根据服务的响应时间,自动给你分配权重
|
|
|
-> 4. BestAvailableRule:根据被调用方并发数最小的去分配
|
|
|
+> 1. RandomRule:随机策略
|
|
|
+> 2. RoundRobbinRule:轮询策略
|
|
|
+> 3. WeightedResponseTimeRule:默认会采用轮询的策略,后续会根据服务的响应时间,自动给你分配权重
|
|
|
+> 4. BestAvailableRule:根据被调用方并发数最小的去分配
|
|
|
|
|
|
> 2、采用注解的形式
|
|
|
|
|
@@ -487,7 +486,7 @@ public interface SearchClient {
|
|
|
private SearchClient searchClient;
|
|
|
|
|
|
@GetMapping("/customer")
|
|
|
-public String customer() {
|
|
|
+public String customer() {
|
|
|
String result = searchClient.search();
|
|
|
return result;
|
|
|
}
|
|
@@ -497,10 +496,10 @@ public String customer() {
|
|
|
|
|
|
> 1、注意事项
|
|
|
>
|
|
|
-> 1. 如果你传递的参数,比较复杂时,默认会采用POST的请求方式。
|
|
|
-> 2. 传递单个参数时,推荐使用@PathVariable,如果传递的单个参数比较多,这里也可以采用@RequestParam,不要省略value属性
|
|
|
-> 3. 传递对象信息时,统一采用json的方式,添加@RequestBody
|
|
|
-> 4. Client接口必须采用@RequestMapping
|
|
|
+> 1. 如果你传递的参数,比较复杂时,默认会采用POST的请求方式。
|
|
|
+> 2. 传递单个参数时,推荐使用@PathVariable,如果传递的单个参数比较多,这里也可以采用@RequestParam,不要省略value属性
|
|
|
+> 3. 传递对象信息时,统一采用json的方式,添加@RequestBody
|
|
|
+> 4. Client接口必须采用@RequestMapping
|
|
|
|
|
|
> 2、在Search模块下准备三个接口
|
|
|
|
|
@@ -515,7 +514,7 @@ public Customer getcustomer(@RequestParam Integer id, @RequestParam String name)
|
|
|
return new Customer(id, name, 23);
|
|
|
}
|
|
|
|
|
|
-@PostMapping("save")
|
|
|
+@PostMapping("save")
|
|
|
public Customer save(@RequestBody Customer customer) {
|
|
|
return customer;
|
|
|
}
|
|
@@ -697,15 +696,15 @@ public Customer findByIdFallBack(Integer id) {
|
|
|
|
|
|
> 1、Hystrix的线程池的配置(具体的配置属性需要去查看HystrixCommandProperties类) [wiki](https://github.com/Netflix/Hystrix/wiki/Configuration)
|
|
|
>
|
|
|
-> 1. 线程隔离策略:name = `hystrix.command.default.execution.isolation.strategy`,value=`THREAD`,`SEMAPHORE `
|
|
|
-> 2. 指定超时时间(针对线程池):name= `hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds` ,value =`1000`
|
|
|
-> 3. 是否开启超时时间配置:name=`hystrix.command.default.execution.timeout.enabled`,value=`true`
|
|
|
-> 4. 超时之后是否中断线程:name=`hystrix.command.default.execution.isolation.thread.interruptOnTimeout`,value=`true`
|
|
|
-> 5. 取消任务之后是否中断线程:name=`hystrix.command.default.execution.isolation.thread.interruptOnCancel`,value=false
|
|
|
+> 1. 线程隔离策略:name = `hystrix.command.default.execution.isolation.strategy`,value=`THREAD`,`SEMAPHORE`
|
|
|
+> 2. 指定超时时间(针对线程池):name= `hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds` ,value =`1000`
|
|
|
+> 3. 是否开启超时时间配置:name=`hystrix.command.default.execution.timeout.enabled`,value=`true`
|
|
|
+> 4. 超时之后是否中断线程:name=`hystrix.command.default.execution.isolation.thread.interruptOnTimeout`,value=`true`
|
|
|
+> 5. 取消任务之后是否中断线程:name=`hystrix.command.default.execution.isolation.thread.interruptOnCancel`,value=false
|
|
|
|
|
|
> 2、信号量配置信息
|
|
|
>
|
|
|
-> 1. 线程隔离策略:name = `hystrix.command.default.execution.isolation.strategy`,value=`THREAD`,`SEMAPHORE `
|
|
|
+> 1. 线程隔离策略:name = `hystrix.command.default.execution.isolation.strategy`,value=`THREAD`,`SEMAPHORE`
|
|
|
> 2. 指定信号量的最大并发请求数:name=`hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests`,value=`10`
|
|
|
|
|
|
### 5.4 断路器
|
|
@@ -763,7 +762,7 @@ hystrix:
|
|
|
|
|
|
> 6、测试
|
|
|
|
|
|
-直接访问: http://host:port/hystrix.stream
|
|
|
+直接访问: <http://host:port/hystrix.stream>
|
|
|
|
|
|
![1605103421256](images/1605103421256.png)
|
|
|
|
|
@@ -775,12 +774,12 @@ hystrix:
|
|
|
|
|
|
> 断路器的属性(10秒之内)
|
|
|
>
|
|
|
-> 1. 断路器的开关:name=` hystrix.command.default.circuitBreaker.enabled `,value=`true`
|
|
|
-> 2. 失败阈值的总请求数:name=` hystrix.command.default.circuitBreaker.requestVolumeThreshold `,value=`20`
|
|
|
-> 3. 请求总数失败率达到%多少时打开断路器:name=` hystrix.command.default.circuitBreaker.errorThresholdPercentage `,value=`50`
|
|
|
-> 4. 断路器open状态后,多少秒是拒绝请求的:name=` hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds `value=`5000`
|
|
|
-> 5. 强制让服务拒绝请求:name=` hystrix.command.default.circuitBreaker.forceOpen `,value=`false`
|
|
|
-> 6. 强制让服务接收请求:name=` hystrix.command.default.circuitBreaker.forceClosed `,value=`false`
|
|
|
+> 1. 断路器的开关:name=` hystrix.command.default.circuitBreaker.enabled `,value=`true`
|
|
|
+> 2. 失败阈值的总请求数:name=` hystrix.command.default.circuitBreaker.requestVolumeThreshold `,value=`20`
|
|
|
+> 3. 请求总数失败率达到%多少时打开断路器:name=` hystrix.command.default.circuitBreaker.errorThresholdPercentage `,value=`50`
|
|
|
+> 4. 断路器open状态后,多少秒是拒绝请求的:name=` hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds `value=`5000`
|
|
|
+> 5. 强制让服务拒绝请求:name=` hystrix.command.default.circuitBreaker.forceOpen `,value=`false`
|
|
|
+> 6. 强制让服务接收请求:name=` hystrix.command.default.circuitBreaker.forceClosed `,value=`false`
|
|
|
|
|
|
> 具体配置方式
|
|
|
|
|
@@ -834,9 +833,9 @@ public class CustomerService {
|
|
|
|
|
|
> 2、使用请求缓存的注解`@CacheResult` `CacheRemove`
|
|
|
>
|
|
|
-> 1. @CacheResult:帮助我们缓存当前方法的返回结果(必须配合@HystrixCommand使用)
|
|
|
-> 2. @CacheRemove:帮助我们清除某一个缓存信息(基于commandKey)
|
|
|
-> 3. @CacheKey:指定那个方法参数作为缓存标识
|
|
|
+> 1. @CacheResult:帮助我们缓存当前方法的返回结果(必须配合@HystrixCommand使用)
|
|
|
+> 2. @CacheRemove:帮助我们清除某一个缓存信息(基于commandKey)
|
|
|
+> 3. @CacheKey:指定那个方法参数作为缓存标识
|
|
|
|
|
|
> 3、修改Search模块的结果返回值
|
|
|
|
|
@@ -860,7 +859,7 @@ public class HystrixRequestContextFilter implements Filter {
|
|
|
> 5、修改Controller
|
|
|
|
|
|
```java
|
|
|
-@Autowired
|
|
|
+@Autowired
|
|
|
private CustomerService customerService;
|
|
|
|
|
|
@GetMapping("/customer/{id}")
|
|
@@ -884,7 +883,7 @@ private CustomerService customerService;
|
|
|
System.out.println(customerService.findById(id));
|
|
|
System.out.println(customerService.findById(id));
|
|
|
customerService.clearFindById(id);
|
|
|
-
|
|
|
+
|
|
|
return searchClient.findById(id);
|
|
|
}
|
|
|
```
|
|
@@ -962,8 +961,6 @@ spring:
|
|
|
|
|
|
![image-20201112113609523](images/image-20201112113609523.png)
|
|
|
|
|
|
-
|
|
|
-
|
|
|
### 6.3 Zuul常用配置信息
|
|
|
|
|
|
#### 6.3.1 Zuul的监控界面
|
|
@@ -1052,8 +1049,6 @@ spring:
|
|
|
name: CUSTOMER-${version}
|
|
|
```
|
|
|
|
|
|
-
|
|
|
-
|
|
|
> 3、修改Zuul的配置
|
|
|
|
|
|
```yml
|
|
@@ -1074,8 +1069,6 @@ zuul:
|
|
|
}
|
|
|
```
|
|
|
|
|
|
-
|
|
|
-
|
|
|
> 5、测试
|
|
|
|
|
|
![image-20201112135410803](images/image-20201112135410803.png)
|
|
@@ -1095,7 +1088,7 @@ zuul:
|
|
|
```java
|
|
|
@Component
|
|
|
public class ZuulFilterTest extends ZuulFilter {
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
|
|
|
```
|
|
@@ -1106,7 +1099,7 @@ public class ZuulFilterTest extends ZuulFilter {
|
|
|
@Override
|
|
|
public String filterType() {
|
|
|
return FilterConstants.PRE_TYPE;
|
|
|
-}
|
|
|
+}
|
|
|
```
|
|
|
|
|
|
> 3、指定过滤器的执行顺序
|
|
@@ -1209,7 +1202,7 @@ public class AuthenticationFilter extends ZuulFilter{
|
|
|
```java
|
|
|
@Component
|
|
|
public class ZuulFallBack implements FallbackProvider {
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
|
|
|
```
|
|
@@ -1280,7 +1273,7 @@ public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
|
|
|
```java
|
|
|
@Component
|
|
|
public class DynamicRoutingFilter extends ZuulFilter{
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
```
|
|
|
|
|
@@ -1603,13 +1596,13 @@ spring:
|
|
|
uri: https://gitee.com/zyjblog/config-resp.git
|
|
|
```
|
|
|
|
|
|
-> 5、测试(例子:`http://localhost:82/master/customer-xxx.yml ` (master可以省略))
|
|
|
+> 5、测试(例子:`http://localhost:82/master/customer-xxx.yml` (master可以省略))
|
|
|
>
|
|
|
> 访问方式如下:
|
|
|
>
|
|
|
> `/{application}/{profile}[/{label}]`
|
|
|
> `/{application}-{profile}.yml`
|
|
|
-> `/{label}/{application}-{profile}.yml`
|
|
|
+> `/{label}/{application}-{profile}.yml`
|
|
|
>
|
|
|
> `/{application}-{profile}.properties`
|
|
|
> `/{label}/{application}-{profile}.properties`
|
|
@@ -1729,11 +1722,11 @@ public class CustomerController {
|
|
|
> 4、测试
|
|
|
>
|
|
|
> 1. CONFIG在gitee修改之后,自动拉取最新的配置信息。
|
|
|
-> 2. 其他模块需要更新的话,手动发送一个POST请求:http://localhost:10000/actuator/bus-refresh ,不重启项目,即可获取最新的配置信息
|
|
|
+> 2. 其他模块需要更新的话,手动发送一个POST请求:<http://localhost:10000/actuator/bus-refresh> ,不重启项目,即可获取最新的配置信息
|
|
|
|
|
|
#### 9.4.4 内网穿透
|
|
|
|
|
|
-> 1、内网穿透官网:http://www.ngrok.cc/
|
|
|
+> 1、内网穿透官网:<http://www.ngrok.cc/>
|
|
|
|
|
|
> 2、注册登录
|
|
|
|
|
@@ -1879,7 +1872,7 @@ logging:
|
|
|
|
|
|
### 10.3 Zipkin的使用
|
|
|
|
|
|
-> 1、搭建Zipkin的web工程 https://zipkin.io/pages/quickstart
|
|
|
+> 1、搭建Zipkin的web工程 <https://zipkin.io/pages/quickstart>
|
|
|
>
|
|
|
> 1. docker安装Zipkin
|
|
|
|
|
@@ -1944,8 +1937,6 @@ spring:
|
|
|
type: rabbit
|
|
|
```
|
|
|
|
|
|
-
|
|
|
-
|
|
|
> 3、修改Zipkin信息
|
|
|
|
|
|
```yml
|
|
@@ -2003,9 +1994,9 @@ services:
|
|
|
docker run --name es1_6.6.0 \
|
|
|
-p 9200:9200 \
|
|
|
-p 9300:9300 \
|
|
|
--e ES_JAVA_OPTS="-Xms256m -Xmx256m" \
|
|
|
--v /d/elasticsearch/config/es1.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
|
|
|
--v /d/elasticsearch/data/es1:/usr/share/elasticsearch/data \
|
|
|
+-e ES_JAVA_OPTS="-Xms256m -Xmx256m" \
|
|
|
+-v /d/elasticsearch/config/es1.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
|
|
|
+-v /d/elasticsearch/data/es1:/usr/share/elasticsearch/data \
|
|
|
-v /d/elasticsearch/logs/es1:/usr/share/elasticsearch/logs \
|
|
|
-d 13aa43015aa1
|
|
|
```
|
|
@@ -2035,4 +2026,4 @@ docker-compose restart
|
|
|
|
|
|
## 十一、完整SpringCloud架构图
|
|
|
|
|
|
-![image-20201113165759501](images/image-20201113165759501.png)
|
|
|
+![image-20201113165759501](images/image-20201113165759501.png)
|