Nacos默認已經集成了Ribbon,配合RestTemplate,非常容易就可以實現服務之間的訪問。
首先,按照上一篇文章的步驟,再新建一個一摸一樣的項目,端口是9001,主要是爲了測試nacos的負載均衡能力。當然,理論上來說只需要打成jar,部署在不同的機器上。但是這裏爲了簡便一點,我就新建了一個除端口不同其它都相同的項目。
建完之後,目錄如下:
分別啓動兩個項目,在nacos的web管理頁面中可以發現已經有兩個服務註冊進去了。
新建一個調用方項目,通過負載均衡調用9000和9001。
創建consumer-order83。
添加pom依賴
<dependencies>
<!-- SpringCloud ailibaba nacos-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--監控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--熱部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
resources目下添加application.yml文件
server: port: 83 spring: application: name: nacos-order-consumer cloud: nacos: discovery: server-addr: localhost:8848 #配置Nacos地址 #消費者將去訪問的微服務名稱(註冊成功進nacos的微服務提供者) service-url: nacos-user-service: http://nacos-payment-provider
按照如圖所示,分別新建這幾個類
添加項目的啓動類:ConsumerOrderMain
package com.xhc.cloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class ConsumerOrderMain { public static void main(String[] args) { SpringApplication.run(ConsumerOrderMain.class,args); } }
添加ApplicationContextConfig類
package com.xhc.cloud.config; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class ApplicationContextConfig { @Bean @LoadBalanced public RestTemplate getRestTemplate() { return new RestTemplate(); } }
添加OrderNacosController類
package com.xhc.cloud.controller; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; @RestController @Slf4j public class OrderNacosController { @Resource private RestTemplate restTemplate; @Value("${service-url.nacos-user-service}") private String serverURL; @GetMapping(value = "/consumer/payment/nacos/{id}") public String paymentInfo(@PathVariable("id") Integer id) { return restTemplate.getForObject(serverURL + "/payment/nacos/" + id,String.class); } }
在9000和9001項目中新建PaymentController類
package com.xhc.cloud.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class PaymentController { @Value("${server.port}") private String serverPort; @GetMapping(value = "/payment/nacos/{id}") public String getPayment(@PathVariable("id") Integer id) { return "Hello Nacos Discovery xhc: " + serverPort + "\t id: " + id; } }
全部新建完畢後分別啓動項目,訪問:http://localhost:83/consumer/payment/nacos/1。
每次訪問會發現請求的是不同的項目。