Springboot+Dubbo+ Zookeeper分布式服务搭建

示例

  • 项目构建
        新建一个Maven工程,该工程为父工程,只要包含两个模块,一个是服务提供方模块dubbo-provider,另一个是服务消费方dubbo-consumer。父工程主要引入相关依赖,其pom文件为:
<?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.hyn.dubbo</groupId>
    <artifactId>dubbo-demo</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>dubbo-provider</module>d
        <module>dubbo-consumer</module>
    </modules>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>com.alibaba.spring.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!-- zookeeper client依赖 -->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>
    </dependencies>
</project>
  • 服务提供方
    在服务提供主要包括两部分,一个是暴露服务,一个是服务实现。
    暴露服务:即我们平常开发中所使用的的接口,这里我们创建一个 ProviderService 的接口,主要包括一个返回字符串方法sayHello。
package com.hyn.dubbo.provider.service;

public interface ProviderService {
    String sayHello(String name);
}

    服务实现:服务实现,与我们平常的服务一样,对接口进行实现,比较特别的是,我们这里需要使用到Dubbo 的 @Service 注解

package com.hyn.dubbo.provider.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.hyn.dubbo.provider.service.ProviderService;
import org.springframework.stereotype.Component;
/**
* interfaceClass 指定当前要暴露的服务
**/
@Service(interfaceClass = ProviderService.class)
@Component
public class ProviderServiceImpl implements ProviderService {
    @Override
    public String sayHello(String name) {
        return "Hello " + name;
    }
}

工程启动服务ProviderApplication

package com.hyn.dubbo.provider;

import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.IOException;

@SpringBootApplication
@EnableDubboConfiguration
public class ProviderApplication{
    public static void main(String[] args) throws IOException {
        SpringApplication.run(ProviderApplication.class, args);
    }
}

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">
    <parent>
        <artifactId>dubbo-demo</artifactId>
        <groupId>com.hyn.dubbo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-provider</artifactId>

</project>

applicatioon.properties

server.port=8080 --工程启动端口
spring.dubbo.application.name=provider --服务名称
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181 --注册中心地址
spring.dubbo.protocol.name=dubbo --协议名称(dubbo 协议)
spring.dubbo.protocol.port=20880 --协议端口

总体架构


总体架构
  • 服务消费方
    服务调用
package com.hyn.dubbo.consumer.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.hyn.dubbo.provider.service.ProviderService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConsumerController {

     //用dubbo的@Reference注解去连接提供者提供服务
    @Reference
    private ProviderService providerService;

    @RequestMapping("/hello/{name}")
    public String sayHello(@PathVariable String name) {
        return providerService.sayHello(name);
    }
}

工程启动服务ConsumerApplication

package com.hyn.dubbo.consumer;

import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDubboConfiguration
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }
}

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">
    <parent>
        <artifactId>dubbo-demo</artifactId>
        <groupId>com.hyn.dubbo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>consumer</artifactId>
    <dependencies>
        <dependency>
            <groupId>com.hyn.dubbo</groupId>
            <artifactId>dubbo-provider</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

application.properties

server.port=8081 --工程启动端口
server.context-path=/
spring.dubbo.application.name=consumer --服务名称
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181 --注册中心地址
spring.dubbo.protocol.name=dubbo --协议名称(dubbo 协议)
spring.dubbo.protocol.port=20880 --协议端口
spring.dubbo.consumer.check=false --启动时检查 
spring.dubbo.reference.loadbalance=roundrobin --负载均衡(轮询机制)

总体结构


总体结构
  • 结果


    服务提供者

    服务消费者

    调用结果

推荐阅读更多精彩内容