springboot-dubbo整合

一、创建springboot-dubbo-provider maven springboot工程 引入一下pom依赖

    <parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.8.RELEASE</version>
	</parent>
    <properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>com.alibaba.boot</groupId>
			<artifactId>dubbo-spring-boot-starter</artifactId>
			<version>0.1.0</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->
		<dependency>
			<groupId>com.101tec</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.2</version>
		</dependency>
		<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>
			<scope>test</scope>
		</dependency>

二、编写application.properties/.yml文件

dubbo.application.name=springboot-dubbo-provider
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.scan.base-packages=com.xxx.service
server.port=8888

三、编写启动文件Application.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

四、编写provider service接口及其实现类

public interface ProvideService {
    public String sayHello(String name);
}
import com.alibaba.dubbo.config.annotation.Service;
import com.camelot.service.ProvideService;

import org.springframework.stereotype.Component;

@Component
@Service//该注解为dubbo的@service注解
public class ProvideServiceImpl implements ProvideService {

    public String sayHello(String name) {
        return "-------hello" + name;
    }

}

五、启动zookeeper和dubbo-admin-2.8.4,发现提供者可以运行了


六、创建dubbo的消费者maven springboot工程(依赖和dubbo提供者相同)

编写application.properties/.yml文件

dubbo.application.name=springboot-dubbo-consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.scan.base-packages=com.xxx.service
server.port=8888

七、编写consumer service接口及其实现类

public interface ConsumerService {
    public String sayHello(String name);
}
import com.alibaba.dubbo.config.annotation.Reference;
import com.camelot.service.ProvideService;

import org.springframework.stereotype.Service;

@Service//该注解为spring的@service注解
public class ConsumerServiceImpl {
@Reference//dubbo注解 private ProvideService provideService; public String sayHello(String name) { return provideService.sayHello(name); }

此外,还需要引入提供者的service(要求包名相同)

@Service
public interface ProvideService {
    public String sayHello(String name);
}
八、编写consumer的测试类
import com.alibaba.dubbo.config.annotation.Reference;
import com.camelot.service.ProvideService;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTest {
    @Reference
    private ProvideService provideService;

    @Test
    public void getMessage() {
        String sayHello = provideService.sayHello(" John-------");
        System.out.println(sayHello);
    }
}

九、运行consumer工程,发现提供者消费者都在dubbo-admin上显示了


十、代码链接

点击打开链接



  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值