初次接触Dubbo—简单案例
参考官网仓库代码案例:dubbo-samples-api
创建一个Maven项目:引入相关依赖
<properties> <spring.version>4.3.16.RELEASE</spring.version> <dubbo.version>3.0.7</dubbo.version> </properties> <dependencies> <!-- Spring依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-framework-bom</artifactId> <version>${spring.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- Dubbo依赖 --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>${dubbo.version}</version> </dependency> <!-- Dubbo相关的zookeeper依赖 --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-dependencies-zookeeper</artifactId> <version>${dubbo.version}</version> <type>pom</type> </dependency> </dependencies>
api/UserService.java:
import com.changlu.pojo.User; /** * @ClassName UserService * @Author ChangLu * @Date 6/5/2022 8:42 PM * @Description 用户业务接口 */ public interface UserService { User getUserById(Integer id); }
client/Application.java:
import com.changlu.api.UserService; import com.changlu.pojo.User; import org.apache.dubbo.config.ApplicationConfig; import org.apache.dubbo.config.ReferenceConfig; import org.apache.dubbo.config.RegistryConfig; /** * @ClassName Application * @Author ChangLu * @Date 6/5/2022 8:44 PM * @Description 客户端应用 */ public class Application { private static String zookeeperHost = System.getProperty("zookeeper.address", "192.168.10.40"); private static String zookeeperPort = System.getProperty("zookeeper.port", "2181"); public static void main(String[] args) { ReferenceConfig<UserService> reference = new ReferenceConfig<>(); reference.setApplication(new ApplicationConfig("first-dubbo-consumer")); reference.setRegistry(new RegistryConfig( "zookeeper://" + zookeeperHost + ":" + zookeeperPort)); reference.setInterface(UserService.class); //获取到一个代理类 UserService service = reference.get(); //进行远程调用 User user = service.getUserById(123); System.out.println(user); } }
pojo/User.java:
import java.io.Serializable; /** * @ClassName User * @Author ChangLu * @Date 6/5/2022 8:40 PM * @Description 用户类 */ public class User implements Serializable { private String name; private String sex; private Integer age; public User(String name, String sex, Integer age) { this.name = name; this.sex = sex; this.age = age; } }
provider/UserServiceImpl.java:
import com.changlu.api.UserService; import com.changlu.pojo.User; /** * @ClassName UserServiceImpl * @Author ChangLu * @Date 6/5/2022 8:46 PM * @Description 用户实现类 */ public class UserServiceImpl implements UserService { @Override public User getUserById(Integer id) { return new User("changlu", "男", 18); } }
provider/Application.java:
import com.changlu.api.UserService; import org.apache.dubbo.config.ApplicationConfig; import org.apache.dubbo.config.RegistryConfig; import org.apache.dubbo.config.ServiceConfig; import java.util.concurrent.CountDownLatch; /** * @ClassName Application * @Author ChangLu * @Date 6/5/2022 8:47 PM * @Description 生产者 */ public class Application { private static String zookeeperHost = System.getProperty("zookeeper.address", "192.168.10.40"); private static String zookeeperPort = System.getProperty("zookeeper.port", "2181"); public static void main(String[] args) throws InterruptedException { ServiceConfig<UserService> service = new ServiceConfig<>(); //当前服务的应用名 service.setApplication(new ApplicationConfig("first-dubbo-provider")); //指定注册地址 service.setRegistry(new RegistryConfig( "zookeeper://" + zookeeperHost + ":" + zookeeperPort) ); //设置相应服务的接口以及对应实例 service.setInterface(UserService.class); service.setRef(new UserServiceImpl()); //进行一个注册 service.export(); //阻塞 System.out.println("dubbo service started"); new CountDownLatch(1).await(); } }
测试:首先启动provider中的application首先进行注册,接着再执行client进行调用获取ip地址来进行RPC调用
这里是以zookeeper作为注册中心。
provider先启动:
此时我们来观察zookeeper:①可以看到上图启动图当红色框以及依据dubbo service started执行时对应zookeeper中出现了/dubbo结点,在/dubbo下有两个结点一个是configurators、providers,在provides结点中包含了一长串的一个url信息(也就是初始提供服务端发送的url信息)。②上面窗口执行黄色框时,此时zookeeper出现了/services,我们查看该结点,在该结点下包含了对应的服务名的一个ip地址。
client启动:调用提供服务的接口,这里我放两个截图
第一张图我猜测首先肯定会去读缓存看是否有对应服务的一个地址,若是有直接就进行RPC调用,没有的话就会去注册中心获取相应服务的ip地址与端口(很明显这里就是去注册中心获取了,rpc调用结束后会响应返回一个结果值)
报错解决
1、启动Dubbo服务Injection of @DubboReference dependencies is failed
启动Dubbo服务Injection of @DubboReference dependencies is failed
1.@DubboService 注解要加入vrsion版本号 2.缺少jar包 3.生产者&消费者服务结构目录一致 4.生产者的Dubbo port端口号问题