跳转至

Nacos#

约 240 个字 25 行代码 5 张图片 预计阅读时间 6 分钟

安装#

Windows安装#

  • 官方下载二进制包:
官方下载nacos二进制包
  • 解压到相应目录,进入bin目录,执行目录启动服务:
Bash
startup.cmd -m standalone

浏览器通过 http://127.0.0.1:8848/nacos 地址可以访问控制台。

带数据库配置#

  • 在mysql数据库中创建一个名为 nacos 的数据库。
  • 在nacos安装包conf目录下找到mysql-schema.sql并执行。
sql脚本
sql脚本会生成以下表
  • 打开conf/application.properties配置数据库链接参数:
修改数据库链接配置

启动服务:

Bash
startup.cmd -m standalone

入门案例#

先确认使用的Spring Cloud Alibaba的版本,再去官网找到对应Spring CloudSpring Boot的版本:

根据Spring Cloud Alibaba版本,找到Spring Cloud和Spring Boot的版本

通过<dependencyManagement>进行版本管理:

XML
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>2023.0.3</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>2023.0.1.2</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

引入spring-cloud-starter-alibaba-nacos-discovery启动器,完整的pom.xml如下:

pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.luguosong</groupId>
    <artifactId>nacos-consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>nacos-consumer</name>
    <description>nacos-consumer</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>2023.0.3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2023.0.1.2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.16</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

服务生产者配置文件:

application.yml
spring:
  application:
    name: nacos-producer
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
server:
  port: 8081

服务生产者编写测试服务接口:

DemoController.java
package com.luguosong.nacosproducer.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author luguosong
 */
@RestController
public class DemoController {
    @GetMapping("/demo")
    public String demo() {
        return "nacos:服务调用成功";
    }
}

服务消费者编写测试接口:

TestController.java
package com.luguosong.nacosconsumer.controller;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.nacos.api.exception.NacosException;
import lombok.RequiredArgsConstructor;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author luguosong
 */
@RestController
@RequiredArgsConstructor
public class TestController {

    //通过构造方法进行依赖注入
    private final DiscoveryClient discoveryClient;

    @GetMapping("/hello")
    public String helloNacos() throws NacosException {
        // 根据名字获取nacos中的实例列表
        List<ServiceInstance> instances = discoveryClient.getInstances("nacos-producer");
        // 随机获取一个实例
        ServiceInstance instance = instances.get(RandomUtil.randomInt(instances.size()));

        // 通过RestTemplate发送请求
        ResponseEntity<String> response = new RestTemplate().exchange(instance.getUri() + "/demo",
                HttpMethod.GET,
                null,
                String.class);

        return response.getBody();
    }
}

评论