dubbo + spring boot

 

boot-dubbo-server(dubbo服务)

boot-dubbo-tclinet(dubbo调用)

 

创建maven项目:boot-dubbo-server(服务)和boot-dubbo-tclinet(调用).

添加依赖boot-dubbo-server:

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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

 

  <groupId>com.dubbo.boot</groupId>

  <artifactId>boot-dubbo-server</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <packaging>jar</packaging>

 

  <name>boot-dubbo-server</name>

  <url>http://maven.apache.org</url>

 

  <properties>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  </properties>

 

 <!-- Inherit defaults from Spring Boot-->

    <parent>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-parent</artifactId>

        <version>1.2.5.RELEASE</version>

        <relativePath />

    </parent>

 

    <!-- Add typical dependencies fora web application -->

    <dependencies>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>

       

        <!-- alibaba-->

            <dependency>

                <groupId>com.alibaba</groupId>

                <artifactId>dubbo</artifactId>

                <version>2.5.3</version>

                <exclusions>

                    <exclusion>

                        <groupId>org.springframework</groupId>

                        <artifactId>spring</artifactId>

                    </exclusion>

                </exclusions>

            </dependency>

            <dependency>

                <groupId>com.alibaba</groupId>

                <artifactId>fastjson</artifactId>

                <version>1.1.43</version>

            </dependency>

            <dependency>

                <groupId>com.github.sgroschupf</groupId>

                <artifactId>zkclient</artifactId>

                <version>0.1</version>

            </dependency>

    </dependencies>

 

    <!-- Package as an executable jar-->

    <build>

        <plugins>

            <plugin>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-maven-plugin</artifactId>

            </plugin>

        </plugins>

    </build>

</project>

 

boot-dubbo-tclinet:

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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

 

  <groupId>com.dubbo.boot</groupId>

  <artifactId>boot-dubbo-tclinet</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <packaging>jar</packaging>

 

  <name>boot-dubbo-tclinet</name>

  <url>http://maven.apache.org</url>

 

  <properties>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  </properties>

 

  <!-- Inherit defaults from SpringBoot -->

    <parent>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-parent</artifactId>

        <version>1.2.5.RELEASE</version>

        <relativePath />

    </parent>

 

    <!-- Add typical dependencies fora web application -->

    <dependencies>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>

       

        <!-- alibaba-->

            <dependency>

                <groupId>com.alibaba</groupId>

                <artifactId>dubbo</artifactId>

                <version>2.5.3</version>

                <exclusions>

                    <exclusion>

                        <groupId>org.springframework</groupId>

                        <artifactId>spring</artifactId>

                    </exclusion>

                </exclusions>

            </dependency>

            <dependency>

                <groupId>com.alibaba</groupId>

                <artifactId>fastjson</artifactId>

                <version>1.1.43</version>

            </dependency>

            <dependency>

                <groupId>com.github.sgroschupf</groupId>

                <artifactId>zkclient</artifactId>

                <version>0.1</version>

            </dependency>

    </dependencies>

 

    <!-- Package as an executable jar-->

    <build>

        <plugins>

            <plugin>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-maven-plugin</artifactId>

            </plugin>

        </plugins>

    </build>

</project>

 

首先是boot-dubbo-server:

添加接口DemoService,  和实现DemoServiceImpl:

packagecom.dubbo.boot.dubbo;

 

publicinterfaceDemoService {

 

    String sayHello(String name);

 

}

 

packagecom.dubbo.boot.dubbo;

publicclassDemoServiceImpl implements DemoService {

     

    public StringsayHello(String name) {

        return"Hello" + name;

    }

 

}

添加application.properties(用于修改端口):

server.port=8081


 

 

添加dubbo服务配置provider.xml:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

   xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

   xsi:schemaLocation="http://www.springframework.org/schema/beans       http://www.springframework.org/schema/beans/spring-beans.xsd        http://code.alibabatech.com/schema/dubbo       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

 

    <!-- 提供方应用信息,用于计算依赖关系 -->

    <dubbo:application name="hello-world-app"  />

 

    <!-- 使用multicast广播注册中心暴露服务地址 -->

