跟着老侯玩编程 跟着老侯玩编程
首页
  • 基础语法
  • 网络编程
  • 设计模式
  • 基础篇
  • 进阶篇
  • 框架篇
  • Redis
  • Alibaba
  • 课程目录
  • 码农宝典
留言
首页
  • 基础语法
  • 网络编程
  • 设计模式
  • 基础篇
  • 进阶篇
  • 框架篇
  • Redis
  • Alibaba
  • 课程目录
  • 码农宝典
留言
  • Redis

    • 基础环境
    • Redis详解
    • 快速列表
    • 链表
    • 跳跃表
    • 压缩列表
    • Redis配置文件详解
    • Ruby环境
  • SpringCloudAlibaba

    • 微服务-注册中心
      • Nacos-Discovery注册中心
        • 主流的注册中心
        • Nacos注册中心架构
        • NacosServer部署(单机)
        • NacosServer部署(集群)
        • 搭建NacosClient服务
    • 微服务-负载均衡
    • 微服务-微服务调用
    • 微服务-Sentinel
    • 微服务-配置中心
    • 微服务-网关
  • 分布式
  • SpringCloudAlibaba
舞动的代码
2022-08-30
目录

微服务-注册中心

官方文档:https://spring-cloud-alibaba-group.github.io/github-pages/2021/zh-cn/index.html

阿里的SpringBoot脚手架:https://start.aliyun.com/bootstrap.html

nacos官方地址:https://nacos.io/zh-cn/index.html

版本说明:https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E

SpringCloudAlibaba以Spring Boot 2.4 为分界线,同时维护 2.2.x 和 2021.x 两个分支迭代

2021.x 分支

Spring Cloud Alibaba Version Spring Cloud Version Spring Boot Version
2021.0.1.0* Spring Cloud 2021.0.1 2.6.3
2021.1 Spring Cloud 2020.0.1 2.4.2

2.2.x 分支

Spring Cloud Alibaba Version Spring Cloud Version Spring Boot Version
2.2.8.RELEASE* Spring Cloud Hoxton.SR12 2.3.12.RELEASE
2.2.7.RELEASE Spring Cloud Hoxton.SR12 2.3.12.RELEASE
2.2.6.RELEASE Spring Cloud Hoxton.SR9 2.3.2.RELEASE
2.1.4.RELEASE Spring Cloud Greenwich.SR6 2.1.13.RELEASE
2.2.1.RELEASE Spring Cloud Hoxton.SR3 2.2.5.RELEASE
2.2.0.RELEASE Spring Cloud Hoxton.RELEASE 2.2.X.RELEASE
2.1.2.RELEASE Spring Cloud Greenwich 2.1.X.RELEASE
2.0.4.RELEASE(停止维护,建议升级) Spring Cloud Finchley 2.0.X.RELEASE

组件版本关系

Spring Cloud Alibaba Version Sentinel Version Nacos Version RocketMQ Version Dubbo Version Seata Version
2.2.8.RELEASE 1.8.4 2.1.0 4.9.3 ~ 1.5.1
2021.0.1.0 1.8.3 1.4.2 4.9.2 ~ 1.4.2
2.2.7.RELEASE 1.8.1 2.0.3 4.6.1 2.7.13 1.3.0
2.2.6.RELEASE 1.8.1 1.4.2 4.4.0 2.7.8 1.3.0
2021.1 or 2.2.5.RELEASE or 2.1.4.RELEASE or 2.0.4.RELEASE 1.8.0 1.4.1 4.4.0 2.7.8 1.3.0
2.2.3.RELEASE or 2.1.3.RELEASE or 2.0.3.RELEASE 1.8.0 1.3.3 4.4.0 2.7.8 1.3.0
2.2.1.RELEASE or 2.1.2.RELEASE or 2.0.2.RELEASE 1.7.1 1.2.1 4.4.0 2.7.6 1.2.0
2.2.0.RELEASE 1.7.1 1.1.4 4.4.0 2.7.4.1 1.0.0
2.1.1.RELEASE or 2.0.1.RELEASE or 1.5.1.RELEASE 1.7.0 1.1.4 4.4.0 2.7.3 0.9.0
2.1.0.RELEASE or 2.0.0.RELEASE or 1.5.0.RELEASE 1.6.3 1.1.1 4.4.0 2.7.3 0.7.1

# Nacos-Discovery注册中心

