目录
注意事项
参考资源
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
Nacos配置中心
初始化项目
- 核心maven依赖
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Hoxton.SR4</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>2.2.1.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>
- 增加项目中配置文件yml
spring:application:name: alibaba-nacos-config-clientcloud:nacos:config:server-addr: localhost:8848file-extension: yamlserver:port: 8604
- 创建测试Controller
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相关配置
Nacos控制台增加配置文件,配置文件名称(Data ID)与spring.application.name保持一致,具体内容如下:
运行
启动项目,浏览器访问:http://localhost:8604/test
我们得到了配置中心中配置的内容,如果Nacos控制台直接修改配置的内容,刷新页面能实时获取到最新的数据,起关键作用的是@RefreshScope注解,可动态刷新Nacos配置。
Nacos注册中心
dubbo方式
对外暴露接口dubbo-api
package com.sc.ali.dubbo.api;public interface HelloService {String hello(String name);
}
服务提供者dubbo-provider
- 核心maven依赖
<!-- Dubbo Spring Cloud Starter --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-dubbo</artifactId></dependency><!-- Spring Cloud Nacos Service Discovery --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><!--API--><dependency><groupId>com.ali.sc.dubbo.api</groupId><artifactId>dubbo-api</artifactId><version>1.0-SNAPSHOT</version></dependency><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Hoxton.SR4</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>2.2.1.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>
- 项目中配置文件yml
spring:application:name: dubbo-providercloud:nacos:discovery:#nacos配置中心地址server-addr: localhost:8848main:allow-bean-definition-overriding: trueserver:port: 8090
dubbo:scan:#dubbo扫描包路径base-packages: com.sc.ali.dubbo.provider.serviceprotocol:name: dubboport: -1registry: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 {@Overridepublic String hello(String s) {return "Hello, " + s + ",welcome....";}
}
- 启动服务提供者
Nacos工作台服务列表中可看到服务提供者对应的服务。
服务消费者dubbo-consumer
- 核心maven依赖
<!-- Dubbo Spring Cloud Starter --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-dubbo</artifactId></dependency><!-- Spring Cloud Nacos Service Discovery --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><!--API--><dependency><groupId>com.ali.sc.dubbo.api</groupId><artifactId>dubbo-api</artifactId><version>1.0-SNAPSHOT</version></dependency><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Hoxton.SR4</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>2.2.1.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>
- 项目中配置文件yml
spring:application:name: dubbo-consumercloud:nacos:discovery:#Nacos服务注册中心地址server-addr: localhost:8848main:allow-bean-definition-overriding: trueserver:port: 8091
dubbo:protocol:name: dubboport: -1registry:address: spring-cloud://localhostcloud:#指定需要订阅的服务subscribed-services: dubbo-provider
- 添加测试controller,完成服务调用
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{@Referenceprivate HelloService helloService;@GetMapping("/hello")public String hello(String name) {return helloService.hello(name);}
}
- 运行输出结果
负载均衡客户端方法
服务提供者
- 核心maven依赖
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Hoxton.SR4</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>2.2.1.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>
- 项目中配置文件yml
spring:application:name: alibaba-nacos-discovery-servercloud:nacos:#Nacos注册中心地址server-addr: localhost:8848server: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工作台服务列表可找到对应的服务
服务消费者
- 核心maven依赖
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Hoxton.SR4</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>2.2.1.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>
- 项目中配置文件yml
spring:application:name: alibaba-nacos-discovery-clientcloud:nacos:discovery:server-addr: localhost:8848server:port: 8602
- 添加测试controller,完成服务调用
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 {@Autowiredprivate LoadBalancerClient loadBalancerClient;@GetMapping("/test")public String test() {// 通过spring cloud common中的负载均衡接口选取服务提供节点实现接口调用ServiceInstance serviceInstance = loadBalancerClient.choose("alibaba-nacos-discovery-server");//构造请求URLString url = serviceInstance.getUri() + "/hello?name=" + "william";//创建RestTemplate对象,通过Restful接口调用具体服务RestTemplate restTemplate = new RestTemplate();String result = restTemplate.getForObject(url, String.class);return "Invoke : " + url + ", return : " + result;}
}
- 运行输出结果