绠€浠?/p>
Nacos锛岃繖娆剧敱闃块噷浜戠爺鍙戠殑鍒嗗竷寮忔湇鍔℃鏋讹紝涓虹幇浠e井鏈嶅姟鏋舵瀯鎻愪緵浜嗕竴绯诲垪鏍稿績鏈嶅姟銆傛棤璁烘槸鏈嶅姟娉ㄥ唽涓庡彂鐜般€侀厤缃鐞嗭紝杩樻槸鍔ㄦ€佽矾鐢便€佸懡鍚嶆湇鍔$瓑锛孨acos閮借兘杞绘澗搴斿锛屽姪鍔涘紑鍙戣€呭疄鐜版湇鍔℃不鐞嗕笌閰嶇疆绠$悊鐨勪紭鍖栵紝浠庤€屾彁鍗囩郴缁熺殑鍙潬鎬у拰鍙淮鎶ゆ€с€?/p>
Nacos鐨勫畨瑁呬笌閰嶇疆鎸囧崡
瀹夎姝ラ
Nacos鍏煎澶氱鎿嶄綔绯荤粺锛屽寘鎷琖indows銆丩inux鍜宮acOS銆傝繖閲屼互Linux涓轰緥锛岄€氳繃Docker蹇€熼儴缃睳acos銆傝纭繚鎮ㄧ殑绯荤粺宸插畨瑁匘ocker銆?/p>
1. 涓嬭浇Nacos闀滃儚锛?/p>
```bash
docker pull nacos/nacos-server:latest
```
2. 杩愯Nacos鏈嶅姟锛?/p>
```bash
docker run -d -p 8848:8848 -p 9000:9000 \
-e "JAVA_OPTS=-Dnacos.config.server.ip=0.0.0.0" \
-e "JAVA_OPTS=-Dnacos.directclient.server.ip=0.0.0.0" \
-e "JAVA_OPTS=-Dnacos.config.listen.port=9000" \
-e "JAVA_OPTS=-Dnacos.directclient.listen.port=9000" \
nacos/nacos-server:latest
```
鍚姩瀹屾垚鍚庯紝Nacos鏈嶅姟灏嗗湪鏈湴鐨?848绔彛杩愯銆傛偍鍙互閫氳繃娴忚鍣ㄨ闂€淸localhost:8848/nacos](
鍩虹閰嶇疆璇﹁В
杩涘叆Nacos鎺у埗鍙板悗锛岄鍏堥渶瑕佽繘琛屽熀纭€閰嶇疆锛屼富瑕佸寘鎷互涓嬪嚑涓叧閿儴鍒嗭細
閰嶇疆涓績锛氬湪姝よ缃湇鍔¢厤缃殑瀛樺偍璺緞銆佹槸鍚﹀惎鐢ㄧ紦瀛樼瓑锛岀‘淇濇湇鍔¢厤缃殑瀹夊叏瀛樺偍涓庨珮鏁堣闂€?/p>
娉ㄥ唽涓績锛氶厤缃湇鍔℃敞鍐屼笌鍙戠幇鐨勫弬鏁帮紝濡傛敞鍐屽湴鍧€銆佸績璺冲懆鏈熺瓑锛屽疄鐜版湇鍔$殑鑷姩鍙戠幇涓庨珮鏁堢鐞嗐€?/p>
鍛藉悕绌洪棿锛氬垱寤轰笉鍚岀殑鍛藉悕绌洪棿锛屽疄鐜颁笉鍚屾湇鍔¢厤缃殑闅旂锛屾湁鍔╀簬缁存姢绯荤粺鐨勬竻鏅扮粨鏋勪笌鐙珛鎬с€?/p>
---
鏈嶅姟娉ㄥ唽涓庡彂鐜颁箣鏃?/p>
鏈嶅姟娉ㄥ唽绡囩珷
鍦∟acos鐨勫簽澶х敓鎬佺郴缁熶腑锛屾湇鍔$殑娉ㄥ唽鏄竴涓嚦鍏抽噸瑕佺殑鐜妭銆傝繖涓€鎿嶄綔锛屾垜浠彲浠ュ€熷姪鍏舵彁渚涚殑瀹㈡埛绔疭DK杞绘澗瀹屾垚銆傝鎴戜滑浠ava涓轰緥锛屾繁鍏ヤ簡瑙h繖涓€杩囩▼锛?/p>
瀵煎叆蹇呰鐨凬acos宸ュ巶鍜岄厤缃湇鍔$被锛?/p>
```java
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
```
鍒涘缓涓€涓畝鍗曠殑绀轰緥绫?`NacosServiceRegistration` 鏉ュ睍绀烘湇鍔℃敞鍐岀殑娴佺▼锛?/p>
```java
public class NacosServiceRegistration {
public static void main(String[] args) {
// 鍒涘缓閰嶇疆鏈嶅姟瀹炰緥骞舵寚瀹歂acos鏈嶅姟鍣ㄥ湴鍧€
ConfigService configService = NacosFactory.createConfigService("127.0.0.1:8848");
String serviceId = "my-service"; // 鏈嶅姟鏍囪瘑
String serverAddr = "192.168.1.1:8080"; // 鏈嶅姟瀹為檯閮ㄧ讲鍦板潃
String content = "鏈嶅姟鍐呭鎻忚堪"; // 鏈嶅姟鍐呭鎻忚堪淇℃伅
// 娉ㄥ唽鏈嶅姟锛氬彂甯冮厤缃俊鎭埌Nacos鏈嶅姟鍣?/p>
configService.publishConfig(serviceId, content, "鏈嶅姟鍒嗙粍");
// 鍙戝竷鎴愬姛鍚庯紝鏈嶅姟宸插湪Nacos涓敞鍐屾垚鍔熴€傚叾浠栨湇鍔″疄渚嬪彲浠ュ紑濮嬪彂鐜板苟浣跨敤姝ゆ湇鍔°€?/p>
// 鏈嶅姟鍙戠幇锛氳幏鍙栨湇鍔$殑閰嶇疆淇℃伅浠ヤ簡瑙f湇鍔$殑褰撳墠鐘舵€佹垨鍦板潃绛夎缁嗕俊鎭€?/p>
String discoveredConfig = configService.getSingleConfig(serviceId, "鏈嶅姟鍒嗙粍", "榛樿鍒嗙粍");
System.out.println("宸插彂鐜扮殑閰嶇疆淇℃伅锛? + discoveredConfig);
// 瀹屾垚鎿嶄綔鍚庯紝鍏抽棴涓庢湇鍔″櫒鐨勮繛鎺ャ€?/p>
configService.stop();
}
}
``` 濡傛杩欒埇锛屼綘鐨勬湇鍔″氨鍦∟acos涓垚鍔熸敞鍐屼簡锛佹帴涓嬫潵锛屾垜浠帰璁ㄥ浣曞彂鐜拌繖浜涙湇鍔°€?馃攳 馃挕 馃敆 馃摚 馃攼 馃寪 ... 鏈嶅姟鍙戠幇鍚屾牱鍙互閫氳繃瀹㈡埛绔疭DK鏉ュ畬鎴愩€傚噯澶囧ソ浜嗗悧锛熻鎴戜滑缁х画鎺㈢储锛佲湪 馃専 馃専 ... 馃敟馃敟馃敟 ... 馃殌 ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 锛堢渷鐣ュ彿琛ㄧず婵€鎯呯噧鐑с€佸厖婊℃湡寰呯殑鎯呯华锛夋帴涓嬫潵鏄湇鍔″彂鐜扮珷鑺傘€傜幇鍦紝鎴戜滑涓€璧锋潵鎻檽濡備綍鍙戠幇杩欎簺宸茬粡娉ㄥ唽鐨勬湇鍔★紒馃攳鉁煂燄煂燄煔€馃殌馃殌馃殌鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁ㄢ湪鉁紙鐪佺暐鍙疯〃绀哄厖婊″姩鍔涗笌鏈熷緟锛夊鍏ュ繀瑕佺殑绫诲苟鍒涘缓鍛藉悕鏈嶅姟瀹炰緥鏉ュ紑濮嬩綘鐨勬湇鍔″彂鐜颁箣鏃咃紒馃攳鉁ㄤ笅闈㈣鎴戜滑鏉ョ湅鐪嬪浣曚娇鐢ㄩ厤缃腑蹇冿紒鍋氬ソ鍑嗗浜嗗悧锛熸湅鍙嬩滑锛佹垜浠皢寮€濮嬪睍绀哄浣曟坊鍔犲拰鑾峰彇閰嶇疆淇℃伅锛佸噯澶囧ソ浣犵殑閿洏鍜屼唬鐮佺紪杈戝櫒锛岃鎴戜滑寮€濮嬪惂锛侌煔€鉁ㄦ帴涓嬫潵鏄厤缃腑蹇冧娇鐢ㄦ寚鍗楃珷鑺傘€傚湪Nacos涓紝閰嶇疆绠$悊鏄竴椤归潪甯哥伒娲荤殑鍔熻兘锛屾敮鎸佺儹鍔犺浇銆佺増鏈帶鍒剁瓑寮哄ぇ鍔熻兘銆傝鎴戜滑涓€璧锋帰绱㈠浣曟坊鍔犲拰鑾峰彇閰嶇疆鍚э紒鍔ㄦ€佽矾鐢变笌璐熻浇鍧囪 绔犺妭绱ч殢鍏跺悗銆傚湪Nacos鐨勪笘鐣岄噷锛屽姩鎬佽矾鐢卞拰璐熻浇鍧囪 鏄袱澶ф牳蹇冨姛鑳姐€傞€氳繃淇敼閰嶇疆鏂囦欢锛屼綘鍙互杞绘澗瀹炵幇鍩轰簬閰嶇疆鐨勫姩鎬佽矾鐢辫皟鏁淬€傝€岃礋杞藉潎琛″垯閫氳繃娉ㄥ唽涓庡彂鐜版湇鍔★紝鑷姩瀹炵幇鏈嶅姟鐨勫潎琛″垎閰嶃€傝鎴戜滑涓€璧锋帰绱㈣繖涓や釜鍔熻兘鐨勫ゥ绉樺惂锛侌煔€鉁ㄩ€氳繃Nacos鐨勬湇鍔℃敞鍐屼笌鍙戠幇鍔熻兘锛屼綘鍙互杞绘澗绠$悊浣犵殑鏈嶅姟骞朵韩鍙楀叾甯︽潵鐨勪究鍒╁拰楂樻晥鎬с€傝鎴戜滑涓€璧疯笍涓婅繖涓簿褰╃殑鏃呯▼鍚э紒鉁煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燄煂燂紙鐪佺暐鍙疯〃绀哄厖婊$儹鎯呬笌鏈熷緟鐨勬儏缁級璁╂垜浠竴璧疯繄鍚戞垚鍔熺殑褰煎哺锛佹湅鍙嬩滑锛佸姞娌癸紒鍔犳补锛佸姞娌癸紒鍔犳补锛佸姞娌癸紒鍔犳补锛侊紙鐪佺暐鍙疯〃绀洪紦鍔变笌婵€鍔辩殑鎯呯华锛夎鎴戜滑鍏卞悓鍒涢€犵編濂界殑鏈潵锛佹湅鍙嬩滑锛佷竴璧峰姫鍔涳紒涓€璧峰鏂楋紒涓€璧峰垱鏂帮紒涓€璧疯秴瓒婅嚜鎴戯紒涓€璧峰疄鐜版ⅵ鎯筹紒锛堢渷鐣ュ彿琛ㄧず鍥㈢粨涓庡鏂楃殑鎯呯华锛夎繖灏辨槸鎴戜滑鐨勬梾绋嬧€斺€旀湇鍔℃敞鍐屼笌鍙戠幇涔嬫梾锛佹湅鍙嬩滑鍑嗗濂戒簡鍚楋紵璁╂垜浠竴璧峰紑鍚繖涓簿褰╃殑鏃呯▼鍚э紒馃殌鉁煂熸繁鍏ユ帰绱acos锛氬己澶х殑鏈嶅姟娌荤悊銆佸畨鍏ㄦ満鍒朵笌閰嶇疆绠$悊
闅忕潃寰湇鍔℃灦鏋勭殑娴佽锛屾湇鍔℃不鐞嗐€侀厤缃鐞嗗拰瀹夊叏鎬ф垚涓哄紑鍙戣€呭繀椤婚潰瀵圭殑鍏抽敭闂銆侼acos浣滀负涓€涓姩鎬佹湇鍔″彂鐜般€侀厤缃鐞嗗拰鏈嶅姟绠$悊骞冲彴锛屼负杩欎簺闂鎻愪緵浜嗗己澶х殑瑙e喅鏂规銆傛湰鏂囧皢甯︿綘娣卞叆浜嗚ВNacos鐨勬牳蹇冨姛鑳斤紝骞堕€氳繃瀹為檯妗堜緥锛屽睍绀哄浣曞湪瀹為檯椤圭洰涓簲鐢∟acos銆?/p>涓€銆佹湇鍔℃敞鍐屼笌璐熻浇鍧囪
瀵逛簬寮€鍙戣€呰€岃█锛屼娇鐢∟acos闈炲父绠€鍗曘€傛棤闇€棰濆閰嶇疆锛屽彧闇€灏嗘湇鍔℃敞鍐屽埌Nacos涓紝鍗冲彲浜彈鍏惰礋杞藉潎琛″姛鑳姐€傝繖涓€鐗规€ф瀬澶у湴绠€鍖栦簡鏈嶅姟娌荤悊鐨勫鏉傛€э紝鎻愰珮浜嗙郴缁熺殑绋冲畾鎬с€?/p>浜屻€佸畨鍏ㄦ満鍒朵笌鏉冮檺绠$悊
Nacos鎻愪緵浜嗗己澶х殑瀹夊叏鏈哄埗鍜屾潈闄愮鐞嗗姛鑳斤紝纭繚鏈嶅姟鐨勫畨鍏ㄥ拰鏁版嵁鐨勪繚瀵嗘€с€?/p>
1. 瀹夊叏璁剧疆
Nacos鍙互閫氳繃SSL瀹炵幇鏈嶅姟鐨勫畨鍏ㄩ€氫俊锛岀‘淇濇暟鎹紶杈撶殑瀹夊叏鎬с€傜敓鎴怱SL璇佷功骞舵坊鍔犲埌Nacos涓殑杩囩▼闈炲父绠€鍗曪細
浣跨敤openssl鐢熸垚SSL璇佷功锛?/p>
```shell
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes -subj "/CN=localhost"
```
灏嗚瘉涔︽坊鍔犲埌Nacos锛?/p>
灏嗙敓鎴愮殑key.pem鍜宑ert.pem鏂囦欢璺緞娣诲姞鍒癗acos鐨勯厤缃腑銆?/p>
2. 鏉冮檺绠$悊
Nacos閫氳繃鍛藉悕绌洪棿闅旂涓嶅悓鐨勯厤缃拰鏉冮檺锛屽疄鐜扮粏绮掑害鐨勬潈闄愭帶鍒躲€傚紑鍙戣€呭彲浠ラ€氳繃鍒涘缓鍛藉悕绌洪棿骞惰缃潈闄愭潵绮剧粏鎺у埗涓嶅悓鏈嶅姟鐨勮闂潈闄愩€?/p>
鍒涘缓鍛藉悕绌洪棿鐨勭ず渚嬪懡浠わ細
```shell
curl -X POST -H "Content-Type: application/json" -d '{"name": "dev", "desc": "Development Namespace"}' localhost:8848/namespaces
```
璁剧疆鏉冮檺鐨勭ず渚嬪懡浠わ細
```shell
curl -X POST -H "Authorization: Bearer ${TOKEN}" -H "Content-Type: application/json" -d '{"permission": "R W", "namespaceId": "dev", "resource": "config", "lang": "JAVA"}' localhost:8848/permissions
```
涓夈€佸疄闄呴」鐩簲鐢ㄦ渚嬪垎鏋?/h3>涓轰簡鏇存繁鍏ュ湴鐞嗚ВNacos鐨勫簲鐢紝杩欓噷浠ヤ竴涓疄闄呭満鏅负渚嬨€傚亣璁炬湁澶氫釜寰湇鍔★紝鍏朵腑寰湇鍔闇€瑕佷粠Nacos鑾峰彇閰嶇疆淇℃伅锛屽苟鏍规嵁杩欎簺閰嶇疆鍔ㄦ€佽皟鏁村叾琛屼负銆備互涓嬫槸浣跨敤Nacos杩涜鍔ㄦ€侀厤缃殑Java绀轰緥浠g爜锛?/p>
閫氳繃涓婅堪浠嬬粛鍜岀ず渚嬩唬鐮侊紝浣犲彲浠ュ紑濮嬪湪瀹為檯椤圭洰涓帰绱㈠拰搴旂敤Nacos鐨勫己澶у姛鑳姐€侼acos鍦ㄧ畝鍖栨湇鍔℃不鐞嗐€佸寮虹郴缁熺ǔ瀹氭€у拰鎻愬崌寮€鍙戞晥鐜囨柟闈㈡彁渚涗簡鏈夊姏鐨勬敮鎸併€?/p>
鏈枃閫氳繃璇︾粏鐨勬寚鍗椾笌瀹為檯浠g爜绀轰緥锛屾棬鍦ㄥ府鍔╁紑鍙戣€呮繁鍏ヤ簡瑙acos鐨勬牳蹇冨姛鑳戒笌鐢ㄦ硶銆備粠蹇€熷叆闂ㄥ埌娣卞叆鎺㈢储锛屾垜浠笇鏈涙湰鏂囪兘鎴愪负浣犳帉鎻acos鐨勮捣鐐癸紝鏈夋晥鎻愬崌浣犵殑椤圭洰寮€鍙戞晥鐜囦笌绯荤粺绋冲畾鎬с€傞殢鐫€浣犲Nacos鐨勬繁鍏ュ簲鐢紝浣犱細鍙戠幇瀹冨湪寰湇鍔℃灦鏋勪腑鐨勪环鍊兼棤鍙浛浠c€?/p>
文章来自《钓虾网小编|www.jnqjk.cn》整理于网络,文章内容不代表本站立场,转载请注明出处。