dubbo + spring boot

来源:互联网 发布:全知视角和限知视角 编辑:程序博客网 时间:2024/05/24 04:20

 

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;

publicclassDemoServiceImplimplements 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:applicationname="hello-world-app" />

 

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

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

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

    <dubbo:registryprotocol="zookeeper"address="127.0.0.1:2181"/>

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

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

 

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

    <dubbo:protocolname="dubbo"port="20880"/>

 

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

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

 

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

    <beanid="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:applicationname="consumer-of-helloworld-app" />

 

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

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

   

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

    <dubbo:registryprotocol="zookeeper"address="127.0.0.1:2181"/>

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

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

 

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

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

 

</beans>

 

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

packagecom.dubbo.boot;

 

importorg.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配置项

publicclassApplicationimplements CommandLineRunner {

 

    @Autowired

    private DemoServicedemoService;

   

    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