java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Nacos注册中调用方式

Nacos注册中心的几种调用方式详解

作者:Java中文社群

Spring Cloud Alibaba Nacos 作为近几年最热门的注册中心和配置中心,也被国内无数公司所使用,本文就来看下 Nacos 作为注册中心时,调用它的接口有几种方式

1.什么是注册中心?

注册中心(Registry)是一种用于服务发现和服务注册的分布式系统组件。它是在微服务架构中起关键作用的一部分,用于管理和维护服务实例的信息以及它们的状态。

它的执行流程如下图所示:

注册中心充当了服务之间的中介和协调者,它的主要功能有以下这些:

使用注册中心有以下优势和好处:

常见的注册中心包括 ZooKeeper、Eureka、Nacos 等。这些注册中心可以作为微服务架构中的核心组件,用于实现服务的自动发现、负载均衡和动态扩容等功能。

2.方法概述

当 Nacos 中注册了 Restful 接口时(一种软件架构风格,它是基于标准的 HTTP 协议和 URI 的一组约束和原则),其调用方式主要有以下两种:

使用 OpenFeign + Spring Cloud LoadBalancer

3.RestTemplate+LoadBalancer调用

此方案的实现有以下 3 个关键步骤:

具体实现如下。

3.1 添加依赖

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

3.2 设置配置文件

spring:
  application:
    name: nacos-discovery-business
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
        username: nacos
        password: nacos
        register-enabled: false

3.3 编写调用代码

此步骤又分为以下两步:

使用 RestTemplate 调用接口

3.3.1 RestTemplate添加LoadBalanced

在 Spring Boot 启动类上添加“@EnableDiscoveryClient”注解,并使用“@LoadBalanced”注解替换 IoC 容器中的 RestTemplate,具体实现代码如下:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
public class BusinessApplication {
 @LoadBalanced
 @Bean
 public RestTemplate restTemplate() {
     return new RestTemplate();
 }
 public static void main(String[] args) {
     SpringApplication.run(BusinessApplication.class, args);
 }
}

3.3.2 使用RestTemplate

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@RequestMapping("/business")
public class BusinessController2 {
 @Autowired
 private RestTemplate restTemplate;
 @RequestMapping("/getnamebyid")
 public String getNameById(Integer id){
     return restTemplate.getForObject("http://nacos-discovery-demo/user/getnamebyid?id="+id,
             String.class);
 }
}

4.OpenFeign+LoadBalancer调用

此步骤又分为以下 5 步:

具体实现如下。

4.1 添加依赖

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

4.2 设置配置文件

spring:
  application:
    name: nacos-discovery-business
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
        username: nacos
        password: nacos
        register-enabled: false

4.3 开启OpenFeign

在 Spring Boot 启动类上添加 @EnableFeignClients 注解。

4.4 编写Service

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Service
@FeignClient(name = "nacos-producer") // name 为生产者的服务名
public interface UserService {
    @RequestMapping("/user/getinfo") // 调用生产者的接口
    String getInfo(@RequestParam String name);
}

4.5 调用Service

import com.example.consumer.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class OrderController {
    @Autowired
    private UserService userService;
    @RequestMapping("/order")
    public String getOrder(@RequestParam String name){
        return userService.getInfo(name);
    }
}

5.获取本文源码

因平台不能上传附件,所以想要获取本文完整源码,请联系我:gg_stone,备注:Nacos 源码,不然不予通过。

6.版本说明

本文案例基于以下版本:

7.小结

注册中心作为微服务中不可或缺的重要组件,在微服务中充当着中介和协调者的作用。而 Nacos 作为近几年来,国内最热门的注册中心,其 Restf 接口调用有两种方式:RestTemplate + LoadBalancer 和 OpenFeign + LoadBalancer,开发者可以根据自己的实际需求,选择相应的调用方式。

以上就是Nacos注册中心有几种调用方式?的详细内容,更多关于Nacos注册中调用方式的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文