springboot+zookeeper集群+dubbo实现服务负载均衡实战教程
解决方法:
1.zookeeper集群搭建参考:http://www.yayihouse.com/yayishuwu/chapter/2914
2.idea创建springboot项目可参考:http://www.yayihouse.com/yayishuwu/chapter/1084
3.创建服务提供者项目
3.1 pom.xml添加以下依赖:
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.0.0</version>
</dependency>
3.2 application.yml配置:
server:
port:
8090
# 定义项目访问端口
dubbo:
application:
name: service-provider # dubbo应用名称
scan:
base-packages: com.example.dubbo.impl # 扫描指定包,将使用
@Service
注解修饰的类发布为服务
protocol:
port:
20880
# dubbo协议端口,默认
20880
name: dubbo # dubbo协议名称
registry: # zookeeper集群注册中心地址
address: zookeeper:
//127.0.0.1:2181?backup=127.0.0.1:2182,127.0.0.1:2183
config-center:
timeout:
10000
# 连接注册中心的超时时间:单位是毫秒
3.3 对外的服务接口
package com.example.dubbo;
public interface ProviderService {
public String get();
}
3.4 接口实现类
package com.example.dubbo.impl;
import com.example.dubbo.ProviderService;
import org.apache.dubbo.config.annotation.Service;
import org.apache.dubbo.rpc.RpcContext;
import org.springframework.stereotype.Component;
@Component
@Service
public class ProviderServiceImpl implements ProviderService {
@Override
public String get() {
System.out.println("添加服务被调用");
return "服务端口:"+ RpcContext.getContext().getLocalPort();
}
}
3.5 springboot项目启动类
package com.example.dubbo;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class DubboApplication {
public static void main(String[] args) {
SpringApplication.run(DubboApplication.class, args);
}
}
4.创建服务消费者项目
4.1 pom.xml依赖:
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.0.0</version>
</dependency>
4.2 application.yml配置:
server:
port:
8080
dubbo:
application:
name: service-consumer # dubbo 应用名称
registry: # zookeeper 集群注册中心地址
address: zookeeper:
//127.0.0.1:2181?backup=127.0.0.1:2182,127.0.0.1:2183
4.3 调用服务接口:
com.example.dubbo路径要和服务提供者的接口路径一致
package com.example.dubbo;
public interface ProviderService {
public String get();
}
4.4 注入服务,调用服务接口测试
package com.example.dubbo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/comsumer")
public class ConsumerController {
@Reference // 注入服务提供方的服务
public ProviderService service;
@GetMapping("/get")
public String get() {
return service.get();
}
}
5.测试效果
idea启动服务提供者项目后,修改application.yml的两个端口为8091、20881后再启动一次,相当启动了两个服务。最后再启动服务消费者项目。
浏览器访问:http://127.0.0.1:8080/comsumer/get
浏览器打印结果会有所变化,有时返回“服务端口:20881”,有时返回“服务端口:20880”,说明服务负载均衡成功。