Dubbo springboot entry level demo

Keywords: Java Dubbo Apache Zookeeper Spring

1. Dubbo springboot entry level demo

1.1. Preface

  1. The last operation and maintenance friend mentioned to me that their company wanted to do a dubbo gray publishing function, and this function fell to him. In my impression, dubbo should be able to realize specific users' routing to specific servers through extended code, so as to realize the gray-scale function in this respect. But it's entirely up to the operation and maintenance department to do it. Maybe it needs scripts. I'm not sure whether dubbo supports scripts or not. Taking advantage of this, I'll learn the basic functions of dubbo systematically They are all developed with springboot, so you can start with the entry-level dubbo application of springboot

1.2. Producers

1.2.1. Service method and interface

public interface GreetingService {
    String sayHello(String name);
}
import com.tzxylao.dubbo.service.GreetingService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Value;

@Service(version = "1.0.0")
public class GreetingServiceImpl implements GreetingService {

    @Value("${dubbo.application.name}")
    private String serviceName;
    @Value("${dubbo.protocol.port:0}")
    private int port;

    @Override
    public String sayHello(String name) {
        return String.format("[%s][%s] : Hello, %s", serviceName,port, name);
    }
}

1.2.2. Configuration file application.properties

server.port=8082

dubbo.application.name=first-dubbo-provider
dubbo.scan.base-packages=com.tzxylao.dubbo.service

dubbo.protocol.name=dubbo
dubbo.protocol.port=20884
dubbo.registry.address=zookeeper://127.0.0.1:2181

1.2.3. pom

<properties>
        <java.version>1.8</java.version>
        <spring-boot.version>2.1.1.RELEASE</spring-boot.version>
        <dubbo.version>2.7.1</dubbo.version>
        <curator.version>2.8.0</curator.version>
        <zookeeper.version>3.4.6</zookeeper.version>
    </properties>

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

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


        <!-- Dubbo Spring Boot Starter -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.1</version>
        </dependency>

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


        <!-- zookeeper -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>${zookeeper.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>io.netty</groupId>
                    <artifactId>netty</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>${curator.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>${curator.version}</version>
        </dependency>
    </dependencies>
    <repositories>
        <repository>
            <id>apache.snapshots.https</id>
            <name>Apache Development Snapshot Repository</name>
            <url>https://repository.apache.org/content/repositories/snapshots</url>
            <releases>
                <enabled>false</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>

1.2.4. Starting method

@EnableAutoConfiguration
public class DubboApplication {

    public static void main(String[] args) {
        SpringApplication.run(DubboApplication.class, args);
    }

}

1.3. Consumers

1.3.1. The interface is the same as above

1.3.2. pom ditto

1.3.3. Configuration file

server.port=8081

spring.application.name=first-dubbo-consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181

1.3.4. Starting method

@EnableAutoConfiguration
public class DubboConsumerApplication {

    @Reference(version = "1.0.0")
    private GreetingService greetingService;

    public static void main(String[] args) throws InterruptedException {
        SpringApplication.run(DubboConsumerApplication.class, args);
    }

    @Bean
    public ApplicationRunner runner(){
        int count = 0;
        while (true) {
            System.out.println(greetingService.sayHello("World "));
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            count++;
            if (count == 100) {
                break;
            }
        }
        return s -> {
            System.out.println(greetingService.sayHello("World"));
        };
    }

}

1.4. Operation results

...
[first-dubbo-provider][20884] : Hello, World
[first-dubbo-provider][20884] : Hello, World
...

1.5. summary

  1. The key points to be noticed here are scan package strength. Don't get it wrong
  2. @The Refernece annotation is in dubbo package, @ Service is also in dubbo package

Github Address: https://github.com/tzxylao/learn-demo

Posted by keziah on Mon, 25 Nov 2019 14:21:33 -0800