使用 Spring Cloud Alibaba Nacos 实现服务注册与发现

什么是 Nacos

Nacos 是 Spring Cloud Alibaba 生态中的重要组件之一,用于发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,可以快速实现 动态服务发现服务配置服务元数据及流量管理

下载安装

# 下载源码
git clone https://github.com/alibaba/nacos.git

# 安装到本地仓库
cd nacos/
mvn -Prelease-nacos clean install -U

启动服务

cd distribution/target/nacos-server-0.7.0/nacos/bin

# Linux
./startup.sh -m standalone

# Windows
startup.cmd

启动完成后,访问 http://192.168.127.132:8848/nacos

登录

账号:nacos

密码:nacos

Nacos 控制台

服务提供者

引入依赖

pom.xml 中主要添加依赖 spring-cloud-starter-alibaba-nacos-discovery

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

相关配置

application.yml 中主要配置 Nacos 的地址

spring:
  application:
    # 服务名
    name: service-provider
  cloud:
    nacos:
      discovery:
        # 服务注册中心
        server-addr: 192.168.127.132:8848

Application 入口类中主要添加注解 @EnableDiscoveryClient 开启服务注册发现功能

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class ProviderApplication {
    
    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
    }
}

Controller

创建一个 RestController 提供 RestFul API 用于测试提供服务

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class EchoController {

    @GetMapping(value = "/echo/{keyword}")
    public  String echo(@PathVariable(value = "keyword") String keyword) {
        return "Hello Nacos Provider " + keyword + ";
    }
}

验证是否成功

启动项目,浏览器访问 http://192.168.127.132:8848/nacos

提供者服务已上线

会发现有一个服务已经注册在 Nacos 中,服务名为 service-provider

此时访问网址 http://localhost:8848/echo/hi,浏览器会响应以下内容:

Hello Nacos Provider hi

服务消费者

引入依赖

pom.xml 中主要添加 spring-cloud-starter-alibaba-nacos-discoveryspring-cloud-starter-openfeign 依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

相关配置

Application 入口类中同样添加注解 @EnableDiscoveryClient 开启服务注册与发现功能,添加注解 @EnableFeignClients 开启 Feign 客户端

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }
}

Service

创建业务层接口,通过 @FeignClient("服务名") 注解来指定具体调用哪个服务

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient(value = "service-provider")
public interface EchoService {

    @GetMapping(value = "/echo/{keyword}")
    String echo(@PathVariable(value = "keyword") String keyword);

}

Controller

创建 Controller 调用服务

import com.funtl.spring.cloud.alibaba.consumer.service.EchoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestEchoController {

    @Autowired
    private EchoService echoService;

    @GetMapping(value = "/feign/echo/{keyword}")
    public String echoFeign(@PathVariable(value = "keyword") String keyword) {
        return echoService.echo(keyword);
    }
}

验证是否成功

启动项目,访问网址 http://localhost:8080/feign/echo/hi

会发现有一个名为 service-consumer 的服务已经注册到 Nacos 中

此时再访问 http://localhost:8080/echo/hi,浏览器会同样响应以下内容:

Hello Nacos Provider hi