目录
不管是使用Nacos配置中心,还是使用Nacos注册中心,大家一定要注意SpringCloudAlibaba和SpringBoot的版本对应关系,以免实际开发过程中出现各种莫名其妙的问题。下图供大家参考(非最新的对应关系)
Nacos环境准备参考官网链接:Nacos 快速开始 | Nacos?
Nacos配置中心和注册中心底层原理实现分析可参考如下链接:
SpringCloudAlibaba系列之Nacos配置管理
SpringCloudAlibaba系列之Nacos服务注册与发现
示例代码源码参考地址:https://gitee.com/williamjava/spring_cloud_alibaba_practise?
<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>
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);
}
<!-- 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>
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工作台服务列表中可看到服务提供者对应的服务。
<!-- 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>
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);
}
}
<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>
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工作台服务列表可找到对应的服务
<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>
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;
}
}