文章目錄
- 建立服務提供者項目[demo-spring-cloud-nacos-service-provider]
-
- 項目源碼位址
- 項目結構
- 引入jar包
-
- 父類包
- 服務發現包和 web項目
- 配置檔案:application.yml
- Controller服務:NacosServiceProviderController
- 啟動類:NacosServiceProviderApp
- 啟動後驗證
-
- 運作啟動類檢視nacos
- 通路服務
- 建立服務消費者項目[demo-spring-cloud-nacos-service-consumer]
-
- 源碼位址
- 項目結構
- 引入jar包
-
- 父類包
- 服務發現包和 web項目
- 涉及到用戶端負載的開啟 @LoadBalanced
- Controller服務:NacosServiceProviderController
- 啟動類:NacosServiceConsumerApp
- 驗證
-
- nacos用戶端
- 從consumer去調用provider
建立服務提供者項目[demo-spring-cloud-nacos-service-provider]
項目源碼位址
https://gitee.com/gaoxinfu_admin/demo-spring-cloud/tree/master/demo-spring-cloud-nacos-service-provider
項目結構
引入jar包
父類包
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
服務發現包和 web項目
需要提供給外部服務的調用,是以需要引用web包
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
配置檔案:application.yml
主要配置端口,項目名稱,nacos位址
server:
port: 8079
spring:
application:
name: nacos-service-provider
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
Controller服務:NacosServiceProviderController
package com.gaoxinfu.demo.spring.cloud;
import org.springframework.web.bind.annotation.*;
/**
* @Description:
* @Author: gaoxinfu
* @Date: 2020-10-19 17:33
*/
@RestController
public class NacosServiceProviderController {
@RequestMapping(value= "/findName/{name}",method = RequestMethod.GET)
public String findName(@PathVariable String name){
return "Nacos Service Provider:歡迎 "+name+" 通路";
}
}
啟動類:NacosServiceProviderApp
package com.gaoxinfu.demo.spring.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class NacosServiceProviderApp
{
public static void main( String[] args )
{
SpringApplication.run(NacosServiceProviderApp.class,args);
}
}
啟動後驗證
運作啟動類檢視nacos
通路服務
http://127.0.0.1:8079/findName/gaoxinfu
綜上,服務端已經能夠正常通路,并注冊到nacos上
建立服務消費者項目[demo-spring-cloud-nacos-service-consumer]
源碼位址
https://gitee.com/gaoxinfu_admin/demo-spring-cloud/tree/master/demo-spring-cloud-nacos-service-consumer
項目結構
引入jar包
父類包
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
服務發現包和 web項目
需要提供給外部服務的調用,是以需要引用web包
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
涉及到用戶端負載的開啟 @LoadBalanced
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
</dependency>
Controller服務:NacosServiceProviderController
package com.gaoxinfu.demo.spring.cloud;
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;
/**
* @Description:
* @Author: gaoxinfu
* @Date: 2020-10-20 10:17
*/
@RestController
public class NacosServiceConsumerContoller {
@Resource
private RestTemplate restTemplate;
@GetMapping("/findName/{name}")
public String findName(@PathVariable String name){
return restTemplate.getForObject("http://nacos-service-provider/findName/"+name,String.class);
}
}
啟動類:NacosServiceConsumerApp
package com.gaoxinfu.demo.spring.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
*
*/
@SpringBootApplication
@EnableDiscoveryClient
public class NacosServiceConsumerApp {
@LoadBalanced
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main( String[] args ){
SpringApplication.run(NacosServiceConsumerApp.class,args);
}
}
驗證
nacos用戶端
從consumer去調用provider
http://127.0.0.1:8080/findName/gaoxinfu