服务注册

NacosClient会通过发送REST请求的方式向NacosServer注册自己的服务,提供自身的元数据,比如ip地址、端口等信息。NacosServer接收到注册请求后,就会把这些元数据信息存储在一个双层的内存Map中。

服务心跳:

在服务注册后,NacosClient会维护一个定时心跳来持续通知NacosServer,说明服务一直处于可用状态,防止被剔除。默认5s发送一次心跳。

服务同步:

NacosServer集群之间会互相同步服务实例,用来保证服务信息的一致性。leader raft

服务发现:

服务消费者(NacosClient)在调用服务提供者的服务时,会发送一个REST请求给NacosServer,获取上面注册的服务清单,并且缓存在NacosClient本地,同时会在NacosClient本地开启一个定时任务定时拉取服务端最新的注册表信息更新到本地缓存

服务健康检查:

NacosServer会开启一个定时任务用来检查注册服务实例的健康情况,对于超过15s没有收到客户端心跳的实例会将它的healthy属性置为false(客户端服务发现时不会发现),如果某个实例超过30秒没有收到心跳,直接剔除该实例(被剔除的实例如果恢复发送心跳则会重新注册)

# 主流的注册中心

img.png

# Nacos注册中心架构

img.png

# NacosServer部署(单机)

下载地址:https://github.com/alibaba/Nacos/releases

官方文档:https://nacos.io/zh-cn/docs/deployment.html

为便于后面描述后面涉及到nacos安装路径的描述,统一使用变量NacosHome=D:\Software\nacos-server-1.4.2\bin

img.png

访问nocas的管理端:http://192.168.3.14:8848/nacos,默认的用户名密码是nocas/nocas

# NacosServer部署(集群)

官网文档:https://nacos.io/zh-cn/docs/cluster-mode-quick-start.html

img.png

环境要求:

  • 64 bit OS Linux/Unix/Mac,推荐使用Linux系统。
  • 64 bit JDK 1.8+;下载.配置。
  • Maven 3.2.x+;下载.配置。
  • 3个或3个以上Nacos节点才能构成集群。

安装JDK

  • 解压到指定的目录

tar -zxvf jdk-8u144-linux-x64.tar.gz -C /opt/apps/

  • 配置环境变量
[root@redis-1 software]# cd /opt/apps/jdk1.8.0_144/
[root@redis-1 jdk1.8.0_144]# 
[root@redis-1 jdk1.8.0_144]# pwd
/opt/apps/jdk1.8.0_144
[root@redis-1 jdk1.8.0_144]# vim /etc/profile

# 按下Shfit+g,然后按下 o,添加以下内容

export JAVA_HOME=/opt/apps/jdk1.8.0_144
export PATH=$PATH:$JAVA_HOME/bin

# 按下ESC键,输入:wq  保存退出

# 重新加载配置文件

source /etc/profile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  • 验证是否配置成功

[root@redis-1 jdk1.8.0_144]# java -version java version "1.8.0_144" Java(TM) SE Runtime Environment (build 1.8.0_144-b01) Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

安装Maven

  • 解压到指定的目录

tar -zxvf apache-maven-3.8.6-bin.tar.gz -C /opt/apps/

  • 配置环境变量
[root@redis-1 software]# cd /opt/apps/apache-maven-3.8.6/

[root@redis-1 apache-maven-3.8.6]# pwd
/opt/apps/apache-maven-3.8.6

[root@redis-1 apache-maven-3.8.6]# vim /etc/profile

# Shift+g跳转到最后一行,按下 o 添加以下内容
export M2_HOME=/opt/apps/apache-maven-3.8.6
export PATH=$PATH:$M2_HOME/bin

# 按下ESC键退出编辑模式,输入:wq保存退出

[root@redis-1 apache-maven-3.8.6]# source /etc/profile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  • 验证是否配置成功
[root@redis-1 apache-maven-3.8.6]# mvn -v
Apache Maven 3.8.6 (84538c9988a25aec085021c365c560670ad80f63)
Maven home: /opt/apps/apache-maven-3.8.6
Java version: 1.8.0_144, vendor: Oracle Corporation, runtime: /opt/apps/jdk1.8.0_144/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-1160.el7.x86_64", arch: "amd64", family: "unix"

1
2
3
4
5
6
7

单机搭建伪集群,

