NACOS注册中心
- 从github下载最新版本的nacos
- 上传至服务器并解压
- 单机启动
sh startup.sh -m standalone
- nacos 控制台访问地址
http://192.168.136.129:8848/nacos
,使用账号nacos/nacos登录并访问
项目框架
本次案例包含三个组件
- 公共接口层 dubbo-api
- 生产者 dubbo-provider
- 消费者 dubbo-consumer 代码目录如下:
父目录dubbo-demo
父目录主要是定义公共组件依赖,版本号,pom
文件如下
<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><packaging>pom</packaging><modules><module>dubbo-api</module><module>dubbo-provider</module><module>dubbo-consumer</module></modules><groupId>com.jianzh5</groupId><artifactId>dubbo-demo</artifactId><version>1.0-SNAPSHOT</version><properties><java.version>1.8</java.version><netty-all.version>4.0.35.Final</netty-all.version><spring-boot.version>2.1.9.RELEASE</spring-boot.version><dubbo.version>2.7.3</dubbo.version><nacos-client.version>1.1.4</nacos-client.version></properties><dependencyManagement><dependencies><!--SpringBoot--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope></dependency><!--ApacheDubbo--><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-dependencies-bom</artifactId><version>${dubbo.version}</version><type>pom</type><scope>import</scope></dependency><!--DubboRegistryNacos--><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-registry-nacos</artifactId><version>${dubbo.version}</version></dependency><dependency><groupId>com.alibaba.nacos</groupId><artifactId>nacos-client</artifactId><version>${nacos-client.version}</version></dependency><!--DubboSpringBootStarter--><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-spring-boot-starter</artifactId><version>${dubbo.version}</version></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo</artifactId><version>${dubbo.version}</version><exclusions><exclusion><groupId>org.springframework</groupId><artifactId>spring</artifactId></exclusion><exclusion><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId></exclusion><exclusion><groupId>log4j</groupId><artifactId>log4j</artifactId></exclusion></exclusions></dependency><!--接口--><dependency><groupId>com.jianzh5</groupId><artifactId>dubbo-api</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>${spring-boot.version}</version><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins></build></project>
在pom文件中直接依赖接口,这样生产者和消费者都可以使用了
接口层dubbo-api定义
pom
<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>dubbo-demo</artifactId><groupId>com.jianzh5</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><groupId>com.jianzh5</groupId><artifactId>dubbo-api</artifactId><packaging>jar</packaging></project>
定义真实的接口
/*** @author jianzh5* @date 2019/11/5 10:45*/publicinterfaceHelloService { StringsayHello(); }
生产者dubbo-provider
pom
<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>dubbo-demo</artifactId><groupId>com.jianzh5</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><groupId>com.jianzh5</groupId><artifactId>dubbo-provider</artifactId><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><!--Dubbo--><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-spring-boot-starter</artifactId></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo</artifactId></dependency><!--DubboRegistryNacos--><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-registry-nacos</artifactId></dependency><dependency><groupId>com.alibaba.nacos</groupId><artifactId>nacos-client</artifactId></dependency><dependency><groupId>com.jianzh5</groupId><artifactId>dubbo-api</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
配置文件application.xml
dubbo.application.name=dubbo-providerdubbo.registry.address=nacos://192.168.136.129:8848dubbo.scan.base-packages=com.jianzh5.provider.service.impldubbo.protocol.port=20881dubbo.protocol.name=dubbo
接口实现
publicclassHelloServiceImplimplementsHelloService { publicStringsayHello() { return"欢迎关注微信公众号:JAVA日知录"; } }
注意这里的@service
引用的是org.apache.dubbo.config.annotation.Service
,不要引用错了
启动类
publicclassPrivoderBootstrap { publicstaticvoidmain(String[] args) { SpringApplication.run(PrivoderBootstrap.class, args); } }
需要引入@EnableDubbo注解
消费者dubbo-consumer
pom
<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>dubbo-demo</artifactId><groupId>com.jianzh5</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><groupId>com.jianzh5</groupId><artifactId>dubbo-consumer</artifactId><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--Dubbo--><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-spring-boot-starter</artifactId></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo</artifactId></dependency><!--DubboRegistryNacos--><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-registry-nacos</artifactId></dependency><dependency><groupId>com.alibaba.nacos</groupId><artifactId>nacos-client</artifactId></dependency><dependency><groupId>com.jianzh5</groupId><artifactId>dubbo-api</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
配置文件application.properties
spring.application.name=dubbo-consumerdubbo.registry.address=nacos://192.168.136.129:8848server.port=9090
消费者HelloController
/*** @author jianzh5* @date 2019/11/5 11:29*/publicclassHelloController { privateHelloServicehelloService; "/sayHello") (publicStringsayHello(){ returnhelloService.sayHello(); } }
使用@Reference
注解注入HelloService
接口启动类
publicclassConsumerBootstrap { publicstaticvoidmain(String[] args) { SpringApplication.run(ConsumerBootstrap.class, args); } }
项目启动
- 启动生产者 dubbo-provider
- 启动消费者 dubbo-consumer
- 查看nacos控制台,观察是否注册
访问浏览器http://localhost:9090/sayHello
查看接口返回