dubbo 整合 springBoot 实践

一、环境信息:

  1、项目依赖:

<properties>
        <java.version>1.8</java.version>
        <spring-boot.version>2.1.3.RELEASE</spring-boot.version>
        <dubbo.version>2.7.0</dubbo.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!-- Spring Boot -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!-- Aapche Dubbo  -->
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-dependencies-bom</artifactId>
                <version>${dubbo.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo</artifactId>
                <version>${dubbo.version}</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>javax.servlet</groupId>
                        <artifactId>servlet-api</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>log4j</groupId>
                        <artifactId>log4j</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

        <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>${dubbo.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>${dubbo.version}</version>
        </dependency>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
        </dependency>
    </dependencies>

    <modules>
        <module>dubbo-provider</module>
        <module>dubbo-consumer</module>
    </modules>

    <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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

  2、配置文件:

server.port=8080
#当前应用名称,用于注册中心计算应用间依赖关系。消费者和提供者应用名不用一样。
spring.application.name=dubbo-provider

#服务注册中心地址
dubbo.registry.address=zookeeper://127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183

#扫描需要登记的服务
dubbo.scan.base-packages=com.mufeng.dubbo.service

#provider 需要配置dubbo 服务的协议与端口
dubbo.protocol.name=dubbo
dubbo.protocol.port=12345

 

二、服务登记、订阅:

@Service(interfaceClass = ServiceApi.class, version = "1.0",protocol = {"protocolConfigDubbo"}, loadbalance = RoundRobinLoadBalance.NAME)
public class SayHelloServiceImpl implements ServiceApi {

    public String sayHello(String message) {
        System.out.println(message);
        return "provider ... ";
    }

    @Configuration
    static class ProviderConfiguration {

        @Bean("protocolConfigDubbo")
        public ProtocolConfig protocolConfigDubbo(){
           return new ProtocolConfig("dubbo", 12345);
        }
    }
@SpringBootApplication
public class DubboConsumerApplication {

    @Reference(interfaceClass = ServiceApi.class, version = "1.0")
    private ServiceApi serviceApi;

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

    @Bean
    public ApplicationRunner applicationRunner(){
        return args -> {
            System.out.println(serviceApi.sayHello("consumer ..."));
        };
    }

} 

 

posted @ 2019-03-10 21:34  吴wuwu  阅读(562)  评论(1编辑  收藏  举报