最近需要用到springboot、nacos、dubbo的整合,自己试着搭建了一下,过程中也遇到了很多问题。问题都在其他文章里的,这篇文章直奔目的地。
主要分4个模块
模块1:nacos
- nacos主要就是下载nacos服务压缩包,然后解压,用cmd运行就可以了。步骤如下:
- 下载nacos服务, github下载很慢,附上一个网盘地址:
链接:https://pan.baidu.com/s/1DnxsSagf0lZKBsumaq4nXQ
提取码:thwq
-
解压:
打开cmd,进入nacos的bin目录,执行startup.cmd:
……\nacos\bin\startup.cmd
-
nacos服务即启动成功:
模块2:api接口模块
创建一个普通项目,新建接口(准备被调用的接口),代码如下:
public interface DemoService {
String hello();
}
然后将该项目打成jar包,准备被分别引用到dubbo的provider和consumer中。
模块3:服务提供者provider模块
- 创建一个项目,引入下列jar包:
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.8</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>2.7.4.1</version>
</dependency>
<dependency>
<groupId>com.bruce</groupId>
<artifactId>provider-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
其中provider-api即模块2的接口模块。
- 新建配置文件bootstrap.yml,内容如下:
server:
port: 8001 # 项目端口
spring:
application:
name: provider-server # 项目名称
cloud:
nacos: # nacos配置
config:
server-addr: 127.0.0.1:8848 # nacos地址和端口
dubbo: # dubbo配置
application:
name: provider-server # dubbo服务名称
protocol:
port: 20880 # dubbo服务端口
name: dubbo # dubbo服务协议名称
registry: # dubbo使用的注册中心
address: nacos://10.10.8.165:8848 # dubbo注册中心地址
monitor:
protocol: registry
scan:
base-packages: com.bruce.provider.service
- 启动类上的注解如下:
@CrossOrigin//允许跨越访问
@EnableDubbo
@EnableDiscoveryClient
@SpringBootApplication(scanBasePackages = "com.bruce")
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
4 实现api模块的接口:
@Service(interfaceClass = DemoService.class)
@Component
public class DemoServiceImpl implements DemoService {
@Override
public String hello() {
return "hello";
}
}
模块4:服务消费者consumer模块
- 创建一个项目,引入下列jar包:
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.8</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>2.7.4.1</version>
</dependency>
<dependency>
<groupId>com.bruce</groupId>
<artifactId>provider-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
其中provider-api即模块2的接口模块。
- 新建配置文件bootstrap.yml,内容如下:
server:
port: 8011
spring:
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848
dubbo:
application:
name: consumer-service
registry:
address: nacos://127.0.0.1:8848
protocol:
name: dubbo
scan:
base-packages: com.bruce.consumer.controller
qos:
port: 22223
consumer:
check: false
- 启动类上的注解如下:
@CrossOrigin//允许跨越访问
@EnableDubbo
@EnableDiscoveryClient
@SpringBootApplication(scanBasePackages = "com.bruce")
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
4 调用provider提供的接口:
@RestController
@RequestMapping("/demo")
public class DemoController {
static final String demoUrl = "dubbo://127.0.0.1:20880";
@Reference(url = demoUrl)
DemoService demoService;
@RequestMapping("/hello")
public String hello() {
return demoService.hello();
}
}
这样就全部配置好啦。启动provider和consumer,然后使用postman请求consumer暴露的http接口进行测试即可: