Dubbo-与SpringBoot整合

与SpringBoot整合的第一种方式,本质还是利用XML配置的方式
项目结构
在这里插入图片描述
Demo的模块依赖关系
在这里插入图片描述
因为是Demo项目,想一切从简,忽略一些细节,突出重点。
先说parent模块,主要是jar包版本管理,然后就是打包,没有代码,只有pom.xml中的配置,需要重点说明的是,在parent中就需要继承SpringBoot 的父包。

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
    </parent>

    <modules>
        <module>../dubbo-api</module>
        <module>../dubbo-common</module>
        <module>../dubbo-facade</module>
    </modules>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <jdk.version>1.8</jdk.version>

        <dubbo.version>2.5.3</dubbo.version>
        <zookeeper.version>3.4.6</zookeeper.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!-- dubbo -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <version>${dubbo.version}</version>
            </dependency>

            <!-- zookeeper -->
            <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
                <version>${zookeeper.version}</version>
            </dependency>
            <dependency>
                <groupId>com.101tec</groupId>
                <artifactId>zkclient</artifactId>
                <version>0.3</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

再说Common模块,主要是一些Pojo,接口的定义,然后基础配置信息,如Zookeeper
在这里插入图片描述

public class User implements Serializable {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Integer id;

    private String name;

    private String password;
    //省略get/set方法
}

定义的一个接口

public interface UserService {
	
	/**
	 * 根据用户ID进行查询
	 */
	public User getUserInfoById(int id);
	
}

然后就是一个配置文件zookeeper.properties,被服务提供方和消费端共同使用

zookeeper.host=192.168.209.101
zookeeper.port=2181

common就这些,主要是抽取了一些公用的代码和配置,方便管理。

然后就是api模块,从Zookeeper获取远程服务。
在这里插入图片描述
先是pom.xml配置

<parent>
        <artifactId>dubbo-parent</artifactId>
        <groupId>dubbo-test</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../dubbo-parent/pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-api</artifactId>

    <name>dubbo-api</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>dubbo-test</groupId>
            <artifactId>dubbo-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!-- 对WEB的支持 -->
        <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>
        </dependency>

        <!-- dubbo 需要的jar start -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

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

        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
        </dependency>
        <!-- dubbo 需要的jar end -->
    </dependencies>

启动类,

@SpringBootApplication
//加载Dubbo配置
@ImportResource({"classpath:dubbo-test-consumer.xml"})
public class ApiApplication {
    public static void main(String[] args) {
        SpringApplication.run(ApiApplication.class, args);
    }
}

需要重点说明的是,要配置加载dubbo的配置文件,配置文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
	xmlns:context="http://www.springframework.org/schema/context"
	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://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-2.5.xsd  
        http://code.alibabatech.com/schema/dubbo  
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
        
	<context:property-placeholder location="classpath:zookeeper.properties"/>
	<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
	<dubbo:application name="dubbo-test-consumer" />

	<!-- 使用zookeeper注册中心暴露服务地址 -->
	<!-- 注册中心地址 -->
	<dubbo:registry protocol="zookeeper" address="${zookeeper.host}:${zookeeper.port}" timeout="10"/>
	
	<!-- 用户服务接口 -->
	<dubbo:reference interface="com.test.dubbo.service.UserService" id="userService" check="false" />
</beans>

需要配置dubbo.xsd文件到idea中,不然会报红。
参考博客:https://blog.csdn.net/qq_26262307/article/details/77222957
写一个Controller用于测试

@RequestMapping("/user")
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    /**
     */
    @RequestMapping(value = "/getUserInfoById/{id}",method = RequestMethod.GET)
    @ResponseBody
    public User getUserInfoById(@PathVariable("id") Integer id) {
        return userService.getUserInfoById(id);
    }
}

SpringBoot的配置文件Application.properties,配置一下端口

server.port=8080

好了Api就完了。只是为了测试远程调用。

