org.springframework.boot spring-boot-starter-parent 2.1.8.RELEASE org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter org.apache.dubbo dubbo-spring-boot-starter 2.7.5 org.apache.curator curator-recipes 4.2.0 org.apache.zookeeper zookeeper 3.4.12
新建两个模块
服务的发起者 Dubbo-server
服务的调用者 dubbo
server:
port: 8000
#dubbo扫描
dubbo:
scan:
base-packages: com
application:
name: dubbo-server
protocol: #dubbo对外暴露的端口信息
name: dubbo
port: 20880
registry: #dubbo注册中心的配置
address: zookeeper://192.168.23.129:2181
timeout: 60000
服务层
application.yml配置文件
server:
port: 8001
#dubbo扫描
dubbo:
scan:
base-packages: com
application:
name: dubbo
registry: #dubbo注册中心的配置
address: zookeeper://192.168.23.129:2181
控制层
@RestController
public class Test {
@Reference
private ServerTest serverTest;
@GetMapping("/test")
public String test(){
String test = serverTest.test();
return test;
}
}
先启动dubbo-server 服务发起者
在启动 dubbo 服务调用者
调用成功
服务调用者
修改controller代码
延时1m
重试一次
@Reference(timeout = 1000,retries = 1)
private ServerTest serverTest;
@GetMapping("/test")
public String test(){
String test = serverTest.test();
return test;
}
服务提供者
修改代码
@Service
public class ServerTestImpl implements ServerTest{
public String test() {
System.out.println("服务端测试");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "aa";
}
}
重新启动项目
服务提供者线程睡眠2秒,这样,服务调用方,调用时,会因为延时时间小于服务提供者睡眠时间而导致调用失败,从而重试,
但是因为打印“服务端测试”在线程睡眠之前完成,所以会打印两次
服务提供者
@Service(version = "2.0")
public class ServerTestImpl implements ServerTest{
public String test() {
System.out.println("服务端测试");
return "aa";
}
}
服务调用者
@Reference(timeout = 1000,retries = 1,version = "2.0")
private ServerTest serverTest;
@GetMapping("/test")
public String test(){
String test = serverTest.test();
return test;
}
负载均衡
按权重随机
服务调用者
@Reference(loadbalance = "random")
private ServerTest serverTest;
@GetMapping("/test")
public String test(){
String test = serverTest.test();
return test;
}
服务提供者1
@Service(weight = 200)
public class ServerTestImpl implements ServerTest{
public String test() {
System.out.println("服务端测试");
return "aa";
}
}
服务提供者2
@Service(weight = 100)
public class ServerTestImpl implements ServerTest{
public String test() {
System.out.println("服务端测试2");
return "aa2";
}
}
调用结果
服务调用者
@Reference(loadbalance = "roundrobin")
private ServerTest serverTest;
@GetMapping("/test")
public String test(){
String test = serverTest.test();
return test;
}
服务提供者代码如上
其他负载均衡方式// Random:按权重随机,默认值。按权重设置随机概率。
// RoundRobin: 按权重轮询。
// LeastActive:最少活跃调用数,相同活跃数的随机。
// ConsistentHash:一 致性Hash,相同参数的请求总是发到同一提供者
这个是查到的,没有试过



