Springboot2集成dubbo,并使用zookeeper作为注册中心

运行环境: JDK1.8、Tomcat8

技术栈:

    springboot-2.1.1

    dubbo-2.6.2

    dubbo-spring-boot-starter-0.2.0

    zookeeper-3.4.13

    dubbo-admin-2.5.10

1、Zookeeper的安装和启动(单节点):

将zookeeper-3.4.13.tar.gz安装包上传到Linux服务器的/opt/software/zookeeper目录下
解压安装包: tar -zxvf zookeeper-3.4.13.tar.gz
定位到/opt/software/zookeeper/zookeeper-3.4.13目录,分别创建名为data、dataLog的文件夹
定位到/opt/software/zookeeper/zookeeper-3.4.13/conf目录,创建zoo.cfg配置文件,文件内容如下:
	tickTime=2000
	initLimit=10
	syncLimit=5
	dataDir=/opt/software/zookeeper/zookeeper-3.4.13/data
	dataLogDir=/opt/software/zookeeper/zookeeper-3.4.13/dataLog
	clientPort=2181
将2181端口添加到防火墙,确保客户端能正常访问到该端口
	firewall-cmd --zone=public --add-port=2181/tcp --permanent
定位到/opt/software/zookeeper/zookeeper-3.4.13/bin目录下,执行 ./zkServer.sh start 命令启动zk服务

2、dubbo-admin的安装:

将dubbo-admin.war安装包复制到Tomcat的webapps目录下
执行bin/startup.bat启动tomcat,然后停止tomcat
修改webapps\dubbo-admin\WEB-INF\dubbo.properties配置文件的dubbo.registry.address参数值
	dubbo.registry.address=zookeeper://192.168.134.134:2181
再次启动tomcat
以root账号登录

 
Springboot2集成dubbo,并使用zookeeper作为注册中心
 

3、maven多模块项目的创建

        创建一个maven项目dubbo,包括三个模块子项目,分别为:

                dubbo-api: 存放服务接口、bean类等公共文件

                dubbo-provider: 服务提供者工程

                dubbo-consumer: 服务消费者工程

        主项目的pom.xml文件配置:

<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.study.dubbo</groupId>
  <artifactId>dubbo</artifactId>
  <version>1.0.0</version>
  <packaging>pom</packaging>
  
  <parent>
  	<groupId>org.springframework.boot</groupId>  
    <artifactId>spring-boot-starter-parent</artifactId>  
    <version>2.1.1.RELEASE</version> 
    <relativePath/> 
  </parent>
  
  <properties>
  		<java.version>1.8</java.version>
    	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  </properties>
  
  <dependencies>
  	<!-- springboot -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-devtools</artifactId>
		<optional>true</optional>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
	</dependency>
    
    <!-- dubbo -->
    <dependency>
	    <groupId>com.alibaba</groupId>
	    <artifactId>dubbo</artifactId>
	    <version>2.6.2</version>
	</dependency>
	<dependency>
	    <groupId>com.alibaba.boot</groupId>
	    <artifactId>dubbo-spring-boot-starter</artifactId>
	    <version>0.2.0</version>
	</dependency>
	<dependency>
	    <groupId>com.alibaba.boot</groupId>
	    <artifactId>dubbo-spring-boot-actuator</artifactId>
	    <version>0.2.0</version>
	</dependency>
    <dependency>
	    <groupId>com.alibaba.boot</groupId>
	    <artifactId>dubbo-spring-boot-autoconfigure</artifactId>
	    <version>0.2.0</version>
	</dependency>
	<dependency>
	    <groupId>com.alibaba.spring</groupId>
	    <artifactId>spring-context-support</artifactId>
	    <version>1.0.2</version>
	</dependency>

    <!-- zookeeper -->
    <dependency>
	    <groupId>org.apache.zookeeper</groupId>
	    <artifactId>zookeeper</artifactId>
	    <version>3.4.9</version>
	    <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion>
            <exclusion>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
            </exclusion>
        </exclusions>
	</dependency>
	<dependency>
    	<groupId>org.apache.curator</groupId>
    	<artifactId>curator-framework</artifactId>
    	<version>2.12.0</version>
	</dependency>
    
	<dependency>
	    <groupId>junit</groupId>
	    <artifactId>junit</artifactId>
	    <scope>test</scope>
	</dependency>
  </dependencies>
  
  <modules>
  	<module>dubbo-api</module>
  	<module>dubbo-provider</module>
  	<module>dubbo-consumer</module>
  </modules>  
</project>

4、dubbo-api工程

    User.java

package com.study.dubbo.api.bean;
import java.io.Serializable;
public class User implements Serializable{
	private static final long serialVersionUID = -8832914162040512009L;
	
	private Long id;
	private String username;
	private String password;
	
