本文将介绍如何基于Spring Boot 2.x的版本,通过Nacos作为配置与注册中心,实现Dubbo服务的注册与消费。
整合组件的版本说明:
Spring Boot 2.1.9
Dubbo 2.7.3
Nacos 1.1.3
本文的亮点:
源代码放置Github: https://github.com/raysonfang/spring-boot-demo-all
之前公司在使用Dubbo 2.6.1的时候,采用Zookeeper作为注册中心。当时,也只是仅仅拿来作为注册中心使用,一没有专门的管理后台进行可视化管理操作,二是功能单一,仅作为注册中心使用。
经过一段时间的学习和了解以后,发现采用阿里开源的Nacos作为注册中心与外部配置中心。它比Zookeeper更适合做服务的注册与配置,毕竟是大厂开源,经过大量实践。
如果不清楚Nacos是什么,或具有什么主要功能以及架构设计思想。自行花点时间查一下资料。
Nacos:
注:此次主要实践Nacos作为注册中心,后面会单独整合Nacos作为配置中心的实践分享。
使用idea+maven多模块进行项目搭建
**spring-boot-dubbo-nacos-demo:**父工程
shop-service-provider: dubbo服务提供者
shop-service-consumer: dubbo服务消费者,是一个web工程
**spring-boot-dubbo-nacos-demo:**父工程的pom.xml
4.0.0
cn.raysonblog
misco-dubbo
0.0.1-SNAPSHOT
misco-dubbo
pom
Demo project for Spring Boot Dubbo Nacos
shop-service-provider
shop-service-consumer
1.8
2.1.9.RELEASE
2.7.3
org.springframework.boot
spring-boot-dependencies
${spring-boot.version}
pom
import
org.apache.dubbo
dubbo-dependencies-bom
${dubbo.version}
pom
import
org.apache.dubbo
dubbo-spring-boot-starter
${dubbo.version}
org.apache.dubbo
dubbo
${dubbo.version}
org.springframework
spring
javax.servlet
servlet-api
log4j
log4j
apache.snapshots.https
Apache Development Snapshot Repository
https://repository.apache.org/content/repositories/snapshots
false
true
org.springframework.boot
spring-boot-maven-plugin
shop-service-provider: pom.xml引入dubbo与nacos相关的jar
4.0.0
cn.raysonblog
misco-dubbo
0.0.1-SNAPSHOT
../pom.xml
cn.raysonblog
shop-service-provider
0.0.1-SNAPSHOT
shop-service-provider
服务者 Demo project for Spring Boot dubbo nacos
1.8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-logging
org.springframework.boot
spring-boot-starter-log4j2
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
org.apache.dubbo
dubbo-spring-boot-starter
org.apache.dubbo
dubbo
org.apache.dubbo
dubbo-registry-nacos
2.7.3
com.alibaba.nacos
nacos-client
apache.snapshots.https
Apache Development Snapshot Repository
https://repository.apache.org/content/repositories/snapshots
false
true
org.springframework.boot
spring-boot-maven-plugin
shop-service-consumer: pom.xml
4.0.0
cn.raysonblog
misco-dubbo
0.0.1-SNAPSHOT
../pom.xml
cn.raysonblog
shop-service-consumer
0.0.1-SNAPSHOT
shop-service-consumer
Demo project for Spring Boot dubbo nacos
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-logging
org.springframework.boot
spring-boot-starter-log4j2
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
org.apache.dubbo
dubbo-spring-boot-starter
org.apache.dubbo
dubbo
org.apache.dubbo
dubbo-registry-nacos
2.7.3
com.alibaba.nacos
nacos-spring-context
com.alibaba.nacos
nacos-client
cn.raysonblog
shop-service-provider
0.0.1-SNAPSHOT
apache.snapshots.https
Apache Development Snapshot Repository
https://repository.apache.org/content/repositories/snapshots
false
true
org.springframework.boot
spring-boot-maven-plugin
网上大部分资料都是基于application.properties配置,或者是基于xml配置dubbo的相关参数。而实际上,在Spring Boot工程中,
**shop-service-provider:**application.yml配置文件说明
spring:
application:
name: shop-service-provider
# log config
logging:
config: classpath:log4j2.xml
level:
root: info
web: info
file: logs/shop-service-provider.log
# Dubbo Application nacos
## The default value of dubbo.application.name is ${spring.application.name}
## dubbo.application.name=${spring.application.name}
nacos:
service-address: 127.0.0.1
port: 8848
dubbo:
registry:
address: nacos://${nacos.service-address}:${nacos.port}
protocol:
name: dubbo
port: 20881
scan:
base-packages: cn.raysonblog.*.service.impl
shop-service-consumer: application.yml说明
server:
address:
port: 8081
servlet:
context-path: /
tomcat:
uri-encoding: UTF-8
spring:
application:
name: shop-service-consumer
# log config
logging:
config: classpath:log4j2.xml
level:
root: info
web: info
file: logs/shop-service-provider.log
# Dubbo Application nacos
## The default value of dubbo.application.name is ${spring.application.name}
## dubbo.application.name=${spring.application.name}
nacos:
service-address: 127.0.0.1
port: 8848
dubbo:
registry:
address: nacos://${nacos.service-address}:${nacos.port}
dubbo相关参数:也可以在DubboConfigurationProperties
类中查看
#####**shop-service-provider:**代码实现
针对dubbo服务提供者,我没有单独把RpcShopService
接口单独放到一个子模块提供,建议在引用到实际项目中,可以单独提供接口包,在消费端直接引用接口包,这样就可以脱离服务提供者。
ShopServiceProviderApplication.java
启动主类
package cn.raysonblog.shopserviceprovider;
import org.apache.dubbo.config.spring.context.annotation.DubboConfigConfiguration;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.util.concurrent.CountDownLatch;
/**
* dubbo 服务提供方
* @author raysonfang
* @公众号 Java技术干货(ID:raysonfang)
*/
@SpringBootApplication
@EnableDubbo
public class ShopServiceProviderApplication {
//使用jar方式打包的启动方式
private static CountDownLatch countDownLatch = new CountDownLatch(1);
public static void main(String[] args) throws InterruptedException{
SpringApplication.run(ShopServiceProviderApplication.class, args).registerShutdownHook();
countDownLatch.await();
}
}
需要注意@EnableDubbo
这个注解,是开启Dubbo服务必要的。
RpcShopService.java
:暴露接口,供消费端使用
package cn.raysonblog.shopserviceprovider.service;
/**
* 提供暴露的Rpc接口
* @author raysonfang
*/
public interface RpcShopService {
String sayHello(String name);
}
ShopServiceImpl.java
: 实现类
package cn.raysonblog.shopserviceprovider.service.impl;
import cn.raysonblog.shopserviceprovider.service.RpcShopService;
import org.apache.dubbo.config.annotation.Service;
/**
* 接口实现类
*
* ## @Service 这个注解是使用dubbo提供的,
* 这个注解中有很多属性,需要单独了解去进行配置
*
* @author raysonfang
*/
@Service
public class ShopServiceImpl implements RpcShopService {
public String sayHello(String name) {
return name;
}
}
#####shop-service-consumer: 代码实现
package cn.raysonblog.shopserviceconsumer;
import cn.raysonblog.shopserviceprovider.service.RpcShopService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
*
* 把主类和controller写在一起,方便简单测试演示。
*
* @author raysonfang
*/
@SpringBootApplication
@RestController
public class ShopServiceConsumerApplication {
@Reference
RpcShopService shopService;
@RequestMapping(name = "/sayHello", method = RequestMethod.GET)
public String sayHello(){
return shopService.sayHello("Hello Dubbo Nacos!更多原创分享,技术交流,关注:Java技术干货(ID:raysonfang)");
}
public static void main(String[] args) {
SpringApplication.run(ShopServiceConsumerApplication.class, args);
}
}
下载nacos-server:https://github.com/alibaba/nacos/releases
在浏览器中输入:http://localhost:8848/nacos/index.html
, 便可以访问到nacos的控制台。用户名和密码默认为nacos
在浏览器端输入:http://localhost:8081/sayHello, 便会返回结果。
###7、问题记录及解决
解决:去maven的本地依赖库中,删除引入不成功的依赖包,在重新reimport。
@EnableDubboConfig
。解决: 换成使用@EnableDubbo
。
解决:通过查看DubboConfigurationProperties.java
源码,去分析属性配置。
###8、后记
由于能力有限,若有错误或者不当之处,还请大家批评指正,一起学习交流!
源代码放置Github: https://github.com/raysonfang/spring-boot-demo-all
欢迎大家star, 批评
我平常学习,编码也都会放置github上,欢迎持续关注交流。
我的github: https://github.com/raysonfang