然后就是服务的提供方facade模块。
在这里插入图片描述
和Api类似。
pom.xml

 <parent>
        <artifactId>dubbo-parent</artifactId>
        <groupId>dubbo-test</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../dubbo-parent/pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-facade</artifactId>

    <name>dubbo-facade</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>dubbo-test</groupId>
            <artifactId>dubbo-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!-- 对WEB的支持 -->
        <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>
        </dependency>

        <!-- dubbo 需要的jar start -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
        </dependency>
        <!-- dubbo 需要的jar end -->
    </dependencies>

facadeSpringBoot启动类:

@SpringBootApplication//说明是SpringBoot应用
//加载Dubbo配置
@ImportResource({"classpath:dubbo-test-provider.xml"})
public class FacadeApplication {

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

加载dubbo的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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://www.springframework.org/schema/context 
				http://www.springframework.org/schema/context/spring-context-2.5.xsd
				http://code.alibabatech.com/schema/dubbo 
				http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <!-- 引入配置文件 -->
    <context:property-placeholder location="classpath:zookeeper.properties"/>

    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="demo-provider"/>


    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <dubbo:registry protocol="zookeeper" address="${zookeeper.host}:${zookeeper.port}" timeout="10"/>

    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20818"/>

    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.test.dubbo.service.UserService"
                   ref="userService"/>

    <!-- 和本地bean一样实现服务 -->
    <bean id="userService" class="com.test.dubbo.facade.impl.UserServiceImpl"/>
</beans>

服务具体的实现类:

@Service
public class UserServiceImpl implements UserService{
    @Override
    public User getUserInfoById(int id) {
        User user=new User();
        user.setId(id);
        user.setName("姓名");
        user.setPassword("密码");
        return user;
    }
}

然后就是SpringBoot 的基础配置文件application.properties,配置一下端口

server.port=8081

启动API和Facade,然后调用Api的接口/user/getUserInfoById/1.即可验证。

代码可参考:
https://download.csdn.net/download/btwangzhi/11174646

与SpringBoot整合的第二种方式是利用官方提供的start包,具体可参考https://github.com/alibaba/dubbo-spring-boot-starter/blob/master/README_zh.md,我下载下来跑都跑不起来,自己整理一下。
项目结构如下:
在这里插入图片描述
common中定义一个接口

public interface IHelloService {

    public String sayHello(String name);
}

然后就是provider

<dependency>
            <groupId>test</groupId>
            <artifactId>02dubbo-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <!-- 对WEB的支持 -->
        <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>
        </dependency>

        <!--Dubbo-->
        <dependency>
            <groupId>com.alibaba.spring.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.6</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.9</version>
        </dependency>

注意,dubbo-spring-boot-starter这个包。
application.properties

server.port=8080

#服务名
spring.dubbo.application.name=provider
#协议
spring.dubbo.protocol.name=dubbo
#协议端口号
spring.dubbo.protocol.port=20880

#注册中心
spring.dubbo.registry.address=zookeeper://192.168.209.101:2181

启动类:

@SpringBootApplication
//表示要开启dubbo功能.
@EnableDubboConfiguration
public class ProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
    }
}

注意需要添加EnableDubboConfiguration注解
接口实现类:

@Service
@com.alibaba.dubbo.config.annotation.Service(version = "1.0.0")
public class HelloServiceImpl implements IHelloService {
    @Override
    public String sayHello(String name) {
        return "hello "+name;
    }
}

注意添加的是dubbo的Service注解

然后就是consumer,pom文件内容是一样的。
application.properties

server.port=8081

spring.application.name=dubbo-spring-boot-starter
spring.dubbo.application.name=consumer
#注册中心
spring.dubbo.registry.address=zookeeper://192.168.209.101:2181

启动类:

@SpringBootApplication
@EnableDubboConfiguration
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }
}

定义一个测试用的接口

@Controller
@RestController
public class HelloController {

    @Reference(version = "1.0.0")
    private IHelloService iHelloService;

    @RequestMapping(value = "/hello/{name}",method = RequestMethod.GET)
    public Object sayHello(@PathVariable("name")String name){
        return iHelloService.sayHello(name);
    }
}

访问http://localhost:8081/hello/dema即可测试。

参考https://blog.csdn.net/pri_sta_pub/article/details/79087592
https://www.cnblogs.com/jaycekon/p/SpringBootDubbo.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值