SpringBoot整合Dubbo超简单 -provider

springBoot整合dubbo的好处是可以放弃xml配置,可以使用注解的方式配置dubbo

DubboStarter分两个版本,旧版的io.dubbo.springboot,还有基于Apache的org.apache.dubbo,这里是用的最新版Apache的DubboStarter

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>test.tomcat</groupId>
    <artifactId>start.up</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <spring-boot.version>2.3.1.RELEASE</spring-boot.version>

    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>${spring-boot.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>3.0.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-registry-zookeeper</artifactId>
            <version>3.0.4</version>
            <exclusions>
                <exclusion>
                        <groupId>org.apache.dubbo</groupId>
                        <artifactId>dubbo-remoting-zookeeper</artifactId>
                </exclusion>

            </exclusions>
        </dependency>
    </dependencies>

</project>

application.properties

dubbo.application.name=provider dubbo.registry.address=zookeeper://127.0.0.1:2181 dubbo.protocol.name=dubbo dubbo.protocol.port=20880

暴露的接口

package com.startup.service;

public interface ISayService {

    String sayHello();
}
package com.startup.service;

import org.apache.dubbo.config.annotation.DubboService;

@DubboService
public class SayServiceImpl implements ISayService {
    public String sayHello() {
        return "hello..........";
    }
}

直接启动就OK了,先启动Zookeeper

package com.startup;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.concurrent.CountDownLatch;

@SpringBootApplication
@EnableDubbo(scanBasePackages = {"com.startup.service"})
public class Application {

    public static void main(String[] args) throws InterruptedException {
        SpringApplication.run(Application.class,args);
        System.out.println("dubbo service started");
        new CountDownLatch(1).await();
    }
}

SpringBoot整合Dubbo超简单 -customer

经验分享 程序员 微信小程序 职场和发展