暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

SpringBoot 集成 Nacos

左羊公社 2023-03-11
729

省流-请直接参考Nacos官网 :Nacos 融合 Spring Boot,成为注册配置中心

https://nacos.io/zh-cn/docs/v2/ecology/use-nacos-with-spring-boot.html

Nacos是阿里巴巴开源的一款注册中心,和常见的Zookeeper、Consul等注册中心一样可以用来做服务注册和发现。在微服务架构中,服务注册和发现是一个非常重要的环节,Nacos可以一站式满足服务的注册、发现、配置管理等需求。本篇文章将介绍如何在SpringBoot项目中集成Nacos。

一、添加依赖

首先,在项目的pom.xml
文件中添加Nacos的相关依赖:

<!-- Nacos Discovery -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    <version>2.2.3.RELEASE</version>
</dependency>

二、配置Nacos连接信息

在SpringBoot项目的application.yml
或者 application.properties
中添加连接Nacos的相关信息配置:

spring:
  application:
    name: your-service # 应用名称,将会作为服务名注册到Nacos中心
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 # 配置Nacos的地址和端口

三、在应用中使用Nacos

  1. 服务注册

在进行服务注册之前,我们先创建一个Controller提供一个简单的API接口:

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

@RestController
public class TestController {
    @GetMapping("/hello")
    public String hello() {
        return "hello, SpringBoot with Nacos";
    }
}

服务注册非常简单,只需要在项目启动类中添加@EnableDiscoveryClient注解即可:

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

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

在控制台上可以看到类似如下输出信息:

Registering service with nacos server: [nacos] 2021-02-25 22:27:10.356  INFO 11680 --- [           main] c.a.n.d.NacosServiceRegistry            : Nacos ServiceRegistry registry, service: your-service, address: 127.0.0.1,8080, weight: 1

我们再去Nacos的控制台可以看到我们的应用注册上来了:

  1. 服务消费

Nacos还提供了非常方便的服务消费功能。在Spring Cloud中,我们通常使用RestTemplate发送HTTP请求调用服务,Nacos为我们提供了更加方便的接口来调用服务。在Controller中添加如下方法:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/consumer")
    public String helloConsumer() {
        // 获取your-service服务的一个实例
        ServiceInstance instance = discoveryClient.getInstances("your-service").get(0);
        String url = "http://" + instance.getHost() + ":" + instance.getPort() + "/hello";
        return restTemplate.getForObject(url, String.class);
    }
}

四、Nacos配置管理

除了服务注册和发现,Nacos还提供了配置管理功能,可以统一管理项目的配置信息。在SpringBoot项目中使用Nacos的配置管理非常方便,只需要依赖spring-cloud-starter-alibaba-nacos-config
,并在bootstrap.yml
或者bootstrap.properties
文件中添加Nacos配置信息即可。例如:

spring:
  cloud:
    nacos:
      config:
        server-addr: localhost:8848
        group: TEST_GROUP # 配置分组
        namespace: you_namespace # 配置命名空间
        file-extension: yml # 配置文件类型

在Nacos注册中心新建一个名为your-service.yml
的配置文件,例如:

server:
  port: 8080
  servlet:
    context-path: /demo

我们就可以在应用中使用Nacos提供的@NacosPropertySource
@NacosValue
注解来获取Nacos配置了,例如:

import com.alibaba.nacos.api.config.annotation.NacosPropertySource;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@NacosPropertySource(dataId = "your-service", groupId = "TEST_GROUP", autoRefreshed = true)
public class ConfigController {
    @NacosValue(value = "${server.port}", autoRefreshed = true)
    private Integer port;

    @GetMapping("/config")
    public String getCOnfig() {
        return "port: " + port;
    }
}

这样,我们就可以通过访问http://localhost:8080/config
来获取Nacos中的配置信息了。

参考文献

    Nacos 官网: https://nacos.io/zh-cn/index.html
    Spring Cloud Alibaba Github:https://github.com/spring-cloud-incubator/spring-cloud-alibaba
    spring-cloud-starter-alibaba-nacos-config官方文档:https://github.com/spring-cloud-incubator/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-examples/nacos-example/nacos-example-config/src/main/resources/application.yml
    spring-cloud-starter-alibaba-nacos-discovery 官方文档:https://github.com/spring-cloud-incubator/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-examples/nacos-example/nacos-example-discovery/src/main/resources/application.yml


    文章转载自左羊公社,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

    评论