学习笔记--Dubbo

Dubbo学习笔记

Dubbo是什么

Dubbo是:

  • 一款分布式服务框架
  • 高性能和透明化的RPC远程服务调用方案
  • SOA服务治理方案

ZooKeeper服务注册中心

下载 ZooKeeper ,地址 http://www.apache.org/dyn/closer.cgi/zookeeper
解压,修改zoo_sample.cfg 重命名为zoo.cfg

tar zxvf zookeeper-3.4.8.tar.gz
cd zookeeper-3.3.6/conf
vim zoo.cfg

zoo.cfg主要配置

tickTime=2000
dataDir=/javaee/zookeeper/data 
dataLogDir=/javaee/zookeeper/log
clientPort=2181

启动zookeeper

cd zookeeper-3.3.6/bin
./zkServer.sh start

搭建Dubbo Admin,Dubbo的管理界面

从GitHub下载源码,位置https://github.com/apache/incubator-dubbo-ops
只需要dubbo-admin项目,导入工程,打成war包放到tomcat下跑

创建服务提供者

创建SpringBoot项目
pom.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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.qyluo</groupId>
    <artifactId>dubbo-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>dubbo-demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.0.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.8</java.version>
    </properties>

    <dependencies>
        <!-- Spring Boot Dubbo 依赖 -->
        <dependency>
            <groupId>io.dubbo.springboot</groupId>
            <artifactId>spring-boot-starter-dubbo</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.6</version>
        </dependency>

        <!-- Spring Boot Web 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Spring Boot Test 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/log4j/log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>


        <!-- Junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

application.properties

## Dubbo 服务提供者配置
spring.dubbo.application.name=provider
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
spring.dubbo.scan=com.qyluo.dubbodemo.service

实体类Person

package com.qyluo.dubbodemo.service;

import com.qyluo.dubbodemo.entity.Person;

public interface FindPerson {
    Person findPersonByName(String name);
}

服务接口FindPerson

package com.qyluo.dubbodemo.service;

import com.qyluo.dubbodemo.entity.Person;

public interface FindPerson {
    Person findPersonByName(String name);
}

FindPerson实现类

package com.qyluo.dubbodemo.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.qyluo.dubbodemo.entity.Person;

/**
 * @author qiyao.luo
 * @create 2018/7/27
 */
@Service
public class FindPersonImpl implements FindPerson {
    @Override
    public Person findPersonByName(String name) {
        return new Person(name, 10);
    }
}

启动应用

创建服务消费者

SpringBoot项目
pom.xml和服务提供者依赖一样
application.properties

server.port=8081

## Dubbo 服务消费者配置
spring.dubbo.application.name=consumer
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.scan=com.qyluo.dubbodemo.service
spring.dubbo.protocol.name=dubbo

需要在和服务提供者接口相同的包路径下创建FindPerson接口

package com.qyluo.dubbodemo.service;

import com.qyluo.dubbodemo.entity.Person;

public interface FindPerson {
    Person findPersonByName(String name);
}

服务调用的类FindPersonConsumer

package com.qyluo.dubbodemo.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qyluo.dubbodemo.service.FindPerson;
import org.springframework.stereotype.Service;

/**
 * @author qiyao.luo
 * @create 2018/7/27
 */
@Service
public class FindPersonConsumer {

    @Reference
    FindPerson findPerson;

    public void printPerson() {
        String name = "xiaoli";
        System.out.println(findPerson.findPersonByName(name));
    }
}

创建controller

package com.qyluo.dubbodemo.controller;

import com.qyluo.dubbodemo.service.impl.FindPersonConsumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author qiyao.luo
 * @create 2018/7/27
 */
@RestController
public class SampleController {

    @Autowired
    FindPersonConsumer findPersonConsumer;

    @RequestMapping("/test")
    public void testDubbo() {
        findPersonConsumer.printPerson();
    }
}

访问http://localhost:8081/test

posted @ 2018-08-22 11:38  KioLuo  阅读(151)  评论(0编辑  收藏  举报