這篇文章主要介紹“NACOS多環(huán)境配置的過程”,在日常操作中,相信很多人在NACOS多環(huán)境配置的過程問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”NACOS多環(huán)境配置的過程”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
蕪湖網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)!從網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、APP開發(fā)、成都響應式網(wǎng)站建設公司等網(wǎng)站項目制作,到程序開發(fā),運營維護。成都創(chuàng)新互聯(lián)于2013年創(chuàng)立到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設就選成都創(chuàng)新互聯(lián)。
啟動命令(standalone代表著單機模式運行,非集群模式):
sh startup.sh -m standalone
如果您使用的是ubuntu系統(tǒng),或者運行腳本報錯提示[[符號找不到,可嘗試如下運行:
bash startup.sh -m standalone
啟動命令:
cmd startup.cmd
或者雙擊startup.cmd運行文件。
訪問:127.0.0.1:8848/nacos/index.html 出現(xiàn)登錄界面,啟動成功。(用戶名和秘密都是nacos)
首先Nacos規(guī)定必須是bootstrap配置文件才能注入。我項目中用yml,示例如下:
server: port: 8060 spring: application: name: power-match #項目名 profiles: active: local cloud: nacos: config: server-addr: 127.0.0.1:8848 #注冊中心地址 discovery: server-addr: 127.0.0.1:8848
其次啟動類示例如下(我使用了feign):
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class PowerMatchApplication {
public static void main(String[] args) {
SpringApplication.run(PowerMatchApplication.class, args);
}
}運行后去Nacos頁面查看,效果如下:

表示注冊成功。
在這我只舉例官方推薦的方法,別的就不再介紹了。還是同一個nacos,登錄——找到命名空間——新建命名空間,輸入內(nèi)容后就會生成命名空間ID
以application-local.yml配置為例:
spring: cloud: nacos: config: namespace: e503611c-9c54-4669-baff-e12770b3e948 discovery: namespace: e503611c-9c54-4669-baff-e12770b3e948 ribbon: ReadTimeout: 60000 ConnectTimeout: 60000
啟動后,去看服務列表的test下面就有注冊的服務了,只會服務調(diào)用就會只在local中調(diào)用。
他支持多種服務消費方式WebClient、Feign、RestTemplate。
@EnableDiscoveryClient
@SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
@Slf4j
@RestController
static class TestController {
@Autowired
private WebClient.Builder webClientBuilder;
@GetMapping("/test")
public Mono<String> test() {
Mono<String> result = webClientBuilder.build()
.get()
.uri("http://alibaba-nacos-discovery-server/hello?name=didi")
.retrieve()
.bodyToMono(String.class);
return result;
}
}
@Bean
@LoadBalanced
public WebClient.Builder loadBalancedWebClientBuilder() {
return WebClient.builder();
}
}上面介紹的RestTemplate和WebClient都是Spring自己封裝的工具,下面介紹一個Netflix OSS中的成員,通過它可以更方便的定義和使用服務消費客戶端。下面也舉一個具體的例子,其實現(xiàn)內(nèi)容與上面兩種方式結(jié)果一致:
第一步:在pom.xml中增加openfeign的依賴:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
第二步:定義Feign客戶端和使用Feign客戶端:
@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
@Slf4j
@RestController
static class TestController {
@Autowired
Client client;
@GetMapping("/test")
public String test() {
String result = client.hello("word");
return "Return : " + result;
}
}
@FeignClient("alibaba-nacos-discovery-server")
interface Client {
@GetMapping("/hello")
String hello(@RequestParam String name);
}
}@EnableDiscoveryClient
@SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
@Slf4j
@RestController
static class TestController {
@Autowired
RestTemplate restTemplate;
@GetMapping("/test")
public String test() {
String result = restTemplate.getForObject("http://alibaba-nacos-discovery-server/hello?name=word", String.class);
return result;
}
}
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}總的來說,還說和以前沒的什么區(qū)別。
到此,關(guān)于“NACOS多環(huán)境配置的過程”的學習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
網(wǎng)站名稱:NACOS多環(huán)境配置的過程
文章來源:http://chinadenli.net/article24/gsjgce.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供云服務器、ChatGPT、營銷型網(wǎng)站建設、虛擬主機、電子商務、手機網(wǎng)站建設
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)