springboot   dubbo 整合简单的示例(用dubbo的xml配置方法 在xml配置里暴露接口)

和这个有些差别 https://blog.csdn.net/cui163007/article/details/102074599

1  工具

eclipse mar    jkd1.8   maven3.5.3   springboot2.1.0   dubbo2.0.0(dubbo用阿里官方版本)

 

2 创建项目

  1. 父项目--主要是添加公用的依赖pom.xml

Maven项目,只有pom文件

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

 

<parent>

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

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

<version>2.1.0.RELEASE</version>

<relativePath />

</parent>

 

<groupId>com.springboot</groupId>

<artifactId>common</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>pom</packaging>

 

<properties>

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

</properties>

 

<modules>

<module>../example</module>

<module>../dubboconsumer</module>

<module>../dubbotest</module>

</modules>

 

<dependencies>

<!--dubbo-springBoot依赖 -->

<dependency><!--dubbo的官方版本  用这个 -->

<groupId>com.alibaba.spring.boot</groupId>

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

<version>2.0.0</version>

</dependency>

<!-- <dependency>dubbo其他版本

<groupId>io.dubbo.springboot</groupId>

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

<version>1.0.0</version>

</dependency> -->

<!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->

<dependency>

<groupId>org.apache.zookeeper</groupId>

<artifactId>zookeeper</artifactId>

<version>3.4.6</version>

<type>pom</type>

</dependency>

<dependency>

<groupId>com.101tec</groupId>

<artifactId>zkclient</artifactId>

<version>0.10</version>

</dependency>

 

</dependencies>

 

</project>

 

 

 

  1. 创建公用的接口项目,供服务提供者和服务消费者调用

Pom文件不用配置,只有一个接口,服务提供者和服务消费者依赖该项目,实现该接口

package com.springboot.dubboAPI;

public interface IBookService {

    String readBooks();

}

 

  1. 服务提供者

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

<modelVersion>4.0.0</modelVersion>

<parent>

<groupId>com.springboot</groupId>

<artifactId>common</artifactId>

<version>0.0.1-SNAPSHOT</version>

</parent>

 

<groupId>com.srpingboot</groupId>

<artifactId>example</artifactId>

<packaging>war</packaging>

 

<properties>

<java.version>1.8</java.version>

</properties>

 

<dependencies>

<dependency>

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

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

<exclusions>

<exclusion><!--排除springboot自动加载的tomcat -->

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

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

</exclusion>

</exclusions>

</dependency>

<dependency>

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

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

<scope>provided</scope><!--设置在打包时不包含tomcat的包 -->

</dependency>

<dependency>

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

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

<scope>test</scope>

</dependency>

</dependencies>

 

<build>

<plugins>

<plugin>

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

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

</plugin>

</plugins>

</build>

</project>

 

不用application.properties文件

添加dubbo-provider.xml配置文件

<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="springboot-dubbo-provider"/>

 

    <!-- 注册中心配置,使用zookeeper注册中心暴露服务地址 -->

    <dubbo:registry address="zookeeper://192.168.181.128:2181" timeout="60000" />

    

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

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

    

    <!-- 当ProtocolConfig和ServiceConfig某属性没有配置时,采用此缺省值 -->

    <dubbo:provider timeout="30000" threadpool="fixed" threads="500" accepts="1000" />

 

    <!--关闭服务消费方所有服务的启动检查。dubbo缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止Spring初始化完成。-->

    <dubbo:consumer check="false" />

 

    <!-- 使用注解方式暴露接口,会自动扫描package下所有包中dubbo相关的注解,这样就不用在xml中再针对每个服务接口配置dubbo:service interface-->

    <!-- <dubbo:annotation package="com.srpingboot.example"/> -->

    

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

    <dubbo:service interface="com.springboot.dubboAPI.IBookService" ref="bookServiceImpl" />  

    

</beans>

 

实现类

package com.srpingboot.example;

 

import com.alibaba.dubbo.config.annotation.Service;

import com.springboot.dubboAPI.IBookService;

 

//@Service

@org.springframework.stereotype.Service

public class BookServiceImpl implements IBookService {

 

@Override

public String readBooks() {

return "I like read book";

}

 

@Override

public void buyBooks() {

 

}

 

}

 

 

注意该类有两个service注解,一个是alibaba的,一个是spring的,大家要注意区分。

 

启动类

package com.srpingboot.example;

 

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.context.annotation.ImportResource;

 

import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;

 

//@EnableDubboConfiguration

@SpringBootApplication

@ImportResource(value = {"classpath:dubbo-provider.xml"})

public class ProviderApplication {

public static void main(String[] args) {

SpringApplication.run(ProviderApplication.class, args);

}

}

 

 

(4) 服务消费者

<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/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

 

<parent>

<groupId>com.springboot</groupId>

<artifactId>common</artifactId>

<version>0.0.1-SNAPSHOT</version>

</parent>

 

<artifactId>dubboconsumer</artifactId>

<packaging>war</packaging>

<dependencies>

<dependency>

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

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

<exclusions>

<exclusion><!--排除springboot自动加载的tomcat -->

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

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

</exclusion>

</exclusions>

</dependency>

<dependency>

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

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

<scope>provided</scope><!--设置在打包时不包含tomcat的包 -->

</dependency>

</dependencies>

<build>

<finalName>dubboconsumer</finalName>

</build>

</project>

 

application.properties文件

## 避免和提供者项目端口冲突

server.port=8082

 

dubbo-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="springboot-dubbo-consumer"/>

 

        <!-- 注册中心配置,使用zookeeper注册中心暴露服务地址 -->

        <dubbo:registry address="zookeeper://192.168.181.128:2181" timeout="60000" />

        

        <!-- 监控中心配置,protocol="registry",表示从注册中心发现服务地址 -->

     <dubbo:monitor protocol="registry"/>

 

        <!-- 使用注解方式创建远程服务代理-->

      <!--   <dubbo:annotation package="com.srpingboot.example"/> -->

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

     <dubbo:reference id="iBookService" interface="com.springboot.dubboAPI.IBookService" />  

    

</beans>

 

controller

package com.srpingboot.example;

 

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

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RestController;

 

import com.alibaba.dubbo.config.annotation.Reference;

import com.springboot.dubboAPI.IBookService;

 

@RestController

public class DubboController {

 

// @Reference

// private IBookService iBookService;

 

@Autowired

private IBookService iBookService;

 

@GetMapping("test")

public String testDubbo() {

return iBookService.readBooks();

}

 

 

}

 

 

启动类

package com.srpingboot.example;

 

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.context.annotation.ImportResource;

 

//@EnableDubboConfiguration

@ImportResource("classpath:dubbo-consumer.xml")

@SpringBootApplication

public class ConsumerApplication {

public static void main(String[] args) {

SpringApplication.run(ConsumerApplication.class, args);

}

 

}

 

 

 

启动测试

先启动zookeeper,再启动provider,然后启动consumer

访问consumer : http://localhost:8082/test

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值