Dubbo服务的搭建与使用

时间:2023-03-10 01:18:38
Dubbo服务的搭建与使用

官方地址Dubbo.io

Dubbo 主要功能

高并发的负载均衡,多系统的兼容合并(理解不深,不瞎掰了)

Dubbo 主要组成有四部分

Zookeeper(服务注册中心)

Consumer(服务消费方)

Provider(服务提供方)

Admin(管理平台)

搭建一个Demo

1.下载一个zookeeper (3.3.6稳定版)修改conf下的配置文件

拷贝一份zoo_sample.cfg 文件并重命名为zoo.cfg 修改配置

如图:Dubbo服务的搭建与使用

使用的是默认端口号2181

服务提供方service-provider (借鉴官方demo)

服务方由四部分组成 :Dubbo服务的搭建与使用

//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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.pikzas</groupId>
  <artifactId>dubboProvider</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>dubboProvider Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <spring.version>3.1.4.RELEASE</spring.version>
    <slf4j.version>1.6.6</slf4j.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <!-- Spring -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-support</artifactId>
      <version>2.0.8</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-asm</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <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-expression</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <!-- spring end -->
    <!-- log -->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.16</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>${slf4j.version}</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>${slf4j.version}</version>
    </dependency>
    <!-- dubbo -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.5.3</version>
    </dependency>
    <!-- zkclient  -->
    <dependency>
      <groupId>com.github.sgroschupf</groupId>
      <artifactId>zkclient</artifactId>
      <version>0.1</version>
    </dependency>
    <!--  zookeeper -->
    <dependency>
      <groupId>org.apache.zookeeper</groupId>
      <artifactId>zookeeper</artifactId>
      <version>3.3.6</version>
    </dependency>
    <!--jmail-->
    <dependency>
      <groupId>jmail</groupId>
      <artifactId>jmail</artifactId>
      <version>1.0</version>
    </dependency>
    <dependency>
      <groupId>net.spy</groupId>
      <artifactId>spymemcached</artifactId>
      <version>2.11.4</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>dubboProvider</finalName>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.1</version>
        <configuration>
          <source>1.5</source>
          <target>1.5</target>
          <encoding>UTF-8</encoding>
          <failOnError>false</failOnError>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>
<?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="hello-world-app" />
    <!-- 本机 伪集群 测试 -->
    <dubbo:registry  protocol="zookeeper"  address="127.0.0.1:2181" />

    <dubbo:protocol name="dubbo" port="20880" />

    <dubbo:service interface="com.dubboProvider.service.EmailService"
                   ref="emailService" />
    <!-- 和本地bean一样实现服务 -->
    <bean id="emailService" class="com.dubboProvider.service.impl.EmailServiceImpl" />
</beans>
//提供服务的接口:
/**
 * Created by pikzas on 2016/5/5.
 */
public interface EmailService {
    /**
     * 发送邮件的公用方法
     * @param message 发送的邮件
     * @return
     */
    boolean sendEmail(EmailMessage message,SenderInfo senderInfo);
}
//提供服务的接口实现类
/**
 * Created by pikzas on 2016/5/5.
 */
public class EmailServiceImpl implements EmailService {
    /**
     * 发送邮件的共用方法
     * @param message 发送的邮件
     * @return
     */
    public boolean sendEmail(EmailMessage message,SenderInfo senderInfo){
         return flag;
    }
//开启服务的main方法
/**
 * Created by pikzas on 2016/5/5.
 */
public class Start {
    public static void main(String[] args) throws IOException{
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext(new String[]{"applicationProvider.xml"});
        context.start();
        System.out.println("按任意键退出");
        System.in.read();
    }
}怎么切换出去 我日