	public User(){
		
	}
	
	public User(Long id, String username, String password){
		this.id = id;
		this.username = username;
		this.password = password;
	}
	
	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 Long getNowtime() {
		return System.currentTimeMillis();
	}	
}

    UserService.java  —— 服务接口类

package com.study.dubbo.api.service;
import com.study.dubbo.api.bean.User;
/**
 * 定义dubbo服务接口
 */
public interface UserService {
	public User getUser(long id);
}

5、dubbo-provider工程

     UserServiceImpl.java  —— 服务接口的实现类

package com.study.dubbo.provider.service;

import org.springframework.stereotype.Component;
import com.alibaba.dubbo.config.annotation.Service;
import com.study.dubbo.api.bean.User;
import com.study.dubbo.api.service.UserService;

/**
 * dubbo服务接口的实现类,使用@Service注解暴露dubbo服务
 */
@Service(interfaceClass=UserService.class, version="1.0.0", timeout=10000)
@Component
public class UserServiceImpl implements UserService {
	@Override
	public User getUser(long id) {
		return new User(id, "zhangsan", "123");
	}
}

     application.properties

server.port=8001
server.servlet.context-path=/dubbo-provider

##dubbo
dubbo.scan.basePackages=com.study.dubbo.provider.service
dubbo.application.name=provider-name

dubbo.protocol.name=dubbo
dubbo.protocol.port=20880

dubbo.registry.protocol=zookeeper
dubbo.registry.address=192.168.134.134:2181

    pom.xml文件:

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <artifactId>dubbo-provider</artifactId>
  <packaging>jar</packaging>
  
  <parent>
    <groupId>com.study.dubbo</groupId>
    <artifactId>dubbo</artifactId>
    <version>1.0.0</version>
    <relativePath>../pom.xml</relativePath>
  </parent>
  
  <dependencies>
    <dependency>
        <groupId>com.study.dubbo</groupId>
        <artifactId>dubbo-api</artifactId>
    	<version>1.0.0</version>
    </dependency>
  </dependencies>
  
  <build>
    <finalName>dubbo-provider</finalName>
  </build>
  
</project>

    Main.java

package com.study.dubbo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication()
public class Main{
	public static void main(String[] args){
		SpringApplication springApp = new SpringApplication(Main.class);
		springApp.run(args);
	}
	
	@RequestMapping("/index")
    public String greeting() {
        return "hello provider";
    }
}

      执行Main.java类,启动服务提供者工程应用。

6、dubbo-consumer工程

    ServiceConsumer.java

package com.study.dubbo.consumer.service;

import org.springframework.stereotype.Service;
import com.alibaba.dubbo.config.annotation.Reference;
import com.study.dubbo.api.service.UserService;

@Service
public class ServiceConsumer {
	/**
	 * 消费dubbo服务
	 */
	@Reference(version="1.0.0")
	private UserService userService;

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}	
}

    application.properties

server.port=8002
server.servlet.context-path=/dubbo-consumer

##dubbo
dubbo.application.name=consumer-name

dubbo.registry.protocol=zookeeper
dubbo.registry.address=192.168.134.134:2181

    pom.xml文件

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <artifactId>dubbo-consumer</artifactId>
  <packaging>jar</packaging>
  
  <parent>
    <groupId>com.study.dubbo</groupId>
    <artifactId>dubbo</artifactId>
    <version>1.0.0</version>
    <relativePath>../pom.xml</relativePath>
  </parent>
  
  <dependencies>
    <dependency>
        <groupId>com.study.dubbo</groupId>
        <artifactId>dubbo-api</artifactId>
    	<version>1.0.0</version>
    </dependency>
  </dependencies>
  
  <build>
    <finalName>dubbo-consumer</finalName>
  </build>
  
</project>

    Main.java

package com.study.dubbo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.study.dubbo.api.bean.User;
import com.study.dubbo.consumer.service.ServiceConsumer;

@RestController
@SpringBootApplication()
public class Main{
	@Autowired()
	private ServiceConsumer serviceConsumer;
	
	public static void main(String[] args){
		SpringApplication springApp = new SpringApplication(Main.class);
		springApp.run(args);
	}
	
	@RequestMapping("/index")
    public String greeting() {
		User user = serviceConsumer.getUserService().getUser(110L);
        return "hello consumer: username=" + user.getUsername() + ", password=" + user.getPassword() + ", nowtime=" + user.getNowtime();
    }
}

      执行Main.java类,启动服务消费者工程应用。

7、测试验证

     刷新dubbo-admin的服务列表页面,可以看到列表中有一个UserService服务

Springboot2集成dubbo,并使用zookeeper作为注册中心

     浏览器访问: http://localhost:8002/dubbo-consumer/index ,页面会显示如下内容:

相关推荐