# 解压
[root@redis-1 software]# tar -zxvf nacos-server-1.4.2.tar.gz  -C /opt/apps/

# 复制nacos安装包,修改为nacos8849,nacos8850,nacos8851
[root@redis-1 software]# cd /opt/apps/
# 重命名
[root@redis-1 apps]# mv nacos/ nacos8849/
[root@redis-1 apps]# cp -r nacos8849/ nacos8850
[root@redis-1 apps]# cp -r nacos8849/ nacos8851
1
2
3
4
5
6
7
8
9

以nacos8849为例,进入nacos8849目录

配置集群配置文件

  • 在nacos的解压目录nacos/的conf目录下,有配置文件cluster.conf,请每行配置成ip:port。(请配置3个或3个以上节点)

[root@redis-1 conf]# cp cluster.conf.example  cluster.conf
[root@redis-1 conf]# vim cluster.conf
#it is ip
#example
192.168.236.130:8849
192.168.236.130:8850
192.168.236.130:8851
1
2
3
4
5
6
7
8
  • 修改application.properties的配置,使用外置数据源要使用mysql5.7+(包括)

#使用外置mysql数据源
spring.datasource.platform=mysql
#Count of DB:
db.num=1
#Connect URL of DB:
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user.0=root
db.password.0=root

1
2
3
4
5
6
7
8
9
10
  • 将conf\cluster.conf.example改为cluster.conf,添加节点配置

# ip:port
192.168.65.220:8849
192.168.65.220:8850
192.168.65.220:8851

1
2
3
4
5
6

nacos8850,nacos8851按同样的方式配置。

  • 创建mysql数据库,sql文件位置:conf\nacos-mysql.sql

  • 如果出现内存不足:修改启动脚本(bin\startup.sh)的jvm参数

JAVA_OPT="${JAVA_OPT} -server -Xms512m -Xmx512m -Xmn256 -XX:MetaspaceSize=64m -XX:MaxMetaspaceSize=128m"

img.png

  • 分别启动nacos8849,nacos8850,nacos8851,以nacos8849为例,进入nacos8849目录,启动nacos

  • 测试 登录 http://192.168.3.14:8849/nacos,用户名和密码都是nacos

Nginx反向代理

upstream nacoscluster {
    server 127.0.0.1:8849;
    server 127.0.0.1:8850;
    server 127.0.0.1:8851;
    }
    server{
      listen 8847;
      server_name localhost;
      location /nacos/{
      proxy_pass http://nacoscluster/nacos/;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12

# 搭建NacosClient服务

父Pom中支持spring cloud&spring cloud alibaba, 引入依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>spring-cloud-alibaba-hello</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <!--父模块-->
    <packaging>pom</packaging>
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <!--自定义属性(版本号)-->
        <alibaba.version>2021.0.1.0</alibaba.version>
        <boot.version>2.6.3</boot.version>
        <cloud.version>2021.0.1</cloud.version>
    </properties>
    <dependencyManagement>
            <dependencies>
                <!--父模块:主要用于管理alibaba组件的版本-->
                <dependency>
                    <groupId>com.alibaba.cloud</groupId>
                    <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                    <version>${alibaba.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
                <!--SpringBoot版本管理-->
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-parent</artifactId>
                    <version>${boot.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
                <!--SpringCloud版本-->
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>${cloud.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
    </dependencyManagement>
</project>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

当前项目pom中引入依赖

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
1
2
3
4

application.properties中配置

server.port=8002
#微服务名称
spring.application.name=service-user
#配置 Nacos server 的地址
spring.cloud.nacos.discovery.server-addr=localhost:8848
1
2
3
4
5

更多配置:https://github.com/alibaba/spring-cloud-alibaba/wiki/Nacos-discovery

img.png

使用RestTemplate进行服务调用,可以使用微服务名称 (spring.application.name)

注意:需要添加@LoadBalanced注解


@Configuration

public class BeanConfig {
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(RestTemplateBuilder builder) {

        return  builder.build();
    }
}

1
2
3
4
5
6
7
8
9
10
11
12

错误提示

2022-08-15 14:10:25.697 ERROR 17156 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://service-order/api/hello": service-order; nested exception is java.net.UnknownHostException: service-order] with root cause


1
2
3
Ruby环境
微服务-负载均衡

← Ruby环境 微服务-负载均衡→

Theme by Vdoing | Copyright © 2013-2023 冀ICP备16006233号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×