美文网首页
Dubbo入门学习四:SpringBoot下使用XML配置方式

Dubbo入门学习四:SpringBoot下使用XML配置方式

作者: 哪吒小子 | 来源:发表于2019-01-08 23:28 被阅读8次

话不多说!先上我的代码:Demo
设计的子项目有:provider , consumertest-dubbo-boot-api



其中:

test-dubbo-boot-api:是暴露的接口。
provider:服务的提供者,它实现了接口。
consumer:服务的消费者。

代码实现

1.暴露的接口:test-dubbo-boot-api

public interface DemoService {
    String sayHello(String name);
}

2.服务提供者:provider

DubboDemoServiceImpl

@Service
public class DubboDemoServiceImpl implements DemoService {
    @Override
    public String sayHello(String name) {
        System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
        return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress();
    }
}

resources/dubbo/dubbo-provider.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    <!-- provider's application name, used for tracing dependency relationship -->
    <dubbo:application name="demo-provider"/>
    <!-- use multicast registry center to export service -->
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>
    <!-- use dubbo protocol to export service on port 20880 -->
    <dubbo:protocol name="dubbo" port="20880"/>
    <!-- service implementation, as same as regular local bean -->
    <bean id="demoService" class="com.nezha.provider.api.DubboDemoServiceImpl"/>
    <!-- declare the service interface to be exported -->
    <dubbo:service version="1.0.0" interface="com.nezha.test.dubbo.DemoService" ref="demoService"/>
</beans>

ProviderApplication

@SpringBootApplication
@ImportResource("classpath:dubbo/*.xml")
public class ProviderApplication {
    public static void main(String[] args) throws Exception{
        SpringApplication.run(ProviderApplication.class, args);
        System.in.read();
    }
}

3.服务消费者:consumer

resources/dubbo/dubbo-consumer.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    <!-- consumer's application name, used for tracing dependency relationship (not a matching criterion),
    don't set it same as provider -->
    <dubbo:application name="demo-consumer"/>
    <!-- use multicast registry center to discover service -->
    <dubbo:registry protocol="zookeeper" address="${zookeeper.connect}"/>
    <!-- generate proxy for the remote service, then demoService can be used in the same way as the
    local regular interface -->
    <dubbo:reference scope="remote" version="${dubbo.provider.version}" id="demoService" check="false" interface="com.nezha.test.dubbo.DemoService"/>
</beans>

resources/dubbo.properties

dubbo.provider.version=1.0.0
zookeeper.connect=127.0.0.1:2181

PropertiesConfig

//2.第二种方式
@Configuration
@PropertySource("classpath:dubbo.properties")
@ImportResource({"classpath:dubbo/*.xml"})
public class PropertiesConfig {
}

ConsumerApplication

@SpringBootApplication
//1.第一种方式,如果直接使用xml方式的话,注释掉配置类:PropertiesConfig
//@ImportResource("classpath:dubbo/consumer.xml")
public class ConsumerApplication {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(ConsumerApplication.class, args);
        DemoService demoService = (DemoService) ctx.getBean("demoService");
        while (true) {
            try {
                Thread.sleep(1000);
                // call remote method
                String hello = demoService.sayHello("world, NEZHA");
                // get result
                System.out.println(hello);
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }
    }
}

参考文献

  1. spring boot配置dubbo(XML)
  2. Spring Boot 中使用 Dubbo 详解

相关文章

网友评论

      本文标题:Dubbo入门学习四:SpringBoot下使用XML配置方式

      本文链接:https://www.haomeiwen.com/subject/nbcplqtx.html