Java分布式组件--dubbo(springboot集成dubbo)

dubbo

Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和 Spring框架无缝集成。

下载安装zookeeper点击官网下载

windows 下安装运行zookeeper
1.解压后进入conf文件夹新增zoo.cfg文件
2.在zookeeper-3.4.11文件夹下新建data文件夹和logs文件夹
3.在zoo.cfg中添加下面配置,路径按照本机的来

tickTime=2000
dataDir=E:\zookeeper-3.4.11\data
dataLogDir=E:\zookeeper-3.4.11\logs
clientPort=2181

4.启动zookeeper bin 目录下 zkServer.cmd 执行即可

spring-boot-starter-dubbo

dubbo 和 springboot的集成组件 githu官方地址

创建项目

dubbo-api – dubbo服务暴露接口
dubbo-customer –dubbo服务消费者
dubbo-provider –dubbo服务提供者
Java分布式组件--dubbo(springboot集成dubbo)_第1张图片

dubbo-api

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.0modelVersion>
    <parent>
        <groupId>top.hahawagroupId>
        <artifactId>Spring-boot-dubboartifactId>
        <version>0.0.1-SNAPSHOTversion>
    parent>
    <artifactId>dubbo-apiartifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
    dependencies>
project>
package top.hahawa.dubbo.service;

public interface IHelloService {
    String echo(String str);
}

dubbo-provider

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.0modelVersion>
    <parent>
        <groupId>top.hahawagroupId>
        <artifactId>Spring-boot-dubboartifactId>
        <version>0.0.1-SNAPSHOTversion>
    parent>
    <artifactId>dubbo-providerartifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>

        <dependency>
            <groupId>com.gitee.regergroupId>
            <artifactId>spring-boot-starter-dubboartifactId>
            <version>1.0.7version>
        dependency>

        <dependency>
            <groupId>top.hahawagroupId>
            <artifactId>dubbo-apiartifactId>
            <version>0.0.1-SNAPSHOTversion>
        dependency>

    dependencies>
project>

aaplication.yml

server:
  port: 8090
spring:
  dubbo:
    base-package: top.hahawa.provider.serviceImpl
    application:
      name: provider_1
    registry:
      address: zookeeper://127.0.0.1:2181
    protocol:
      name: dubbo
      port: 20881
    #可选 性能调优 负载均衡策略,可选值:random,roundrobin,leastactive,分别表示:随机,轮循,最少活跃调用 
    service:
      loadbalance: random
package top.hahawa.provider.serviceImpl;

import com.alibaba.dubbo.config.annotation.Service;

import top.hahawa.dubbo.service.IHelloService;

@Service(version = "1.0.0")
public class HelloServiceImpl implements IHelloService {

    @Override
    public String echo(String str) {
        return "this is provider response: " + str;
    }

}

dubbo-customer

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.0modelVersion>
    <parent>
        <groupId>top.hahawagroupId>
        <artifactId>Spring-boot-dubboartifactId>
        <version>0.0.1-SNAPSHOTversion>
    parent>
    <artifactId>dubbo-customerartifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>

        <dependency>
            <groupId>com.gitee.regergroupId>
            <artifactId>spring-boot-starter-dubboartifactId>
            <version>1.0.7version>
        dependency>

        <dependency>
            <groupId>top.hahawagroupId>
            <artifactId>dubbo-apiartifactId>
            <version>0.0.1-SNAPSHOTversion>
        dependency>

    dependencies>
project>

application.yml

server:
  port: 80
spring:
  dubbo:
    application:
      name: consumer
    registry:
      address: zookeeper://127.0.0.1:2181
package top.hahawa.customer.web;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;

import top.hahawa.dubbo.service.IHelloService;

@RestController
@RequestMapping("")
public class HelloController {

    @Reference(version = "1.0.0")
    public IHelloService helloService;

    @GetMapping("hello")
    public Object hello(String msg) {
        return helloService.echo(msg);
    }
}

源码下载

你可能感兴趣的:(spring-boot)