# 拉取镜像
docker pull zookeeper:3.8.0
# 运行容器
docker run --name zookeeper -p 2181:2181 --restart always -d zookeeper:3.8.0
public interface HelloService {
String hello(String name);
}
<?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-demo</artifactId>
<groupId>com.coffee</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-provider</artifactId>
<properties>
<dubbo-version>3.0.0</dubbo-version>
</properties>
<dependencies>
<dependency>
<groupId>com.coffee</groupId>
<artifactId>common-api</artifactId>
<version>${project.version}</version>
</dependency>
<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>
<!--dubbo配置-->
<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-registry-zookeeper</artifactId>
<version>${dubbo-version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-configcenter-zookeeper</artifactId>
<version>${dubbo-version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-metadata-report-zookeeper</artifactId>
<version>${dubbo-version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-config-spring</artifactId>
<version>${dubbo-version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
server:
port: 6060
dubbo:
application:
name: dubbo-provider
protocol:
name: dubbo
port: 20880
registry:
id: zk-registry
address: zookeeper://127.0.0.1:2181
config-center:
address: zookeeper://127.0.0.1:2181
metadata-report:
address: zookeeper://127.0.0.1:2181
@DubboService
public class HelloServiceImpl implements HelloService {
@Override
public String hello(String name) {
return "Hello " + name + ", This is Dubbo + Zookeeper !";
}
}
@SpringBootApplication
@EnableDubbo
public class DubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class, args);
}
}
<?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-demo</artifactId>
<groupId>com.coffee</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-consumer</artifactId>
<properties>
<dubbo-version>3.0.0</dubbo-version>
</properties>
<dependencies>
<dependency>
<groupId>com.coffee</groupId>
<artifactId>common-api</artifactId>
<version>${project.version}</version>
</dependency>
<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>
<!--dubbo配置-->
<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-registry-zookeeper</artifactId>
<version>${dubbo-version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-configcenter-zookeeper</artifactId>
<version>${dubbo-version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-metadata-report-zookeeper</artifactId>
<version>${dubbo-version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-config-spring</artifactId>
<version>${dubbo-version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
server:
port: 8686
dubbo:
application:
name: dubbo-consumer
protocol:
name: dubbo
port: 20880
registry:
id: zk-registry
address: zookeeper://127.0.0.1:2181
config-center:
address: zookeeper://127.0.0.1:2181
metadata-report:
address: zookeeper://127.0.0.1:2181
@RestController
public class ConsumerController {
@DubboReference
private HelloService helloService;
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String hello(@RequestParam("name") String name){
return helloService.hello(name);
}
}
@SpringBootApplication
@EnableDubbo
public class DubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DubboConsumerApplication.class, args);
}
}
docker pull apache/dubbo-admin:0.4.0
docker run -p 8888:8080 --name dubbo-admin \
-e admin.registry.address=zookeeper://127.0.0.1:2181 \
-e admin.config-center=zookeeper://127.0.0.1:2181 \
-e admin.metadata-report.address=zookeeper://127.0.0.1:2181 \
--restart=always \
-d apache/dubbo-admin:0.4.0
访问 http://127.0.0.1:8888,并使用账号与密码登录(默认账号与密码都是root)
a、访问 http://127.0.0.1:8888 如果出现无法访问或者查看 dubbo-admin 容器日志显示无法连接上 zookeeper,那么执行以下步骤:
b、查看zookeeper产生的日志,ZooKeeper的日志是序列化过的,所以需要反序列化。
# ZooKeeper 3.6版本及之后的版本,可使用该命令
zkTxnLogToolkit.sh -d 日志文件名
Dubbo 负载均衡策略支持用户自定义拓展,内置默认的负载均衡算法为“random”,内置五种负载均衡算法分别为:
- random:随机,按照权重设置随机概率
- roundrobin:轮询,按公约后的权重设置轮询比例
- leastactive:最少活跃数,如果活跃数相同则随机调用,活跃数指调用前后计数差
- consistenthash:一致性哈希,相同参数的请求总是发送到同一个提供者
- shortestresponse:基于响应的时间进行计算,过滤成功调用响应时间最短的调用者数量,统计这些调用者的权重和数量(dubbo 2.7.7之后新增)
@SPI(RandomLoadBalance.NAME)
public interface LoadBalance {
/**
* select one invoker in list.
* @param invokers invokers.
* @param url refer url
* @param invocation invocation.
* @return selected invoker.
*/
@Adaptive("loadbalance")
<T> Invoker<T> select(List<Invoker<T>> invokers, URL url, Invocation invocation) throws RpcException;
}
public class CustomizeLoadBalanceRule extends AbstractLoadBalance {
private AtomicInteger count = new AtomicInteger(0);
@Override
protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
int index = count.incrementAndGet() % invokers.size();
return invokers.get(index);
}
}
在resource目录下,创建 META-INF/dubbo 目录,并创建名称为org.apache.dubbo.rpc.cluster.LoadBalance的文件,内容(key,value)形式,key自定义,value为自定义类的权限定类名。比如customize=com.huaweicloud.sample.config.CustomizeLoadBalanceRule
@DubboReference(loadbalance = "customize")
private HelloService helloService;