SpringCloud通過Nacos實(shí)現(xiàn)注冊(cè)中心與遠(yuǎn)程服務(wù)調(diào)用詳解流程
本文主要記錄基于Nacos實(shí)現(xiàn)服務(wù)注冊(cè)中心和遠(yuǎn)程服務(wù)調(diào)用
1. 基于Nacos實(shí)現(xiàn)服務(wù)注冊(cè)與發(fā)現(xiàn)
基于pring-boot-starter-parent 2.6.8,pring-cloud-dependencies 2021.0.3,order服務(wù)和user服務(wù)
1.1 pom依賴
<!--服務(wù)注冊(cè)與發(fā)現(xiàn)--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2021.0.1.0</version> </dependency> <!--遠(yuǎn)程服務(wù)調(diào)用負(fù)載均衡--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-loadbalancer</artifactId> </dependency>
1.2 yaml配置
order服務(wù)application.yml
spring:
application:
name: orderservice
cloud:
#找對(duì)應(yīng)網(wǎng)段的網(wǎng)卡 不配置內(nèi)部服務(wù)就走外網(wǎng)
inetutils:
preferred-networks: 192.168.0
nacos:
discovery:
server-addr: 192.168.0.221:8848
user服務(wù)application.yml
spring:
application:
name: userservice
cloud:
#找對(duì)應(yīng)網(wǎng)段的網(wǎng)卡 不配置內(nèi)部服務(wù)就走外網(wǎng)
inetutils:
preferred-networks: 192.168.0
nacos:
discovery:
server-addr: 192.168.0.221:8848
1.3 添加啟動(dòng)注解
@EnableDiscoveryClient,需要注冊(cè)到Nacos的服務(wù)都需要添加
@SpringBootApplication
@EnableDiscoveryClient
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}1.4 啟動(dòng)服務(wù)查看控制臺(tái)
控制臺(tái)地址http://192.168.0.221:8848/nacos,賬號(hào)密碼都是nacos,查看服務(wù)列表

服務(wù)詳情圖

如果未配置preferred-networks,ip則顯示外網(wǎng)ip,也會(huì)用于服務(wù)調(diào)用
2.基于Nacos實(shí)現(xiàn)遠(yuǎn)程服務(wù)調(diào)用
2.1 客戶端創(chuàng)建RestTemplate Bean
@LoadBalanced // 開啟負(fù)載均衡策略
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
2.2 客戶端調(diào)用代碼
@Autowired
RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping("/test")
public String test() throws Exception {
//可以獲取到對(duì)應(yīng)服務(wù)的列表 服務(wù)名 ip 端口均可從這里面獲取到 也可以自己決定調(diào)用順序
List<ServiceInstance> instances = discoveryClient.getInstances("userservice");
//get方式調(diào)用
String template = restTemplate.getForObject("http://userservice/getTime/1123?name=jack", String.class);
Map<String, Object> resMap = new HashMap<>();
resMap.put("aaaa", "bbbb");
//post調(diào)用方式
RequestEntity<Map<String, Object>> requestEntity = RequestEntity
.post("http://userservice/postTime")
.contentType(MediaType.APPLICATION_JSON)
.body(resMap);
ResponseEntity<Map> responseEntity = restTemplate.exchange(requestEntity, Map.class);]
log.info("rest -- {}", template + ":" + responseEntity.getBody());
return template + ":" + responseEntity.getBody();
}
2.3 服務(wù)端暴露接口
@GetMapping("/getTime/{uuid}")
public String getTime(@PathVariable String uuid, @RequestParam String name) {
return new Date().getTime() + ":" + uuid + ":" + name;
}
@PostMapping("/postTime")
public Map<String, Object> getTime(@RequestBody Map<String, Object> params) {
params.put("time", new Date().getTime());
return params;
}2.4 服務(wù)調(diào)用測試
訪問客戶端調(diào)用接口,截圖如下

控制臺(tái)日志:
c.e.order.controller.OrderController : rest -- 1657182229010:1123:jack:{aaaa=bbbb, time=1657182229068}
在使用過程中發(fā)現(xiàn)想接收List<Map<String,Object>>太麻煩了,還是使用模板的遠(yuǎn)程調(diào)用openfeign了,下文分享。
到此這篇關(guān)于SpringCloud通過Nacos實(shí)現(xiàn)注冊(cè)中心與遠(yuǎn)程服務(wù)調(diào)用詳解流程的文章就介紹到這了,更多相關(guān)SpringCloud Nacos注冊(cè)中心內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis中模糊查詢使用CONCAT('%',#{str},'%')出錯(cuò)的解
這篇文章主要介紹了MyBatis中模糊查詢使用CONCAT('%',#{str},'%')出錯(cuò)的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
詳解關(guān)于eclipse中使用jdk15對(duì)應(yīng)javafx15的配置問題總結(jié)
這篇文章主要介紹了詳解關(guān)于eclipse中使用jdk15對(duì)應(yīng)javafx15的配置問題總結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11

