Nacos 作為服務注冊中心,可以快速簡單的將服務自動注冊到 Nacos 服務端,並且能夠動態無感知的刷新某個服務實例的服務列表,為分布式系統提供服務注冊與發現功能
一、創建服務
1、創建項目
pom.xml
中添加nacos
支持
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>0.9.0.RELEASE</version>
</dependency>
bootstrap.properties
加入nacos
參數
server.port=8080
spring.application.name=service-provider
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
#如果不想 Nacos 注冊和發現當前服務,可設置為false
spring.cloud.nacos.discovery=true
spring.cloud.nacos.discovery.server-addr
:為nacos
的服務器地址:
端口,80端口不可省略
通過 Spring Cloud 原生注解 @EnableDiscoveryClient
開啟服務注冊發現功能:
package com.ichochy.nacos;
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.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
public class NacosApplication {
public static void main(String[] args) {
SpringApplication.run(NacosApplication.class, args);
}
}
2、編寫服務
package com.ichochy.nacos.service;
import com.ichochy.nacos.service.ServiceInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@RequestMapping("/service")
public class EchoService {
@RequestMapping("echo/{name}")
public String echo(@PathVariable String name){
System.out.println("##############");
return "Hello Nacos Discovery "+name;
}
}
3、查看服務
啟動項目,可以在Nacos
的服務列表中查看到當前服務
訪問地址:http://127.0.0.1:8080/service/echo/ichochy
二、調用服務
1、創建項目
創建項目和服務方項目相同,server.port
和spring.application.name
設置為不同
構建RestTemplate
,@LoadBalanced
開啟負載均衡
package com.ichochy.nacos;
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.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
public class NacosApplication {
public static void main(String[] args) {
SpringApplication.run(NacosApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
}
2、編寫遠程服務調用
package com.ichochy.nacos.controller;
import com.ichochy.nacos.service.ServiceInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@RequestMapping("/result")
public class ResultController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping("restTemplate/{name}")
public String restTemplate(@PathVariable String name){
String url = "http://service-provider/service/echo/";
return restTemplate.getForObject(url+name,String.class);
}
}
注:通過RestTemplate
遠程調用服務,遠程調用地址為服務方項目名+請求路徑
spring.application.name=service-provider
3、遠程調用
啟動項目,測試服務調用
訪問地址:http://127.0.0.1:8080/result/restTemplate/ichochy
三、優化調用,使用FeignClient
pom.xml
中添加FeignClient
支持
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
1、通過 Spring Cloud 原生注解 @EnableFeignClients
開啟
package com.ichochy.nacos;
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.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class NacosApplication {
public static void main(String[] args) {
SpringApplication.run(NacosApplication.class, args);
}
}
2、編寫遠程服務調用接口
package com.ichochy.nacos.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@FeignClient(value = "service-provider")
public interface ServiceInterface {
@RequestMapping("/service/echo/{name}")
public String echo(@PathVariable String name);
}
3、遠程調用服務
package com.ichochy.nacos.controller;
import com.ichochy.nacos.service.ServiceInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@RequestMapping("/result")
public class ResultController {
@Autowired
private ServiceInterface serviceInterface;
@RequestMapping("feignClient/{name}")
public String feignClient(@PathVariable String name){
return serviceInterface.echo(name);
}
}
訪問地址:http://127.0.0.1:8080/result/feignClient/ichochy
聯系方式:
郵箱:iChochy@qq.com
網站:https://www.ichochy.com
源文:https://www.ichochy.com/blog/2019/10/18/Spring-Cloud-Alibaba-Nacos-Discovery-實戰.html