乐趣区

关于dubbo:一SpringBoot-整合-Dubbo-光速入门

0、docker 启动 zk

docker-compose.yml

version: '3'
services:
  zoo1:
    image: zookeeper
    restart: always
    hostname: zoo1
    ports:
      - 2181:2181
docker-compose up -d

1、创立父 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.csp</groupId>
    <artifactId>dubbo-exam</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>dubbo-api</module>
        <module>dubbo-provider</module>
        <module>dubbo-consumer</module>
    </modules>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.7</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.7.7</version>
        </dependency>

        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>4.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>2.8.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.13</version>
            <type>pom</type>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>
    </dependencies>

</project>

2、创立 api maven 工程

package com.csp.dubbo.demo;

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

3、创立服务提供者

Application

package com.csp.dubbo.demo.provider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

TestApiServiceImpl

package com.csp.dubbo.demo.provider.impl;

import com.csp.dubbo.demo.TestApiService;
import org.apache.dubbo.config.annotation.DubboService;

@DubboService(version = "1.0.0", interfaceClass = TestApiService.class)
public class TestApiServiceImpl implements TestApiService {

    @Override
    public String sayHello(String name) {System.out.println("我是 provider,我被调用了");
        return name + "say hello";
    }
}

application.properties

#spring 我的项目名
spring.application.name=dubbo_auto_configuration_provider_demo
#Dubbo provider configuration
dubbo.application.name=dubbo_provider
dubbo.registry.protocol=zookeeper
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
#扫描注解包通过该设置将服务注册到 zookeeper
dubbo.scan.base-packages=com.csp.dubbo.demo.provider

4、创立消费者

Application

package com.csp.dubbo.demo.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

TestController

package com.csp.dubbo.demo.consumer.controller;

import com.csp.dubbo.demo.Test2Service;
import com.csp.dubbo.demo.TestApiService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {@DubboReference(version = "1.0.0")
    TestApiService testApiService;

    @GetMapping("/sayHello")
    public Object sayHello(String name) {String hello = testApiService.sayHello(name);
        return hello;
    }
}

application.properties

#spring 我的项目名
spring.application.name=dubbo_auto_configuration_consumer_demo
#Dubbo provider configuration
dubbo.application.name=dubbo_consumer
dubbo.registry.protocol=zookeeper
dubbo.registry.address=zookeeper://127.0.0.1:2181

server.port=8082

5、总结

  1. provider 和 consumer 必须实现同一个接口。
  2. consumer 设置的 version 也要和 provider 写的 version 统一。
  3. 实际上,provider 是不须要启动 web 容器的!
退出移动版