Nacos 作爲服務註冊中心 - 配置中心

註冊中心

  1. 服務註冊與發現流程

角色說明

流程說明

  1. 單機版客戶端搭建

引入依賴

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

編寫配置文件

management:
  endpoints:
     web:
       exposure:
         include: '*'  #暴露所有端點
server:
  port: 8081
spring:
  application:
    name: service-provider
cloud:
  nacos:
    discovery:
      server-addr: 127.0.0.1:8848   #nacos地址

啓動類添加註解

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

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

頁面查看

配置中心

  1. 配置獲取流程圖

  1. 單機版客戶端搭建

引入依賴

編寫 bootstrap.yml 配置文件

bootstrap.yml 是系統級別的,加載優先級高於application.yml ,負責從外部加載配置並解析

management:
  endpoints:
     web:
       exposure:
         include: '*' #springboot監控開啓所有端點
server:
  port: 8083
spring:
  application:
    name: config-client
cloud:
  nacos:
    discovery:
      server-addr: 127.0.0.1:8848   #服務註冊到nacos註冊中心的地址
    config:
      server-addr: 127.0.0.1:8848 #Nacos作爲配置中心地址
      file-extension: yaml #指定yaml格式的配置

編寫application.yml配置文件

可以通過指定不同的激活文件配合 Data Id 從 nacos 獲取不同環境下的配置

spring:
  profiles:
     active: dev #激活 dev 的配置

nacos 控制檯添加配置

Nacos Server 配置的 Data ID 的完整格式如下:${prefix}-${spring.profiles.active}.${file-extension}  dataId 格式中各參數說明如下:

啓動類添加註解

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

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

編寫配置類獲取配置

注意註解@RefreshScope

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "config")
@RefreshScope
public class Config {

  private String version;

  private String name;

  public String getVersion() {
      return version;
  }

  public void setVersion(String version) {
      this.version = version;
  }

  public String getName() {
      return name;
  }

  public void setName(String name) {
      this.name = name;
  }
}

配置使用

注意註解@RefreshScope

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RefreshScope
@RestController
@RequestMapping("api/vi/config-client")
public class Controller {

  @Autowired
  private Config config;

  @GetMapping
  public String get() {
      return "name:" + config.getName() + " | version:" + config.getVersion();
  }
}

愛你們

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/hIf1s68-mMy55FkNDbuyGQ