正文
一、Dubbo架构
调用关系说明
服务容器负责启动,加载,运行服务提供者。
服务提供者在启动时,向注册中心注册自己提供的服务。
服务消费者在启动时,向注册中心订阅自己所需的服务。
注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
二、代码
代码结构如下
pom文件
<?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.xiaojie</groupId> <artifactId>dubbo-demo</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>dubbo-demo-api</module> <module>dubbo-demo-provider</module> <module>dubbo-demo-consumer</module> </modules> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.2</version> <relativePath/> </parent> <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> </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.12</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.6.2</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework --> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>4.2.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.curator/curator-recipes --> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>4.2.0</version> </dependency> </dependencies> </project>
接口类
package com.xiaojie.dubbo.api; /** * @Description:用户接口,只有接口 没有实现类 * @author: xiaojie * @date: 2021.08.02 */ public interface UserServiceApi { /* * * @param userId * @获取用户名称 * @author xiaojie * @date 2021/8/2 9:19 * @return java.lang.String */ String getUserName(String userId); }
接口实现类(服务提供者),此模块要依赖dubbo-demo-api模块,或者单独将dubbo-demo-api打成jar包,添加到maven依赖。
package com.xiaojie.dubbo.api.impl; import com.xiaojie.dubbo.api.UserServiceApi; import org.apache.dubbo.config.annotation.DubboService; /** * @Description:接口实现类 * @author: xiaojie * @date: 2021.08.02 */ @DubboService(version = "1.0.0") public class UserServiceApiImpl implements UserServiceApi { @Override public String getUserName(String userId) { return "我是服务的提供者:我的名字是小杰"; } }
application.properties
#spring项目名 spring.application.name=xiaojie-dubbo-provider #Dubbo provider configuration dubbo.application.name=dubbo_provider dubbo.registry.protocol=zookeeper dubbo.registry.address=192.168.6.136:2181 dubbo.protocol.name=dubbo dubbo.registry.timeout=50000 #服务端口 dubbo.protocol.port=20881 #扫描注解包通过该设置将服务注册到zookeeper dubbo.scan.base-packages=com.xiaojie.dubbo.api server.port=8081
服务消费者
package com.xiaojie.controller; import com.xiaojie.dubbo.api.UserServiceApi; import org.apache.dubbo.config.annotation.DubboReference; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * @Description: * @author: xiaojie * @date: 2021.08.02 */ @RestController public class ConsumerController { @DubboReference(version = "1.0.0") private UserServiceApi userServiceApi; @GetMapping("/getUserName") public String getUserName(String userId){ return userServiceApi.getUserName(userId); } }
application.properties
spring.application.name=xiaojie-dubbo-consumer dubbo.application.name=dubbo_consumer dubbo.registry.protocol=zookeeper #zk地址 dubbo.registry.address=192.168.6.136:2181 dubbo.registry.timeout=50000 server.port=8090
三、注册到Zookeeper之后数据存储