目录
一、代码地址:
https://gitee.com/hankeyan555/dubbo-text
二、实现过程
目录结构:
1、新建父项目dubbo-text
添加基本信息:
选择springboot版本
建好后,删除src等用不上的。
父项目pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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>
<groupId>com.hky</groupId>
<artifactId>dubbo-text</artifactId>
<packaging>pom</packaging>
<version>1.0.0</version>
<modules>
<module>dubbo-api</module>
<module>dubbo-provider</module>
<module>dubbo-consumer</module>
</modules>
<!--<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
<relativePath/>
</parent>-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.4.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.4.4</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<!-- 在构建路径中 指定源文件编译的字符编码 -->
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
2、新建子项目dubbo-api
然后依旧是选择Spring Initializr类型的新建,然后填写基本信息,依次下一步直到完成就好。
pom.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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-text</artifactId>
<groupId>com.hky</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<version>1.0.0</version>
<artifactId>dubbo-api</artifactId>
</project>
3、新建子项目dubbo-provider
与上一步类似,不再复述,pom.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.hky</groupId>
<artifactId>dubbo-text</artifactId>
<version>1.0.0</version>
</parent>
<groupId>com.hky</groupId>
<artifactId>dubbo-provider</artifactId>
<version>1.0.0</version>
<name>dubbo-provider</name>
<description>Demo project for Spring Boot</description>
<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>com.hky</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-recipes -->
<!-- 解决java.lang.NoClassDefFoundError: org/apache/curator/framework/CuratorFrameworkFactory报错 -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<!--在这里修改版本-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4、新建子项目dubbo-consumer
与上一步类似,不再复述,pom.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.hky</groupId>
<artifactId>dubbo-text</artifactId>
<version>1.0.0</version>
</parent>
<groupId>com.hky</groupId>
<artifactId>dubbo-consumer</artifactId>
<version>1.0.0</version>
<name>dubbo-consumer</name>
<description>Demo project for Spring Boot</description>
<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>com.hky</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-recipes -->
<!-- 解决java.lang.NoClassDefFoundError: org/apache/curator/framework/CuratorFrameworkFactory报错 -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
三、编码和配置
1、编写dubbo-api
子项目dubbo-api我理解为是承上启下的作用,它只包含接口,不包含实现,也无需配置。由于生产者和消费者都引用dubbo-api的jar包,这样有多个消费者,直接引用dubbo-api的jar即可。
依照目录结构,新建DemoService接口:
package com.hky.dubbo.api.service;
public interface DemoService {
public String sayHello(String name);
}
2、编写dubbo-provider(生产者)
首先application.properties配置基本信息和zookeeper信息,如下:
#设置内嵌Tomcat
server.port=8081
#设置上下文根
server.servlet.context-path=/
#配置 dubbo 的服务提供者信息
#服务提供者应用名称(必须写,且不能重复)
spring.application.name=dubbo-provider
#设置当前工程为服务提供者
spring.dubbo.server=true
#设置注册中心
spring.dubbo.registry=zookeeper://localhost:2181
dubbo.registry.address=zookeeper://localhost:2181
dubbo.metadata-report.address=zookeeper://localhost:2181
然后,编写启动类,加上@EnableDubbo注解:
package com.hky.dubbo;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDubbo
@SpringBootApplication
public class DubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class, args);
}
}
然后,编写DemoServiceImpl是对dubbo-api中DemoService的实现类,并将其注册到zookeeper:
package com.hky.dubbo.service.impl;
import com.hky.dubbo.api.service.DemoService;
import org.apache.dubbo.config.annotation.DubboService;
@DubboService(version = "1.0.0")
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
return "hello word! "+ name;
}
}
以前用
@service和@reference注解,新版本这俩注解已经淘汰了,用@DubboService和@DubboReference注解了
再有version要必填,可自定义版本号
3、编写dubbo-consumer(消费者)
配置文件application.properties:(注意端口和应用名称设置不一样的)
#设置内嵌Tomcat
server.port=8089
#设置上下文根
server.servlet.context-path=/
#配置 dubbo 的服务提供者信息
#服务提供者应用名称(必须写,且不能重复)
spring.application.name=dubbo-consumer
#设置注册中心
spring.dubbo.registry=zookeeper://localhost:2181
dubbo.registry.address=zookeeper://localhost:2181
dubbo.metadata-report.address=zookeeper://localhost:2181
启动类,依旧添加@EnableDubbo
package com.hky.dubbo;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDubbo
@SpringBootApplication
public class DubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DubboConsumerApplication.class, args);
}
}
然后,consumer添加对外访问的controller,如下:
package com.hky.dubbo.controller;
import com.hky.dubbo.api.service.DemoService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoController {
// 调用暴露的接口
@DubboReference(interfaceClass = DemoService.class,version = "1.0.0",check = false)
private DemoService demoService;
@GetMapping("/demo")
public String getDemo(){
System.out.println("调用了dubbo");
String res = demoService.sayHello("hankeyan");
return res;
}
}
四、启动
1、启动zookeeper
先下载zookeeper,当前最新版本下载地址:
https://mirrors.bfsu.edu.cn/apache/zookeeper/zookeeper-3.7.0/
注意:要下载apache-zookeeper-3.7.0-bin这个
解压后,如下:
2、启动provider
3、启动consumer
五、测试
访问地址:http://localhost:8089/demo
效果: