dubbo 搭建分布式项目

原理我就不解释了,先百度下。在搭建之前,我们先把zookeeper启动,挂在那里。

zookeeper的搭建很简单,请参考:

https://blog.csdn.net/shushugood/article/details/80803044

1.服务提供方

1.1 先创maven项目





1.2 把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.broadtech</groupId>
  <artifactId>provider</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
   <properties>  
      <spring.version>3.2.8.RELEASE</spring.version>  
  </properties>  

  <dependencies>  
      <dependency>  
          <groupId>com.alibaba</groupId>  
          <artifactId>dubbo</artifactId>  
          <version>2.5.3</version>  
          <exclusions>  
              <exclusion>  
                  <groupId>org.springframework</groupId>  
                  <artifactId>spring</artifactId>  
              </exclusion>  
          </exclusions>  
      </dependency>  
      <dependency>  
          <groupId>com.github.sgroschupf</groupId>  
          <artifactId>zkclient</artifactId>  
          <version>0.1</version>  
      </dependency>  
      <!-- spring相关 -->  
      <dependency>  
          <groupId>org.springframework</groupId>  
          <artifactId>spring-core</artifactId>  
          <version>${spring.version}</version>  
      </dependency>  
      <dependency>  
          <groupId>org.springframework</groupId>  
          <artifactId>spring-beans</artifactId>  
          <version>${spring.version}</version>  
      </dependency>  
      <dependency>  
          <groupId>org.springframework</groupId>  
          <artifactId>spring-context</artifactId>  
          <version>${spring.version}</version>  
      </dependency>  
      <dependency>  
          <groupId>org.springframework</groupId>  
          <artifactId>spring-jdbc</artifactId>  
          <version>${spring.version}</version>  
      </dependency>  
      <dependency>  
          <groupId>org.springframework</groupId>  
          <artifactId>spring-web</artifactId>  
          <version>${spring.version}</version>  
      </dependency>  
      <dependency>  
          <groupId>org.springframework</groupId>  
          <artifactId>spring-webmvc</artifactId>  
          <version>${spring.version}</version>  
      </dependency>  
      <dependency>  
          <groupId>org.springframework</groupId>  
          <artifactId>spring-aop</artifactId>  
          <version>${spring.version}</version>  
      </dependency>  
      <dependency>  
          <groupId>org.springframework</groupId>  
          <artifactId>spring-tx</artifactId>  
          <version>${spring.version}</version>  
      </dependency>  
      <dependency>  
          <groupId>org.springframework</groupId>  
          <artifactId>spring-orm</artifactId>  
          <version>${spring.version}</version>  
      </dependency>  
      <dependency>  
          <groupId>org.springframework</groupId>  
          <artifactId>spring-context-support</artifactId>  
          <version>${spring.version}</version>  
      </dependency>  
      <dependency>  
          <groupId>org.springframework</groupId>  
          <artifactId>spring-test</artifactId>  
          <version>${spring.version}</version>  
      </dependency>  
      <dependency>  
          <groupId>org.springframework</groupId>  
          <artifactId>spring-jms</artifactId>  
          <version>${spring.version}</version>  
      </dependency>  
  </dependencies>  
</project>


目录结构:


package com.broadtech.service;

public interface MengService {
	public String getName();
}
package com.broadtech.service.impl;

import com.broadtech.service.MengService;

public class MengServiceImpl implements MengService {

	public String getName() {
		// TODO Auto-generated method stub
		return "孟梁测试dubbo service";
	}

}

<?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 ">   
    <!-- 具体的实现bean -->  
    <bean id="mengService" class="com.broadtech.service.impl.MengServiceImpl" />  
    <!-- 提供方应用信息,用于计算依赖关系 -->  
    <dubbo:application name="provider"  />    
    <!-- 使用zookeeper注册中心暴露服务地址 -->  
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />     
    <!-- 用dubbo协议在20880端口暴露服务 -->  
    <dubbo:protocol name="dubbo" port="28770" />  
    <!-- 声明需要暴露的服务接口 -->  
    <dubbo:service interface="com.broadtech.service.MengService" ref="mengService" />  
</beans>

package com.broadtech.service;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestService {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		ClassPathXmlApplicationContext context= new ClassPathXmlApplicationContext(
				new String[] {"application.xml"}
				);
		context.start();
		System.out.println("提供服务已经注册成功");
		try {
			System.in.read();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}

在启动provider之前,先启动zookeeper

下图是启动provider




2.消费方



<?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" />     
      <!-- 使用multicast广播注册中心暴露发现服务地址 -->  
    <dubbo:registry  protocol="zookeeper" address="zookeeper://127.0.0.1:2181" />       
      <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->  
    <dubbo:reference id="mengService" interface="com.broadtech.service.MengService" />  
</beans>

package com.broadtech.service;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ConsumerServiceTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
	                new String[] { "application.xml" });
	        context.start();
	        MengService mengService = (MengService) context.getBean("mengService");
	        System.out.println(mengService.getName());
	      
	            try {
					System.in.read();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	        
	        
	}

}

