Nacos 作爲服務註冊中心 - 配置中心
註冊中心
- 服務註冊與發現流程
角色說明
-
服務註冊中心(Register Service):它是一個 Nacos Server,可以爲服務提供者和服務消費者提供服務註冊和發現功能。
-
服務提供者(Provider Service):它是一個 Nacos Client,用於對外服務。它將自己提供的服務註冊到服務註冊中心,以供服務消費者發現和調用。
-
服務消費者(Consumer Service):它是一個 Nacos Client,用於消費服務。它可以從服務註冊中心獲取服務列表,調用所需的服務。
流程說明
-
從 Nacos 官方提供的下載頁面中,下載 Nacos Server 並運行。
-
服務提供者 Nacos Client 啓動時,會把服務以服務名(spring.application.name)的方式註冊到服務註冊中心(Nacos Server);
-
服務消費者 Nacos Client 啓動時,也會將自己的服務註冊到服務註冊中心;
-
服務消費者在註冊服務的同時,它還會從服務註冊中心獲取一份服務註冊列表信息,該列表中包含了所有註冊到服務註冊中心上的服務的信息(包括服務提供者和自身的信息);
-
在獲取了服務提供者的信息後,服務消費者通過 HTTP 或消息中間件遠程調用服務提供者提供的服務。
- 單機版客戶端搭建
引入依賴
<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);
}
}
頁面查看
配置中心
- 配置獲取流程圖
- 單機版客戶端搭建
引入依賴
編寫 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 格式中各參數說明如下:
-
${prefix}:默認取值爲微服務的服務名,即配置文件中
spring.application.name
的值,我們可以在配置文件中通過配置spring.cloud.nacos.config.prefix
來指定。 -
${spring.profiles.active}:表示當前環境對應的 Profile,例如 dev、test、prod 等。當沒有指定環境的 Profile 時,其對應的連接符也將不存在, dataId 的格式變成 ${prefix}.${file-extension}。
-
${file-extension}:表示配置內容的數據格式,可以在配置文件中通過配置項
spring.cloud.nacos.config.file-extension
來配置,例如 properties 和 yaml。
啓動類添加註解
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