Posted 绋嬪簭鍛樻偿鐡﹀尃
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了相关的知识,希望对你有一定的参考价值。
鑰佸娴欐睙涓滄捣杈癸紝闈犳捣鍚冩捣锛岀洰鍓嶇粡钀ヤ竴涓皬鍝佺墝锛岃鏅€氫汉鍚冨埌鏈€鏂伴矞鐨勬捣椴溿€傛湁鍏磋叮鍙互鐐瑰嚮浜嗚В锛氥€娿€嬸煇燄煇燄煇?/p>
鏈枃鎺㈣ 浣滀负閰嶇疆涓績锛屽浣曞疄鐜颁笉鍚岀幆澧冿紙寮€鍙戙€佹祴璇曘€佺伆搴︺€佹寮忥級鐨勯厤缃鐞嗛棶棰樸€?/span>
鐢辨锛屽疄鐜板鐜閰嶇疆鐨勬柟妗堜篃鏈変笁绉嶏細
1銆佺敤鍛藉悕绌洪棿锛坣amespace锛夋潵鍖哄垎涓嶅悓鐨勭幆澧冿紝涓€涓懡鍚嶇┖闂村搴斾竴涓幆澧冿紱
2銆佺敤閰嶇疆缁勶紙group锛夋潵鍖哄垎涓嶅悓鐨勭幆澧冿紝鍛藉悕绌洪棿鐢ㄩ粯璁ょ殑public鍗冲彲锛屼竴涓粍瀵瑰簲涓€绉嶇幆澧冿紱
3銆佺敤閰嶇疆闆咺D锛圖ata ID锛夊悕绉版潵鍖哄垎涓嶅悓鐨勭幆澧冿紝鍛藉悕绌洪棿鍜岀粍鐢ㄩ粯璁ょ殑鍗冲彲锛岄€氳繃鏂囦欢鍛藉悕鏉ュ尯鍒嗭紱
鎺ヤ笅鏉ワ紝閫愪釜鏉ョ湅
http://{host}:{port}/nacos
http://{host}:{port}/nacos/index.html
榛樿鐢ㄦ埛鍚嶅瘑鐮侀兘鏄痭acos
![閰嶇疆涓績 Nacos 涓嶅悓鐜鐨勯厤缃鐞嗘柟妗?>
</figure>
<p data-tool=](https://image.cha138.com/20210329/e88017fa5ab44f3da54ff0101a73c506.jpg)
![閰嶇疆涓績 Nacos 涓嶅悓鐜鐨勯厤缃鐞嗘柟妗?>
</figure>
<p data-tool=](https://image.cha138.com/20210329/72fcaee38f154582855477f23ee91b12.jpg)
1 <?xml version="1.0" encoding="UTF-8"?>
2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4 <modelVersion>4.0.0</modelVersion>
5 <parent>
6 <groupId>org.springframework.boot</groupId>
7 <artifactId>spring-boot-starter-parent</artifactId>
8 <version>2.3.6.RELEASE</version>
9 <relativePath/> <!-- lookup parent from repository -->
10 </parent>
11 <groupId>com.example</groupId>
12 <artifactId>example</artifactId>
13 <version>0.0.1-SNAPSHOT</version>
14 <name>example</name>
15
16 <properties>
17 <java.version>1.8</java.version>
18 <spring-cloud-alibaba.version>2.2.3.RELEASE</spring-cloud-alibaba.version>
19 </properties>
20
21 <dependencies>
22 <dependency>
23 <groupId>org.springframework.boot</groupId>
24 <artifactId>spring-boot-starter-web</artifactId>
25 </dependency>
26 <dependency>
27 <groupId>com.alibaba.cloud</groupId>
28 <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
29 </dependency>
30 </dependencies>
31
32 <dependencyManagement>
33 <dependencies>
34 <dependency>
35 <groupId>com.alibaba.cloud</groupId>
36 <artifactId>spring-cloud-alibaba-dependencies</artifactId>
37 <version>${spring-cloud-alibaba.version}</version>
38 <type>pom</type>
39 <scope>import</scope>
40 </dependency>
41 </dependencies>
42 </dependencyManagement>
43
44 <build>
45 <plugins>
46 <plugin>
47 <groupId>org.springframework.boot</groupId>
48 <artifactId>spring-boot-maven-plugin</artifactId>
49 </plugin>
50 </plugins>
51 </build>
52
53 </project>
bootstrap.yml
spring:
application:
name: example
cloud:
nacos:
config:
server-addr: 192.168.100.10:8848
file-extension: yaml
Spring Boot 鍩虹涓嶄粙缁嶄簡锛屼笉浼氱殑鎺ㄨ崘鐪嬩笅杩欎釜鏁欑▼锛?/p>
https://github.com/javastacks/spring-boot-best-practice
HelloController.java
package com.example.controller;
import org.springframework.beans.factory.annotation.Value;
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;
/**
* @author ChengJianSheng
* @data 2020/11/19
*/
@RestController
@RequestMapping("/hello")
@RefreshScope
public class HelloController {
@Value("${greet.hello}")
private String greet;
@GetMapping("/sayHi")
public String sayHi() {
return greet;
}
}
1銆佸埄鐢?Data ID 鍛藉悕 鏉ュ尯鍒嗙幆澧?/h2>
鍒╃敤Data ID鍛藉悕鏉ュ尯鍒嗙幆澧冿紝鍛藉悕绌洪棿鍜岀粍榛樿鍗冲彲銆?/p>
鍦?Nacos Spring Cloud 涓紝dataId 鐨勫畬鏁存牸寮忓涓嬶細
${prefix}-${spring.profiles.active}.${file-extension}
-
prefix 榛樿涓?spring.application.name 鐨勫€硷紝涔熷彲浠ラ€氳繃閰嶇疆椤?spring.cloud.nacos.config.prefix鏉ラ厤缃? -
spring.profiles.active 鍗充负褰撳墠鐜瀵瑰簲鐨?profile锛岃鎯呭彲浠ュ弬鑰?Spring Boot鏂囨。銆傛敞鎰忥細褰?spring.profiles.active 涓虹┖鏃讹紝瀵瑰簲鐨勮繛鎺ョ - 涔熷皢涓嶅瓨鍦紝dataId 鐨勬嫾鎺ユ牸寮忓彉鎴?nbsp; {file-extension} -
file-exetension 涓洪厤缃唴瀹圭殑鏁版嵁鏍煎紡锛屽彲浠ラ€氳繃閰嶇疆椤?spring.cloud.nacos.config.file-extension 鏉ラ厤缃€傜洰鍓嶅彧鏀寔 properties 鍜?yaml 绫诲瀷銆?
![閰嶇疆涓績 Nacos 涓嶅悓鐜鐨勯厤缃鐞嗘柟妗?>
</figure>
<figure data-tool=](https://image.cha138.com/20210329/eb75df0bbb394f589cc46e2bb247496d.jpg)
![閰嶇疆涓績 Nacos 涓嶅悓鐜鐨勯厤缃鐞嗘柟妗?>
</figure>
<figure data-tool=](https://image.cha138.com/20210329/f83f7bb243284165a6ff0cc6cfafc659.jpg)
![閰嶇疆涓績 Nacos 涓嶅悓鐜鐨勯厤缃鐞嗘柟妗?>
</figure>
<figure data-tool=](https://image.cha138.com/20210329/0a66731d96954cef854e65bddc52d21a.jpg)
![閰嶇疆涓績 Nacos 涓嶅悓鐜鐨勯厤缃鐞嗘柟妗?>
</figure>
<figure data-tool=](https://image.cha138.com/20210329/5f8ee3774972407db508d62086eff4e3.jpg)
![閰嶇疆涓績 Nacos 涓嶅悓鐜鐨勯厤缃鐞嗘柟妗?>
</figure>
<p data-tool=](https://image.cha138.com/20210329/505be8e5fbf14ad1921c1da359741093.jpg)
![閰嶇疆涓績 Nacos 涓嶅悓鐜鐨勯厤缃鐞嗘柟妗?>
</figure>
<p data-tool=](https://image.cha138.com/20210329/069299514bbc4d3aa28fffb2dbfec441.jpg)
java -Dspring.profiles.active=test -jar example-0.0.1-SNAPSHOT.jar
![閰嶇疆涓績 Nacos 涓嶅悓鐜鐨勯厤缃鐞嗘柟妗?>
</figure>
<h2 data-tool=](https://image.cha138.com/20210329/565f57141b0f457a85705b564a537710.jpg)
椤圭洰涓嶅彉锛屾垜浠妸spring.application.name鏀规垚example2
鍛藉悕绌洪棿鐢ㄩ粯璁ょ殑public
![閰嶇疆涓績 Nacos 涓嶅悓鐜鐨勯厤缃鐞嗘柟妗?>
</figure>
<figure data-tool=](https://image.cha138.com/20210329/7fc5296e5d594b819143e1a57f2206a6.jpg)
![閰嶇疆涓績 Nacos 涓嶅悓鐜鐨勯厤缃鐞嗘柟妗?>
</figure>
<figure data-tool=](https://image.cha138.com/20210329/4fcd176e5f784525a5a74a2af1e55342.jpg)
![閰嶇疆涓績 Nacos 涓嶅悓鐜鐨勯厤缃鐞嗘柟妗?>
</figure>
<pre data-tool=](https://image.cha138.com/20210329/48941f1d70594a6f98653f08153ec6fc.jpg)
java -Dspring.cloud.nacos.config.group=DEV_GROUP -jar example-0.0.1-SNAPSHOT.jar
![閰嶇疆涓績 Nacos 涓嶅悓鐜鐨勯厤缃鐞嗘柟妗? class=](https://image.cha138.com/20210329/37921f4dbaf0484e807fa2f54537cb4e.jpg)
java -Dspring.cloud.nacos.config.group=TEST_GROUP -jar example-0.0.1-SNAPSHOT.jar
![閰嶇疆涓績 Nacos 涓嶅悓鐜鐨勯厤缃鐞嗘柟妗?>
</figure>
<pre data-tool=](https://image.cha138.com/20210329/30960a58693341e9ad32a6745071c7dc.jpg)
java -Dspring.profiles.active=test -Dspring.cloud.nacos.config.group=TEST_GROUP -jar example-0.0.1-SNAPSHOT.jar
濡傛灉鏄繖鏍风殑璇濓紝杩欎釜鏃跺€欙紝Data ID 鍛藉悕灏卞簲璇ユ槸 example2-test.yaml
3銆佸埄鐢?Namespace 鍖哄垎鐜
![閰嶇疆涓績 Nacos 涓嶅悓鐜鐨勯厤缃鐞嗘柟妗?>
</figure>
<p data-tool=](https://image.cha138.com/20210329/dc9a75dfb7034b758326e5038d33ee81.jpg)
![閰嶇疆涓績 Nacos 涓嶅悓鐜鐨勯厤缃鐞嗘柟妗?>
</figure>
<figure data-tool=](https://image.cha138.com/20210329/7a21361c9aac4db8bed81b2ca540e537.jpg)
![閰嶇疆涓績 Nacos 涓嶅悓鐜鐨勯厤缃鐞嗘柟妗?>
</figure>
<figure data-tool=](https://image.cha138.com/20210329/d21d1879cfe74ea986bd1c994d32cda6.jpg)
![閰嶇疆涓績 Nacos 涓嶅悓鐜鐨勯厤缃鐞嗘柟妗?>
</figure>
<figure data-tool=](https://image.cha138.com/20210329/03a4e03e04bd4268a8e411d8c937789b.jpg)
![閰嶇疆涓績 Nacos 涓嶅悓鐜鐨勯厤缃鐞嗘柟妗?>
</figure>
<figure data-tool=](https://image.cha138.com/20210329/00af36abdf0e4cd3af5153c7abd9cda3.jpg)
![閰嶇疆涓績 Nacos 涓嶅悓鐜鐨勯厤缃鐞嗘柟妗?>
</figure>
<figure data-tool=](https://image.cha138.com/20210329/defa626056de453a80f5f2f40d67a990.jpg)
![閰嶇疆涓績 Nacos 涓嶅悓鐜鐨勯厤缃鐞嗘柟妗?>
</figure>
<figure data-tool=](https://image.cha138.com/20210329/2c0da04a82cb4f1cadacb81f18ce02d4.jpg)
![閰嶇疆涓績 Nacos 涓嶅悓鐜鐨勯厤缃鐞嗘柟妗?>
</figure>
<pre data-tool=](https://image.cha138.com/20210329/f86451c3c9984aa79d8828e8890ff481.jpg)
java -Dspring.cloud.nacos.config.namespace=ns_dev -jar example-0.0.1-SNAPSHOT.jar
![閰嶇疆涓績 Nacos 涓嶅悓鐜鐨勯厤缃鐞嗘柟妗? class=](https://image.cha138.com/20210329/8e5a226eee18414cb4606b4cbecb436a.jpg)
java -Dspring.profiles.active=dev -Dspring.cloud.nacos.config.namespace=ns_dev -jar example-0.0.1-SNAPSHOT.jar
![閰嶇疆涓績 Nacos 涓嶅悓鐜鐨勯厤缃鐞嗘柟妗? class=](https://image.cha138.com/20210329/69a18256140a45369261927645b7fb85.jpg)
java -Dspring.cloud.nacos.config.namespace=ns_test -jar example-0.0.1-SNAPSHOT.jar
![閰嶇疆涓績 Nacos 涓嶅悓鐜鐨勯厤缃鐞嗘柟妗? class=](https://image.cha138.com/20210329/e094db30f16c442aaa5bd57da549054f.jpg)
java -Dspring.profiles.active=test
-Dspring.cloud.nacos.config.namespace=ns_test
-Dspring.cloud.nacos.config.group=TEST_GROUP
-jar example-0.0.1-SNAPSHOT.jar
![閰嶇疆涓績 Nacos 涓嶅悓鐜鐨勯厤缃鐞嗘柟妗? class=](https://image.cha138.com/20210329/b2a3bbb4b2b34145af06abf9a1dbe352.jpg)
4銆佸皬缁?/h2>
绗竴绉嶏紝鐢?Data ID 鍖哄垎鐜锛岃櫧鐒剁畝鍗曪紝浣嗘槸姣忎釜椤圭洰瑕佸垱寤?涓厤缃枃浠讹紝闅忕潃椤圭洰鐨勫澶氾紝閮藉湪涓€涓懡鍚嶇┖闂翠笅鍥炴樉寰楀緢娣蜂贡锛屾煡鎵捐捣鏉ヤ篃涓嶆槸寰堟柟渚匡紝鑰屼笖涓嶅埄浜庡仛鏉冮檺鎺у埗
绗簩绉嶏紝鐢℅roup鍖哄垎锛岄棶棰樹篃鏄竴鏍风殑
缁间笂锛屾渶濂界殑鏄敤Namespace鍖哄垎鐜锛屾竻鏅版槑浜嗭紝鑰屼笖鏈夊埄浜庡仛鏉冮檺鎺у埗
https://nacos.io/zh-cn/docs/concepts.html
![閰嶇疆涓績 Nacos 涓嶅悓鐜鐨勯厤缃鐞嗘柟妗? class=](https://image.cha138.com/20210329/2520ad2daebb4bb68b3fe1b46d525eda.jpg)
鑰佸娴欐睙涓滄捣杈癸紝闈犳捣鍚冩捣锛岀洰鍓嶇粡钀ヤ竴涓皬鍝佺墝锛岃鏅€氫汉鍚冨埌鏈€鏂伴矞鐨勬捣椴溿€傛湁鍏磋叮鍙互鐐瑰嚮浜嗚В锛氥€娿€嬸煇燄煇燄煇?/p>
寰€鏈熸帹鑽?/p>
涓嬫柟浜岀淮鐮佸叧娉ㄦ垜
鎶€鏈崏鏍?/span>锛?span>鍧氭寔鍒嗕韩 缂栫▼锛岀畻娉曪紝鏋舵瀯
以上是关于的主要内容,如果未能解决你的问题,请参考以下文章