<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.broadtech</groupId>
  <artifactId>consumer</artifactId>
  <version>0.0.1-SNAPSHOT</version>
   
  <properties>  
      <spring.version>3.2.8.RELEASE</spring.version>  
  </properties>  

  <dependencies>  
      <!-- 添加provider的jar包 -->  
      <dependency>  
          <groupId>com.broadtech</groupId>  
          <artifactId>provider</artifactId>  
          <version>0.0.1-SNAPSHOT</version>  
      </dependency>  
      <!-- 添加dubbo依赖 -->  
      <dependency>  
          <groupId>com.alibaba</groupId>  
          <artifactId>dubbo</artifactId>  
          <version>2.5.3</version>  
          <exclusions>  
              <exclusion>  
                  <groupId>org.springframework</groupId>  
                  <artifactId>spring</artifactId>  
              </exclusion>  
          </exclusions>  
      </dependency>  
      <!-- 添加zk客户端依赖 -->  
      <dependency>  
          <groupId>com.github.sgroschupf</groupId>  
          <artifactId>zkclient</artifactId>  
          <version>0.1</version>  
      </dependency>  
      <!-- spring相关 -->  
      <dependency>  
          <groupId>org.springframework</groupId>  
          <artifactId>spring-core</artifactId>  
          <version>${spring.version}</version>  
      </dependency>  
      <dependency>  
          <groupId>org.springframework</groupId>  
          <artifactId>spring-beans</artifactId>  
          <version>${spring.version}</version>  
      </dependency>  
      <dependency>  
          <groupId>org.springframework</groupId>  
          <artifactId>spring-context</artifactId>  
          <version>${spring.version}</version>  
      </dependency>  
      <dependency>  
          <groupId>org.springframework</groupId>  
          <artifactId>spring-jdbc</artifactId>  
          <version>${spring.version}</version>  
      </dependency>  
      <dependency>  
          <groupId>org.springframework</groupId>  
          <artifactId>spring-web</artifactId>  
          <version>${spring.version}</version>  
      </dependency>  
      <dependency>  
          <groupId>org.springframework</groupId>  
          <artifactId>spring-webmvc</artifactId>  
          <version>${spring.version}</version>  
      </dependency>  
      <dependency>  
          <groupId>org.springframework</groupId>  
          <artifactId>spring-aop</artifactId>  
          <version>${spring.version}</version>  
      </dependency>  
      <dependency>  
          <groupId>org.springframework</groupId>  
          <artifactId>spring-tx</artifactId>  
          <version>${spring.version}</version>  
      </dependency>  
      <dependency>  
          <groupId>org.springframework</groupId>  
          <artifactId>spring-orm</artifactId>  
          <version>${spring.version}</version>  
      </dependency>  
      <dependency>  
          <groupId>org.springframework</groupId>  
          <artifactId>spring-context-support</artifactId>  
          <version>${spring.version}</version>  
      </dependency>  
      <dependency>  
          <groupId>org.springframework</groupId>  
          <artifactId>spring-test</artifactId>  
          <version>${spring.version}</version>  
      </dependency>  
      <dependency>  
          <groupId>org.springframework</groupId>  
          <artifactId>spring-jms</artifactId>  
          <version>${spring.version}</version>  
      </dependency>  
  </dependencies>  
</project>



这就是调用服务提供方的方法。



3.查看dubbo-admin,  管理所有的服务与消费。

1.安装tomcat,一定要安装版,避免启动失败。

https://tomcat.apache.org/download-70.cgi#7.0.88



默认安装后,是启动状态。



先停止了, 把默认的端口改为8097.( 因为zookeeper的端口也是8080.避免重复)

修改端口的路径在D:\Program Files\Apache Software Foundation\Tomcat 7.0\conf\server.xml



然后把dubbo-admin-2.5.4-SNAPSHOT.war 放到tomcat的 webapp下面。 war包下载地址(下载后是zip结尾,直接把zip后缀改为war包)

http://www.itmayun.com/it/files/226631678709806/resource/901920001882583/1.html


然后启动tomcat


直接访问:

http://localhost:8097/dubbo-admin-2.5.4-SNAPSHOT/

首次会让设置密码,直接输入root/root




相关文章
相关标签/搜索