springboot整合dubbo

一、添加maven

<dependency>
	<groupId>com.alibaba.spring.boot</groupId>
	<artifactId>dubbo-spring-boot-starter</artifactId>
	<version>2.0.0</version>
</dependency>
<dependency>
	<groupId>com.101tec</groupId>
	<artifactId>zkclient</artifactId>
	<version>0.10</version>
</dependency>

二,provider编码

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

@Service("helloService")
public class HelloServiceImpl implements HelloService {
    public String sayHello(String name) {
        return String.format("Hello %s. Response form provider: %s",
                name, RpcContext.getContext().getLocalAddress());
    }
}

 1、通过代码发布

@SpringBootApplication
public class DubboProviderApp {
    private static String zookeeperHost = System.getProperty("zookeeper.address", "127.0.0.1");
    public static void main(String[] args) {
        ServiceConfig<HelloService> service = new ServiceConfig<>();
        service.setApplication(new ApplicationConfig("first-dubbo-provider"));
        RegistryConfig registryConfig=new RegistryConfig("zookeeper://" + zookeeperHost + ":2181");
        registryConfig.setVersion("1.0");
        service.setRegistry(registryConfig);
        service.setInterface(HelloService.class);
        service.setRef(new HelloServiceImpl());
        service.export();
        System.out.println("dubbo service started");
        try {
            new CountDownLatch(1).await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

 2、通过配置xml发布

dubbo.container=spring
dubbo.application.name=33.2222
dubbo.application.owner=zjhcsoft
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.monitor.protocol=registry
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <context:component-scan base-package="com.fullmark.dubbo"/>

    <dubbo:service interface="com.fullmark.dubbo.HelloService"
                   ref="helloService" version="1.0">
        <dubbo:method name="sayHello" />
    </dubbo:service>

    <dubbo:service interface="com.fullmark.dubbo.UopService"
                   ref="uopService" version="1.0">
        <dubbo:method name="uopServiceCall"/>
    </dubbo:service>

</beans>
@SpringBootApplication
@ImportResource("classpath:dubbo-provider.xml")
public class DubboProviderApp {
    public static void main(String[] args) {
        SpringApplication.run(DubboProviderApp.class, args);
    }
}

 

三、consumer编码

1、通过代码获取

@SpringBootApplication
public class DubboConsumerApp {
    public static void main(String[] args) {
        ReferenceConfig<HelloService> reference =   new ReferenceConfig<HelloService>();
        reference.setApplication(new ApplicationConfig("first-dubbo-consumer"));
        RegistryConfig registryConfig = new RegistryConfig("zookeeper://127.0.0.1:2181");
        registryConfig.setVersion("1.0");
        reference.setRegistry(registryConfig);
        reference.setInterface(HelloService.class);
        HelloService helloService =  reference.get();
        System.out.println(helloService.sayHello("你好"));
    }
}

2、通过xml配置获取

 

dubbo.container=spring
dubbo.application.name=33.2222
dubbo.application.owner=zjhcsoft
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.monitor.protocol=registry
<?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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <context:component-scan base-package="com"/>

    <dubbo:consumer check="false" timeout="300000"/>

    <dubbo:reference id="uopService"
                     interface="com.pubinfo.uop.dubbo.api.UopService"
                     version="${app.bss.dubbo.producer.version}">
        <dubbo:method name="uopServiceCall" servicecode="sc001"/>
    </dubbo:reference>


</beans>

 

@ImportResource({"classpath:dubbo.xml"})
@SpringBootApplication
public class DubboConsumerApplication implements CommandLineRunner {
    @Resource
    private HelloService hellpService;

    public static void main(String[] args) {
        SpringApplicationBuilder builder = new SpringApplicationBuilder(FzpZjdxApplication.class);
        builder.bannerMode(Banner.Mode.LOG).run(args);
        builder.addCommandLineProperties(false);
    }

    @Override
    public void run(String... args) throws Exception {
        SignalApplication.startModule(args);
        SignalApplication.shutdownEventRegistration();
        System.out.println(JacksonUtil.getJsonFromObject(hellpService.sayHello("你好")));
    }
}

 备注:dubbo消费者如果是远程调用其他厂家的dubbo服务,根据接口创建包名与接口文件就可以获取到实例啦。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值