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/posts/20191018/
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。