Windows环境 springboot+dubbo+zookerper 实现相互调用

2019-11-07 18:53:25 浏览数 (1)

windows环境springboot dubbo zookerper实现相互调用

一、zookerper的下载与配置:

jar下载地址:http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz

1、下载下来然后解压,修改配置文件。把conf文件夹下面的 zoo_sample.cfg 复制一份,改名为zoo.cfg

2、然后修改其中一些基本属性,端口,和日志生成的路径。

注意:win10 的zookerper路径不能有空格,与中文。

3、启动 windos很好启动直接双击zkServer就可以了。然后启动客户端。

说明启动成功了!!!

二、dubbo的部署

下载地址:https://codeload.github.com/apache/dubbo/zip/2.5.x

1、下载解压,然后找到admin文件夹

2、修改配置文件

3、修改连接zookeper的配置,已经dubbo登录用户名密码。

4、修改完毕后回到这个界面用cmd进行maven打包。快捷键:shift 鼠标右键 然后点击在此处打开窗口命令。

5、执行完毕后,出现targe目录文件夹把这个 war 复制到tomcat的webapp (修改war包的名字,这个随便你) 然后启动tomcat

6、启动完毕后访问dubbo控制页面, 输入用户名密码后 就可以进去了。

三、代码配置

1、pom.xml文件配置

代码语言:javascript复制
  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
   <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.7</java.version>
         <dubbo.version>2.5.5</dubbo.version>
         <zkclient.version>0.10</zkclient.version>
    </properties>
    
  <dependencies>
  
 	 <!-- spring boot web -->
        <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>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>${dubbo.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

         <!--zookeeper的客户端依赖-->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>${zkclient.version}</version>
        </dependency>
	
  </dependencies>

yml 文件配置:

代码语言:javascript复制
#端口设置
server:
  port: 9998

spring-dubbo-provide.xml 文件配置:

代码语言:javascript复制
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:application name="provider"/>
    <!-- 注册中心的ip地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
   

    <!-- use dubbo protocol to export service on port 20880 -->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!-- service implementation, as same as regular local bean-->
    
    <bean id="userService" class="com.siyuan.serviceimp.UserImp"/> 
    
    <!-- declare the service interface to be exported-->
    
    <dubbo:service interface="com.siyuan.service.Users" ref="userService"/> 
    
    
</beans>

接口代码:

代码语言:javascript复制
public interface Users {
	
	public void add();
}

实现类代码:

代码语言:javascript复制
import com.siyuan.service.Users;


public class UserImp implements Users{

	public void add() {

		System.out.println("调用了第doubbo02项目");
		
	}

}

启动类代码:

代码语言:javascript复制
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;


@SpringBootApplication //spring boot启动
@ImportResource({"classpath:spring-dubbo-provide.xml"})
public class APP {
	
	public static void main(String[] args) {
		SpringApplication.run(APP.class, args);
	}
}

然后启动我们可以看到dubbo中有服务提供者了。

下面是消费者代码:

pom文件配置:

代码语言:javascript复制
 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
   <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.7</java.version>
          <dubbo.version>2.5.5</dubbo.version>
         <zkclient.version>0.10</zkclient.version>
    </properties>
    
  <dependencies>
  
 	 <!-- spring boot web -->
        <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>
        <!-- 提供服务项目的jar  -->
        <dependency>
           <groupId>com.siyuan</groupId>
		  <artifactId>springBootDoubbo02</artifactId>
		  <version>0.0.1-SNAPSHOT</version>
        </dependency>
        
       

        
 	<!-- Dubbo依赖 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>${dubbo.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

         <!--zookeeper的客户端依赖-->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>${zkclient.version}</version>
        </dependency>
	
  </dependencies>

yml配置:

代码语言:javascript复制
#端口设置
server:
  port: 9999

spring-dubbo-remote.xml 配置:

代码语言:javascript复制
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:application name="consumer"/>
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <dubbo:protocol name="dubbo" port="20880"/>
    <dubbo:reference id="users" check="false" interface="com.siyuan.service.Users"/>
    
    <bean id="remoteUsers" class="com.siyuan.serviceimp.RemotUserImp"/> 
    
    
</beans>

接口代码:

代码语言:javascript复制
public interface RemoteUsers {
	
	public void add();
}

实现类代码: 注意这里的service用的是spring的注解

代码语言:javascript复制
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.siyuan.service.Users;
import com.siyuan.services.RemoteUsers;

@Service
public class RemotUserImp implements RemoteUsers{
	
	@Autowired
	private Users users;

	public void add() {
		
		// 调用
		users.add();
		
	}

}

controller代码:

代码语言:javascript复制
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.siyuan.services.RemoteUsers;

@RestController
@RequestMapping("test/")
public class LoginController {
	
	
	@Autowired
	private RemoteUsers remoteUsers;
	
	
	@RequestMapping("test01")
	@ResponseBody
	public String login(HttpServletRequest req){
		String  name = "调用成功";
		System.out.println("进入dubbo项目");

		remoteUsers.add();
		
		
		return name;
	}
}

启动类代码:

代码语言:javascript复制
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication //spring boot启动
@ImportResource({"classpath:spring-dubbo-remote.xml"})
public class APP {
	
	public static void main(String[] args) {
		SpringApplication.run(APP.class, args);
	}
}

然后启动项目

我们到dubbo中可以看到 消费者:

最后我们通过浏览器访问看是否调用成功!!!

看看控制台:

分别在两个控制台打印。。说明远程调用成功!!!

做技术最难能可贵的就是持续的学习,吸收知识,不断的思考形成自己的理解和模式。

0 人点赞