Spring Cloud之alibaba服务的搭建,nacos的基本使用,服务的注册

326 阅读1分钟

一、nacos安装

官网下载:点击下载nacos-server.2.2.0.zip

二、步骤

  1. 数据库中新建nacos数据库

  2. 运行nacos安装目录中的sql:mysql-schema.sql

  3. nacos注册

    在nacos安装路径中,bin文件夹旁边, conf文件夹中配置(配置数据库,配置数据库类型MySql)

nacos配置文件.png

  1. 启动nacos(使用微服务nacos服务必须启动)

    访问地址: http://127.0.0.1:8848/nacos/index.html,进行登录

  2. 进行idea搭建微服务项目

三、搭建微服务项目

1.建立一个maven项目,改项目作为父项目,只定义版本号,不进行其他操作

  • 安装相关依赖
<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>con.zking</groupId>
    <artifactId>shop-user</artifactId>
    <packaging>pom</packaging>
    <version>1.0</version>

    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.3.5.RELEASE</version>
    </parent>
    <dependencyManagement>
        <dependencies>
            <!--springCloudAlibaba-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.8.RELEASE</version>
                <!--引入一个父项目-->
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!--springCloud-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR9</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

        </dependencies>
    </dependencyManagement>

</project>

2.在父包项目中new 一个module,作为子模块

  • 建议new servse、new spi
  • serve包中写do、vo、dto包
  • api包提供给外部调用的接口

3.在server包中进行配置,application.yaml配置自己的端口信息、注册中心地址、项目名

server:
  port: 8012 #801开头表示集群
spring:
  application:
    name: shop-user # 项目名称 作为微服务明
  cloud:
    nacos:
      server-addr: 127.0.0.1:8848 # 注册中心地址

4.启动服务

  • 启动服务,登录nacos,查看服务是否被注册,这样基本服务框架创建成功

nacos查看服务状态.png

四、Springboot SpringCloud SpringCloudalibaba 的版本对应关系

img