为了账号安全,请及时绑定邮箱和手机立即绑定

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

标签:
Spring Cloud

什么是 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

《使用 Docker Compose 部署 Nacos》

Nacos 控制台

《使用 Docker Compose 部署 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 Discovery 实现服务注册与发现》提供者服务已上线

会发现有一个服务已经注册在 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;




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
点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
JAVA开发工程师
手记
粉丝
0
获赞与收藏
10

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消