跳至主要內容

SpringCloud.H-基础

晨光-向大约 3 分钟JavaSpringJavaSpringSpringCloud

SpringCloud.H基础

笔记日期:2020.5.20

1. SpringBoot2.X版和SpringCloud H版

版本选择 2.2.2.RELEASE + Hoxton.SR1+ Meaven3.5及以上+MySQL5.7及以上+Java8

版本选择 2.2.5.RELEASE + Hoxton.SR3

SpringBoot2.1.4版和SpringCloud G版

1. SpringBoot 2.0 之后

官网建议 2.0之后

2. SpringCloud


2.关于Cloud组件的停更/升级/替换

1. 停更前

20210223154503
20210223154503

2. 注册中心

1. eureka

停更

2. zookeeper

可以替换

3. consul

可以替换(GO语言写的)

4. Nacos (阿里)


3. 服务的调用

1. rabbon

还在使用

2. LoadBalancer

新的服务调用


4. 服务的调用2

1. feign

挂了

2. OpenFeign


5. 降级服务

1. Hystrix

官网不用

2. resilience4j

国外推荐

3. sentienl(阿里)

推荐


6. 服务网关

1. zuul

挂了 (zuul死了)

2. gateway

推荐


7. 服务配置

1. Config

不用

2. Nocos


8. 服务总线

1. Bus

不用

2. Nacos


3. 微服务架构编码

1. 订单-支付模块微服务

https://blog.csdn.net/qq_41211642/article/details/104772140open in new window

参考此博客


2. actuator微服务信息完善

https://blog.csdn.net/qq_41211642/article/details/104802731open in new window

1. 主机名称:服务名称修改

1. 当前问题:

服务注册含有主机名称,要想按照规范的要求,只暴露服务名,不要出现主机名

2. 配置

依赖标配

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

application.yml eureka中添加

  # 主机名的修改
  instance:
    instance-id: order80
3. 效果

2. IP提示

1. 配置
# 访问路径可以显示IP地址
prefer-ip-address: true
2. 效果

3. 服务发现Discovery

对于注册进入eureka里面的服务,可以通过服务发现来获取该服务信息

https://blog.csdn.net/qq_41211642/article/details/104803913open in new window

1. 使用

1. 启动类
@EnableDiscoveryClient
2. 代码
@Resource
private DiscoveryClient discoveryClient;

 @GetMapping(value = "/discovery")
    public Object discovery() {
        // 1. getServices 获取服务信息
        List<String> services = discoveryClient.getServices();
        services.forEach(s -> {
            log.info("服务信息: " + s);
        });

        // 2. serviceId: eureka对外暴露的服务实例, 如CLOUD-PAYMENT-SERVICE, CLOUD-ORDER-SERVER
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        instances.forEach(i -> {
            log.info(i.getServiceId() + "\t", i.getPort() + "\t" + i.getUri());
        });
        return this.discoveryClient;
    }

4. Eureka自我保护理论知识

https://blog.csdn.net/qq_41211642/article/details/104804049open in new window

1. 概述:

保护模式主要用于一组客户端和Eureka Server之间存在网络分区场景下的保护,一旦进入保护模式,Eureka Server将会尝试保护其服务注册表中的信息,不再删除服务注册表中的数据,也就是不会注销任何微服务。
一句话: 某时刻某一个微服务不可用了,Eureka不会立刻清理,依旧会对该微服务的信息进行保存。

如果在Eureka Server的首页看到以下这段提示,则说明Eureka进入了保护模式。属于CAP里面的AP分支。

2. 解决

1. eureka服务端
2. 客户端

但客户端挂了后,eureka直接删除

宕机