<!--     <dubbo:registryaddress="multicast://224.5.6.7:1234" /> -->

    <!-- zookeeper注册中心 -->

    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>

    <!-- redis注册中心 -->

<!--     <dubbo:registryprotocol="redis" address="127.0.0.1:6379" /> -->

 

    <!-- dubbo协议在20880端口暴露服务 -->

    <dubbo:protocol name="dubbo" port="20880"/>

 

    <!-- 声明需要暴露的服务接口 -->

    <dubbo:service interface="com.dubbo.boot.dubbo.DemoService" ref="demoService" />

 

    <!-- 和本地bean一样实现服务 -->

    <bean id="demoService" class="com.dubbo.boot.dubbo.DemoServiceImpl"/>

 

</beans>

 

最后添加Application(启动项目):

packagecom.dubbo.boot;

 

importorg.springframework.boot.SpringApplication;

importorg.springframework.boot.autoconfigure.SpringBootApplication;

importorg.springframework.context.annotation.ImportResource;

 

 

//@Configuration

//@EnableAutoConfiguration

//@ComponentScan

@SpringBootApplication// 等同于 @Configuration @EnableAutoConfiguration @ComponentScan三个标签

@ImportResource("classpath:provider.xml")   //导入xml配置项

publicclassApplication {

 

//  @Autowired

//  private StudentMapper stuMapper;

   

    publicstaticvoidmain(String[] args) {

        SpringApplication.run(Application.class, args);

    }

 

//  public void run(String... args) throwsException {

//       System.out.println(stuMapper.getById(1));

//       System.out.println(stuMapper.getNameById(1));

//  }

 

}

 

运行main方法启动服务.


boot-dubbo-tclinet:

添加接口DemoService:

packagecom.dubbo.boot.dubbo;

publicinterfaceDemoService {

     

    String sayHello(String name);

 

application.properties文件:

server.port=8082

添加consumer.xml:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

   xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

   xsi:schemaLocation="http://www.springframework.org/schema/beans       http://www.springframework.org/schema/beans/spring-beans.xsd        http://code.alibabatech.com/schema/dubbo       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

 

    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->

    <dubbo:application name="consumer-of-helloworld-app"  />

 

    <!-- 使用multicast广播注册中心暴露发现服务地址 -->

<!--     <dubbo:registryaddress="multicast://224.5.6.7:1234" /> -->

   

    <!-- zookeeper注册中心 -->

    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>

    <!-- redis注册中心 -->

<!--     <dubbo:registryprotocol="redis" address="127.0.0.1:6379" /> -->

 

    <!-- 生成远程服务代理,可以和本地bean一样使用demoService-->

    <dubbo:reference id="demoService" interface="com.dubbo.boot.dubbo.DemoService" />

 

</beans>

 

最后添加Application(启动项目):

packagecom.dubbo.boot;

 

import org.springframework.beans.factory.annotation.Autowired;

importorg.springframework.boot.CommandLineRunner;

importorg.springframework.boot.SpringApplication;

importorg.springframework.boot.autoconfigure.SpringBootApplication;

importorg.springframework.context.annotation.ImportResource;

 

importcom.dubbo.boot.dubbo.DemoService;

 

 

//@Configuration

//@EnableAutoConfiguration

//@ComponentScan

@SpringBootApplication// 等同于 @Configuration @EnableAutoConfiguration @ComponentScan三个标签

@ImportResource("classpath:consumer.xml")   //导入xml配置项

publicclassApplication implements CommandLineRunner {

 

    @Autowired

    private DemoService demoService;

   

    publicstaticvoidmain(String[] args) {

        SpringApplication.run(Application.class, args);

      

    }

 

 

    publicvoidrun(String... args) throws Exception {

         String hello = demoService.sayHello("world"); // 执行远程方法

         

        System.err.println( hello ); // 显示调用结果

    }

 

}

 

运行mainrun方法启动服务.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值