SpringBoot工程中整合Dubbo + Nacos
一、前言
- Dubbo:服务调用的RCP框架,测试版本:2.7.8
- Nacos : 服务注册中心,代替传统的服务注册中心zookeeper
- SpringBoot : 简化了Spring应用的开发
二、服务注册
添加依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.11.RELEASE</version>
<relativePath/>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.8</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
在 application.properties 中配置
# Spring boot application
spring.application.name = dubbo-provider-demo
server.port = 9998
management.port = 9988
# Base packages to scan Dubbo Components (e.g., @Service, @Reference)
dubbo.scan.basePackages = com.tbex.service
# Dubbo Config properties
## ApplicationConfig Bean
dubbo.application.id = dubbo-provider-demo
dubbo.application.name = dubbo-provider-demo
## ProtocolConfig Bean
dubbo.protocol.id = dubbo
dubbo.protocol.name = dubbo
dubbo.protocol.port = 20880
## RegistryConfig Bean
# dubbo.registry.id = my-registry
# dubbo.registry.address = N/A
#================ nacos registry====================
dubbo.registry.id = nacos
dubbo.registry.address = 127.0.0.1
dubbo.registry.port = 8848
dubbo.registry.protocol = nacos
创建服务接口
public interface HelloService {
public String sayHello(String name);
}
创建服务接口实现
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.Service;
@DubboService(
version = "1.0.0",
application = "${dubbo.application.id}",
protocol = "${dubbo.protocol.id}",
registry = "${dubbo.registry.id}"
)
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return "Hello:"+name;
}
}
启动SpringBoot工程 发布dubbo服务
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/69c7f17915aeb587afaf06d37803d1f5.png)
二、服务消费
添加依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
<relativePath/>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.8</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.tbex</groupId>
<artifactId>dubbo-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
在 application.properties 中配置
# Spring boot application
spring.application.name = dubbo-consumer-demo
server.port = 9999
management.port = 9989
# Dubbo Config properties
## ApplicationConfig Bean
dubbo.application.id = dubbo-consumer-demo
dubbo.application.name = dubbo-consumer-demo
#================ nacos registry====================
dubbo.registry.id = nacos
dubbo.registry.address = 127.0.0.1
dubbo.registry.port = 8848
dubbo.registry.protocol = nacos
Dubbo服务消费者
package com.tbex;
import com.tbex.service.HelloService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class DubboConsumerApplication {
@DubboReference(version = "1.0.0",
application = "${dubbo.application.id}",
registry = "${dubbo.registry.id}")
private HelloService helloService;
@RequestMapping("/sayHello/{name}")
public @ResponseBody
String sayHello(@PathVariable String name) {
return helloService.sayHello(name);
}
public static void main(String[] args) {
SpringApplication.run(DubboConsumerApplication.class, args);
}
}
启动SpringBoot工程 测试服务调用
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/72d4edfa16697855d16d278f1e750af9.png)