QUESTIONl:SpringBoot整合Dubbo+Zookeeper进行分布式搭建系统?
ANSWER:
一:创建项目模块
1.1.创建一个Empty Project
名称:Dubbo
1.2.创建一个Provider模块
这里采用SpringBoot快速搭建。
1.3.创建Consumer模块
创建完成后代码架构:
二:provider代码部分
2.1导入provider.xml依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.1.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.xy</groupId><artifactId>provider</artifactId><version>0.0.1-SNAPSHOT</version><name>provider</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><!-- 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.3</version></dependency><!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient --><dependency><groupId>com.github.sgroschupf</groupId><artifactId>zkclient</artifactId><version>0.1</version><exclusions><exclusion><artifactId>slf4j-log4j12</artifactId><groupId>org.slf4j</groupId></exclusion></exclusions></dependency><!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework --><dependency><groupId>org.apache.curator</groupId><artifactId>curator-framework</artifactId><version>2.13.0</version></dependency><dependency><groupId>org.apache.curator</groupId><artifactId>curator-recipes</artifactId><version>2.13.0</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper --><dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>3.4.13</version><exclusions><exclusion><artifactId>slf4j-log4j12</artifactId><groupId>org.slf4j</groupId></exclusion></exclusions></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><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
2.2创建POJO类
创建一个User实体类,只做简单的测试:
package com.xy.provider.pojo;import java.io.Serializable;public class User implements Serializable {private static final long serialVersionUID = -7341603933521593227L;private Long id;private String username;private String password;private Integer age;public static long getSerialVersionUID() {return serialVersionUID;}public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}@Overridepublic String toString() {return "User{" +"id=" + id +", username='" + username + '\'' +", password='" + password + '\'' +", age=" + age +'}';}
}
2.3创建接口及实现类
创建userService接口:
package com.xy.provider.service;import com.xy.provider.pojo.User;import java.util.List;public interface UserService {public List<User> findAll();public void test();
}
UserServiceImpl实现类:
package com.xy.provider.service.impl;import com.xy.provider.pojo.User;
import com.xy.provider.service.UserService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;import java.util.ArrayList;
import java.util.List;@Service //这里要用dubbo的service,在项目一启动就注册到注册中心
@Component //使用了Dubbo尽量不要用@Service
public class UserServiceImpl implements UserService {@Overridepublic List<User> findAll() {System.out.println("查询所有");List<User> list = new ArrayList<User>();for (int i = 0; i < 10; i++) {User user = new User();user.setAge(10 + i);user.setId(Long.valueOf(i + 1));user.setPassword("123456");user.setUsername("username_" + i);list.add(user);}return list;}@Overridepublic void test() {System.out.println("dubbo");}
}
2.4构造application.properties
#提供者名称
dubbo.application.name=provider
#注册中心地址
dubbo.registry.address=zookeeper://www.youngxy.top:2181
#哪些服务要注册
dubbo.scan.base-packages=com.xy.provider.service
三:consumer代码部分
3.1导入consumer.xml依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.4.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.xy</groupId><artifactId>consumer</artifactId><version>0.0.1-SNAPSHOT</version><name>consumer</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><!-- 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.3</version></dependency><!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient --><dependency><groupId>com.github.sgroschupf</groupId><artifactId>zkclient</artifactId><version>0.1</version><exclusions><exclusion><artifactId>slf4j-log4j12</artifactId><groupId>org.slf4j</groupId></exclusion></exclusions></dependency><!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework --><dependency><groupId>org.apache.curator</groupId><artifactId>curator-framework</artifactId><version>2.13.0</version></dependency><dependency><groupId>org.apache.curator</groupId><artifactId>curator-recipes</artifactId><version>2.13.0</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper --><dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>3.4.13</version><exclusions><exclusion><artifactId>slf4j-log4j12</artifactId><groupId>org.slf4j</groupId></exclusion></exclusions></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><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency><dependency><groupId>com.xy</groupId><artifactId>provider</artifactId><version>0.0.1-SNAPSHOT</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
3.2创建实现类
消费者要获取提供者的服务,定义消费者实现类:
package com.xy.consumer.service;import com.xy.provider.pojo.User;
import com.xy.provider.service.UserService;
import com.xy.provider.service.impl.UserServiceImpl;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;import java.util.List;@Service //这里是Spring的服务,表示放到容器中
public class ConsumerService {/** 想要拿到UserService,要到注册中心去拿* */@Referenceprivate UserService userService; //引用,pom导入相关依赖public void consume(){System.out.println("test");List<User> users = userService.findAll();for (User user:users) {System.out.println(user);}userService.test();}
}
3.3构造application.properties
#SpringBoot端口
server.port=8081
#消费者去注册中心获取服务需要暴露自己的名称
dubbo.application.name=consumer
#注册中心的地址
dubbo.registry.address=zookeeper://www.youngxy.top:2181
3.4创建测试类
package com.xy.consumer;import com.xy.consumer.service.ConsumerService;
import com.xy.provider.pojo.User;
import com.xy.provider.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.util.List;@SpringBootTest
class ConsumerApplicationTests {@Autowiredprivate ConsumerService consumerService;@Testpublic void testConsume(){consumerService.consume();System.out.println("成功");}}
四:打开dubbo-admin平台
首先在本地或者云服务器上面安装好dubbo软件,详情请参照:搭建分布式环境:Dubbo+Zookeeper
在浏览器输入:ip:8080/dubbo-admin-2.5.4-SNAPSHOT输入密码进入。
五:SpringBoot和Dubbo、Zookeeper版本不兼容问题的解决
5.1官方匹配的版本
请参考:SrpingBoot整合Zookeeper和Dubbo的版本匹配问题
5.2依赖冲突
请参考:依赖包冲突