SpringCloud通過Nacos實現(xiàn)注冊中心與遠程服務調用詳解流程
本文主要記錄基于Nacos實現(xiàn)服務注冊中心和遠程服務調用
1. 基于Nacos實現(xiàn)服務注冊與發(fā)現(xiàn)
基于pring-boot-starter-parent 2.6.8,pring-cloud-dependencies 2021.0.3
,order服務和user服務
1.1 pom依賴
<!--服務注冊與發(fā)現(xiàn)--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2021.0.1.0</version> </dependency> <!--遠程服務調用負載均衡--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-loadbalancer</artifactId> </dependency>
1.2 yaml配置
order服務application.yml
spring:
application:
name: orderservice
cloud:
#找對應網(wǎng)段的網(wǎng)卡 不配置內部服務就走外網(wǎng)
inetutils:
preferred-networks: 192.168.0
nacos:
discovery:
server-addr: 192.168.0.221:8848
user服務application.yml
spring:
application:
name: userservice
cloud:
#找對應網(wǎng)段的網(wǎng)卡 不配置內部服務就走外網(wǎng)
inetutils:
preferred-networks: 192.168.0
nacos:
discovery:
server-addr: 192.168.0.221:8848
1.3 添加啟動注解
@EnableDiscoveryClient
,需要注冊到Nacos的服務都需要添加
@SpringBootApplication @EnableDiscoveryClient public class OrderApplication { public static void main(String[] args) { SpringApplication.run(OrderApplication.class, args); } }
1.4 啟動服務查看控制臺
控制臺地址http://192.168.0.221:8848/nacos
,賬號密碼都是nacos
,查看服務列表
服務詳情圖
如果未配置preferred-networks
,ip則顯示外網(wǎng)ip,也會用于服務調用
2.基于Nacos實現(xiàn)遠程服務調用
2.1 客戶端創(chuàng)建RestTemplate Bean
@LoadBalanced // 開啟負載均衡策略 @Bean public RestTemplate restTemplate() { return new RestTemplate(); }
2.2 客戶端調用代碼
@Autowired RestTemplate restTemplate; @Autowired private DiscoveryClient discoveryClient; @GetMapping("/test") public String test() throws Exception { //可以獲取到對應服務的列表 服務名 ip 端口均可從這里面獲取到 也可以自己決定調用順序 List<ServiceInstance> instances = discoveryClient.getInstances("userservice"); //get方式調用 String template = restTemplate.getForObject("http://userservice/getTime/1123?name=jack", String.class); Map<String, Object> resMap = new HashMap<>(); resMap.put("aaaa", "bbbb"); //post調用方式 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 服務端暴露接口
@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 服務調用測試
訪問客戶端調用接口,截圖如下
控制臺日志:
c.e.order.controller.OrderController : rest -- 1657182229010:1123:jack:{aaaa=bbbb, time=1657182229068}
在使用過程中發(fā)現(xiàn)想接收List<Map<String,Object>>
太麻煩了,還是使用模板的遠程調用openfeign
了,下文分享。
到此這篇關于SpringCloud通過Nacos實現(xiàn)注冊中心與遠程服務調用詳解流程的文章就介紹到這了,更多相關SpringCloud Nacos注冊中心內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
MyBatis中模糊查詢使用CONCAT('%',#{str},'%')出錯的解
這篇文章主要介紹了MyBatis中模糊查詢使用CONCAT('%',#{str},'%')出錯的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01詳解關于eclipse中使用jdk15對應javafx15的配置問題總結
這篇文章主要介紹了詳解關于eclipse中使用jdk15對應javafx15的配置問題總結,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11