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

表示注冊(cè)成功。
在這我只舉例官方推薦的方法,別的就不再介紹了。還是同一個(gè)nacos,登錄——找到命名空間——新建命名空間,輸入內(nèi)容后就會(huì)生成命名空間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
啟動(dòng)后,去看服務(wù)列表的test下面就有注冊(cè)的服務(wù)了,只會(huì)服務(wù)調(diào)用就會(huì)只在local中調(diào)用。
他支持多種服務(wù)消費(fèi)方式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自己封裝的工具,下面介紹一個(gè)Netflix OSS中的成員,通過它可以更方便的定義和使用服務(wù)消費(fèi)客戶端。下面也舉一個(gè)具體的例子,其實(shí)現(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)境配置的過程”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
網(wǎng)站名稱:NACOS多環(huán)境配置的過程
文章來源:http://www.chinadenli.net/article24/gsjgce.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、ChatGPT、營(yíng)銷型網(wǎng)站建設(shè)、虛擬主機(jī)、電子商務(wù)、手機(jī)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)