SpringCloud使用Nacos的服务注册与发现

前言

在了解Nacos的服务注册与发现前,我们先看看Nacos官方的架构图:

nacos_arch.jpg我们现在开始参考 官方例子(nacos-spring-cloud-discovery-example),尝试将服务注册到Nacos。 

如果还没安装nacos的可以参考我上一篇文章https://blog.csdn.net/qq_36850813/article/details/102635786

server端

第一步:首先,我们新建一个项目,这里我是gradle,如果你用maven一样添加依赖:

gradle:

compile group: 'com.alibaba.cloud', name: 'spring-cloud-starter-alibaba-nacos-discovery', version: '2.1.0.RELEASE'

maven:

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    <version>2.1.0.RELEASE</version>
</dependency>

第二步:application.yml添加配置文件

server:
  port: 8082


spring:
  application:
    name: zoo-plus-nacos-server
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

第三步,在SpringBoot启动类,通过注解@EnableDiscoveryClient开启服务注册发现功能。如官方例子:

package com.zoo.nacos;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableDiscoveryClient
public class ServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServerApplication.class, args);
    }

    @RestController
    class EchoController {
        @RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
        public String echo(@PathVariable String string) {
            return "Hello Nacos Discovery " + string;
        }
    }


}

注意:使用Ribbon负载均衡的时候,服务名中不能使用下划线,不然会找不到服务。

第四步:然后,我们启动该服务,观察Nacos控制台,看到服务已注册。

访问http://localhost:8082/echo/hahah一切正常

客户端

第一步:服务端一样

第二步:application.yml添加配置文件

server:
  port: 8081

spring:
  application:
    name: zoo-plus-nacos-client
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

第三步,在SpringBoot启动类,通过注解@EnableDiscoveryClient开启服务注册发现功能。如官方例子:

package com.zoo.nacos;

import org.springframework.beans.factory.annotation.Autowired;
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.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
public class ClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ClientApplication.class, args);
    }

    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @RestController
    public class TestController {

        private final RestTemplate restTemplate;

        @Autowired
        public TestController(RestTemplate restTemplate) {
            this.restTemplate = restTemplate;
        }

        @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
        public String echo(@PathVariable String str) {
            return restTemplate.getForObject("http://zoo-plus-nacos-server/echo/" + str, String.class);
        }
    }
}

在RestTemplate上添加@LoadBalanced注解,使RestTemplate支持负载均衡,如果不加@LoadBalanced注解的话,会报java.net.UnknownHostException异常,此时无法通过注册到Nacos Server上的服务名来调用服务,因为RestTemplate是无法从服务名映射到ip:port的,映射的功能是由LoadBalancerClient来实现的。

第四步:启动服务,观察Nacos控制台,新的服务也已注册到Nacos。

我们访问8081,http://localhost:8081/echo/hahah,发现可以看到已调用了。

下面再借官方的一个图让大家更清晰的了解整个服务的注册与发现以及接口的调用过程。

项目地址:https://gitee.com/zoo-plus/zoo-plus-cloud/tree/nacos

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud可以使用Nacos作为注册中心。以下是使用Nacos作为注册中心的步骤: 1. 添加依赖 在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2.2.1.RELEASE</version> </dependency> ``` 2. 配置Nacos地址 在application.properties文件中添加以下配置: ``` spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 ``` 其中,127.0.0.1:8848是Nacos的地址。 3. 服务提供者配置 如果要使用Nacos作为服务注册中心,需要在服务提供者的配置文件中添加以下配置: ``` spring.application.name=service-provider spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 spring.cloud.nacos.discovery.register-enabled=true spring.cloud.nacos.discovery.port=8080 ``` 其中,service-provider是服务的名称,127.0.0.1:8848是Nacos的地址,8080是服务的端口号。 4. 服务消费者配置 如果要使用Nacos作为服务注册中心,需要在服务消费者的配置文件中添加以下配置: ``` spring.application.name=service-consumer spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 spring.cloud.nacos.discovery.port=8080 ``` 其中,service-consumer是服务的名称,127.0.0.1:8848是Nacos的地址,8080是服务的端口号。 5. 启动服务 启动服务提供者和服务消费者,它们将自动注册Nacos上。 6. 查看服务列表 在Nacos控制台中可以查看已注册服务列表。可以通过以下URL访问控制台:http://127.0.0.1:8848/nacos。 以上就是使用Nacos作为注册中心的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值