目录
注意事项
参考资源
Nacos配置中心
初始化项目
进行Nacos相关配置
运行
Nacos注册中心
dubbo方式
对外暴露接口dubbo-api
服务提供者dubbo-provider
服务消费者dubbo-consumer
负载均衡客户端方法
服务提供者
服务消费者
不管是使用Nacos配置中心,还是使用Nacos注册中心,大家一定要注意SpringCloudAlibaba和SpringBoot的版本对应关系,以免实际开发过程中出现各种莫名其妙的问题。下图供大家参考(非最新的对应关系)
Nacos环境准备参考官网链接:Nacos 快速开始 | Nacos
Nacos配置中心和注册中心底层原理实现分析可参考如下链接:
SpringCloudAlibaba系列之Nacos配置管理
SpringCloudAlibaba系列之Nacos服务注册与发现
示例代码源码参考地址:https://gitee.com/williamjava/spring_cloud_alibaba_practise
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-config
org.springframework.cloud
spring-cloud-dependencies
Hoxton.SR4
pom
import
com.alibaba.cloud
spring-cloud-alibaba-dependencies
2.2.1.RELEASE
pom
import
spring:
application:
name: alibaba-nacos-config-client
cloud:
nacos:
config:
server-addr: localhost:8848
file-extension: yaml
server:
port: 8604
package com.gui.alibaba.nacos.config.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 注解:RefreshScope
* 主要用来让这个类下的配置内容支持动态刷新,也就是当我们的应用启动之后,修改了Nacos中的配置内容之后,这里也会马上生效
*/
@Slf4j
@RestController
@RefreshScope
public class TestController {
@Value("${practise.title:本地默认内容}")
private String title;
@GetMapping("/test")
public String hello() {
return title;
}
}
Nacos控制台增加配置文件,配置文件名称(Data ID)与spring.application.name保持一致,具体内容如下:
启动项目,浏览器访问:http://localhost:8604/test
我们得到了配置中心中配置的内容,如果Nacos控制台直接修改配置的内容,刷新页面能实时获取到最新的数据,起关键作用的是@RefreshScope注解,可动态刷新Nacos配置。
package com.sc.ali.dubbo.api;
public interface HelloService {
String hello(String name);
}
com.alibaba.cloud
spring-cloud-starter-dubbo
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
com.ali.sc.dubbo.api
dubbo-api
1.0-SNAPSHOT
org.springframework.cloud
spring-cloud-dependencies
Hoxton.SR4
pom
import
com.alibaba.cloud
spring-cloud-alibaba-dependencies
2.2.1.RELEASE
pom
import
spring:
application:
name: dubbo-provider
cloud:
nacos:
discovery:
#nacos配置中心地址
server-addr: localhost:8848
main:
allow-bean-definition-overriding: true
server:
port: 8090
dubbo:
scan:
#dubbo扫描包路径
base-packages: com.sc.ali.dubbo.provider.service
protocol:
name: dubbo
port: -1
registry:
address: spring-cloud://localhost
package com.sc.ali.dubbo.provider.service;
import com.sc.ali.dubbo.api.HelloService;
import org.apache.dubbo.config.annotation.Service;
@Service
public class HelloServiceImpl implements HelloService {
@Override
public String hello(String s) {
return "Hello, " + s + ",welcome....";
}
}
Nacos工作台服务列表中可看到服务提供者对应的服务。
com.alibaba.cloud
spring-cloud-starter-dubbo
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
com.ali.sc.dubbo.api
dubbo-api
1.0-SNAPSHOT
org.springframework.cloud
spring-cloud-dependencies
Hoxton.SR4
pom
import
com.alibaba.cloud
spring-cloud-alibaba-dependencies
2.2.1.RELEASE
pom
import
spring:
application:
name: dubbo-consumer
cloud:
nacos:
discovery:
#Nacos服务注册中心地址
server-addr: localhost:8848
main:
allow-bean-definition-overriding: true
server:
port: 8091
dubbo:
protocol:
name: dubbo
port: -1
registry:
address: spring-cloud://localhost
cloud:
#指定需要订阅的服务
subscribed-services: dubbo-provider
package com.sc.ali.dubbo.consumer.controller;
import com.sc.ali.dubbo.api.HelloService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController{
@Reference
private HelloService helloService;
@GetMapping("/hello")
public String hello(String name) {
return helloService.hello(name);
}
}
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
org.springframework.cloud
spring-cloud-dependencies
Hoxton.SR4
pom
import
com.alibaba.cloud
spring-cloud-alibaba-dependencies
2.2.1.RELEASE
pom
import
spring:
application:
name: alibaba-nacos-discovery-server
cloud:
nacos:
#Nacos注册中心地址
server-addr: localhost:8848
server:
port: 8601
package com.gui.alibaba.nacos.discovery.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Slf4j
public class TestController {
@GetMapping("/hello")
public String hello(@RequestParam String name) {
log.info("invoked name = " + name);
return "hello " + name;
}
}
Nacos工作台服务列表可找到对应的服务
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
org.springframework.cloud
spring-cloud-dependencies
Hoxton.SR4
pom
import
com.alibaba.cloud
spring-cloud-alibaba-dependencies
2.2.1.RELEASE
pom
import
spring:
application:
name: alibaba-nacos-discovery-client
cloud:
nacos:
discovery:
server-addr: localhost:8848
server:
port: 8602
package com.gui.alibaba.nacos.client.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@Slf4j
public class TestController {
@Autowired
private LoadBalancerClient loadBalancerClient;
@GetMapping("/test")
public String test() {
// 通过spring cloud common中的负载均衡接口选取服务提供节点实现接口调用
ServiceInstance serviceInstance = loadBalancerClient.choose("alibaba-nacos-discovery-server");
//构造请求URL
String url = serviceInstance.getUri() + "/hello?name=" + "william";
//创建RestTemplate对象,通过Restful接口调用具体服务
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject(url, String.class);
return "Invoke : " + url + ", return : " + result;
}
}