Linux nacos 集群 部署+idea2022 Spring cloud Alibaba 2022 OpenFeign

准备

安装mysql (192.168.1.101) 建库 nacos_config

 

集群部署方案

三台Linux服务器

nacos-server-01 192.168.1.136

nacos-server-02 192.168.1.137

nacos-server-03 192.168.1.138

 

 

 

下载nacos,

Releases · alibaba/nacos (github.com)

 

上传到Linux 服务器

解压缩

 

 修改解压后目录中的conf/application.properties

 

 

修改集群配置文件 cluster.conf.example  保存后改名 cluster.conf

 

 

启动nacos

 

 开机启动nacos集群

[root@machine136 nacos]#  vim /lib/systemd/system/nacos.service
[root@machine136 nacos]# systemctl enable nacos
Created symlink from /etc/systemd/system/multi-user.target.wants/nacos.service to /usr/lib/systemd/system/nacos.service.

nacos.service 内容:

注意:/opt/module 为nacos所在目录


[Unit]
Description=nacos
After=network.target

[Service]
Type=forking
ExecStart=/opt/module/nacos/bin/startup.sh
ExecReload=/opt/module/nacos/bin/shutdown.sh
ExecStop=/opt/module/nacos/bin/shutdown.sh
PrivateTmp=true

[Install]
WantedBy=multi-user.target
~

 

 查看日志是否成功

 

 

开发环境

IntelliJ IDEA 2022.2.4 

SpringCloud SpringBoot Alibaba版本如下

     <spring-boot.version>2.6.13</spring-boot.version>
        <spring-cloud.version>2021.0.5</spring-cloud.version>
        <spring-cloud-alibaba.version>2021.0.4.0</spring-cloud-alibaba.version>
        

 微服务

两个provider 服务

provider 8001 

provider 8002

一个消费服务  order 88

provider 主要文件

 pom

<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springclound</artifactId>
        <groupId>com.hztech</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>provider-payment8002</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>


    <dependencies>
        <!-- hystrix  需要cloud 的版本支持 见 父项目的POM
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        -->
        <!-- nacos注册中心 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <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>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
        </dependency>

        <!--mysql-connector-java-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--jdbc-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>

        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- 热部署-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.11</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.hztech</groupId>
            <artifactId>cloud-api-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

    </dependencies>


</project>

yam 注意 server-addr: 192.168.1.137:8848

server:
    port: 8002

spring:
  application:
      name: cloud-payment-service # 项目名,也是注册的名字

  datasource:
      type: com.alibaba.druid.pool.DruidDataSource  #当前数据源操作类型
      driver-class-name: org.gjt.mm.mysql.Driver    #mysql驱动包
      url: jdbc:mysql://localhost:3306/hztech?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
      username: root
      password: xxxxxx
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.1.137:8848
    inetutils:
      ignored-interfaces: 'VMware Virtual Ethernet Adapter for VMnet1,VMware Virtual Ethernet Adapter for VMnet8'

mybatis:
    mapper-locations: classpath:mapper/*.xml
    # 所有Entity 别名类所在包
    type-aliases-package: com.hztech.entities

druid:
  test-while-idle: true
  validation-query: SELECT 1

 

 

 

order 88

pom

<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springclound</artifactId>
        <groupId>com.hztech</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>customer-order80</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!-- hystrix
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        -->
        <!-- nacos注册中心 -->
        <!--
        由于SpringCloud Feign在Hoxton.M2 RELEASED版本之后不再使用Ribbon而是使用spring-cloud-loadbalancer,
        所以不引入spring-cloud-loadbalancer会报错 解决方法 加入spring-cloud-loadbalancer依赖 并且在nacos中排除ribbon依赖,不然loadbalancer无效
        -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-loadbalancer</artifactId>
        </dependency>
        <!-- openFeign          -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <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>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>

        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.hztech</groupId>
            <artifactId>cloud-api-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>


    </dependencies>
</project>

 

YAM  注意:server-addr: 192.168.1.136:8848

server:
  port: 88
spring:
  cloud:

    nacos:
      discovery:
        server-addr: 192.168.1.136:8848

    inetutils:
      ignored-interfaces: 'VMware Virtual Ethernet Adapter for VMnet1,VMware Virtual Ethernet Adapter for VMnet8'
  application:
    name: payment-order

 

OpenFeign 接口 完成微服务调用

@Service
@FeignClient(value = "cloud-payment-service")//,fallback = PaymentFeignServiceFallbackImpl.class


public interface PaymentFeignService {

    @PostMapping("/payment/create")
    public CommonResult<Payment> create(@RequestBody Payment payment)
    ;
    @GetMapping("/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable( "id")  Long id)
    ;
}

运行项目

 

 

 

 

登录控制台,查看效果

 两个provider 服务分别是对应一台 nacos-server ,order服务对应的server-addr是第三台服务器,

现在登录到任何一台都会看到3个完整的微服务,如下:

 我自测过 用docker nacos 搭建cluster 后,显示 运行成功,控制台 集群 节点列表中正常显示 ,但是服务列表却是各自独立的(因为每个微服务分别 指向了一个集群中不同的主机)!! 这点要注意 尽量不用docker nacos.

 

 

 停止一个机器后

/opt/module/nacos/bin/shutdown.sh

 

 服务仍然可用

 保留一个server 响应正常

 但是,如果现在某个nacos 服务停止了,那么对应以此为注册地址的微服务就无法正常工作了,比如:订单服务 注册的nacos 是192.168.1.136 ,把运行在136 上的nacos 服务 shutdown

那么再访问 order服务就出问题了

 

 微服务经过一段时间后也被下线了

 

 订单服务前台提示错误

 

 

后台也打印错误信息:

2023-02-20 16:20:44.511  INFO 15476 --- [t.remote.worker] com.alibaba.nacos.common.remote.client   : 
[c5c5c18d-809b-43dd-b192-83ba88c57bab] Fail to connect server, after trying 51 times, last try server is {serverIp = '192.168.1.136', server main port = 8848}, error = unknown

其他任何一个微服务都存在这个问题,为解决这个问题,要用搭建高可用的nginx集群来解决:

方案见:

高可用Nacos服务实战 - hztech - 博客园 (cnblogs.com)

 

 

 

posted on 2023-02-17 10:25  hztech  阅读(136)  评论(0编辑  收藏  举报

导航