spring boot 2.x整合dubbo-xml的方式

spring boot 2.x整合dubbo-xml的方式

传统的项目都是基于xml配置文件的方式集成的dubbo,现在假如要升级框架到spring boot但是还是想用原来的方式整合dubbo,那么这篇文章对你应该有帮助

项目结构

spring boot 2.x整合dubbo-xml的方式_第1张图片

生产者

pom.xml


       <dependency>
            <groupId>com.sunjin.vip.dubbogroupId>
            <artifactId>dubbo-service-providerartifactId>
            <version>1.0.0-SNAPSHOTversion>
dependency>
        <dependency>
            <groupId>org.apache.zookeepergroupId>
            <artifactId>zookeeperartifactId>
            <version>3.4.8version>
        dependency>
        <dependency>
            <groupId>com.101tecgroupId>
            <artifactId>zkclientartifactId>
            <version>0.3version>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>dubboartifactId>
            <version>2.5.3version>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

注意应为我们不需要springboot的自动装配特性因此不要导入 dubbo-sprong-boor-starter dubbo-service-provider是接口,dubbo-service-provider-impl 是现在。

启动类

@SpringBootApplication
@ImportResource(locations = {"classpath:spring-application-dubbo-provider.xml"})
public class DubboServiceProviderImplApplication {

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

}

spring-application-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-2.5.xsd
	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:application name="boot-dubbo-provider-0"/>

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

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

    <dubbo:service interface="com.sunjin.vip.dubbo.service.HelloDubboService" ref="helloService"/>
    
beans>

这样生产者就整合好了。spring-application-dubbo-provider.xml的配置没有调优有需求可以按照公司的架构去配置

消费者

spring boot 2.x整合dubbo-xml的方式_第2张图片

pom.xml

		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-webartifactId>
		dependency>

        <dependency>
            <groupId>com.sunjin.vip.dubbogroupId>
            <artifactId>dubbo-service-providerartifactId>
            <version>1.0.0-SNAPSHOTversion>
        dependency>

        <dependency>
            <groupId>org.apache.zookeepergroupId>
            <artifactId>zookeeperartifactId>
            <version>3.4.8version>
        dependency>
        <dependency>
            <groupId>com.101tecgroupId>
            <artifactId>zkclientartifactId>
            <version>0.3version>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>dubboartifactId>
            <version>2.5.3version>
        dependency>

启动类

@SpringBootApplication
@ImportResource(locations = {"classpath:spring-application-dubbo-consumer.xml"})
public class SpringBootDubboConsumerApplication {

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

}

spring-application-dubbo-consumer.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-2.5.xsd
	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:application name="boot-dubbo-consumer" owner="sunjin"/>

    <dubbo:protocol name="dubbo" />

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

    <dubbo:reference id="helloDubboService"  interface="com.sunjin.vip.dubbo.service.HelloDubboService" check="false"/>
beans>

测试类

@RestController
public class SayHelloController {
    
    @Autowired
    private HelloDubboService helloDubboService;


    @GetMapping("/say")
    public String sayHello(){
        System.out.println("dubbo hello");
        return helloDubboService.sayHelloWord("dubbo");
    }
}

这样消费者也整合好了。同样配置没有调优。

spring boot 整合dubbo xml的方式完成了,这种方式还是比较简单的,但是没有用到 spring boot 的自动装配。不推荐这种方式

你可能感兴趣的:(springboot)