Springboot整合Dubbo 坑

`先说遇到的问题

1.zookeeper需要打包到tomcat运行
2.提供者需要开启`@EnableDubbo注解(网上很多都没说),不开在注册中心找 不到,但是消费者可以
3.版本之前用的1.5.9但是启动一直报错,换成2.0.3解决了

<parent> 
  <groupId>org.springframework.boot</groupId> 
  <artifactId>spring-boot-starter-parent</artifactId>
   	<version>2.0.3.RELEASE</version> 
   	<relativePath /> <!-- lookup    parent from repository --> 	
</parent>

4.服务提供者和消费的Service路径要一致(可以看看我的包,本来是Srping_Dubbo_Privider换成了Srping_Dubbo_Customer),不然@Reference(version = "1.0.0") public ProviderService service;为空,报空指针

下面是代码以及信息

1.zookeeper下载安装开启(网上很多)
2.提供者代码

package com.study.SpringData.SpringBoot_Dubbo_Customer.Service.impl;

import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Service;
import com.study.SpringData.SpringBoot_Dubbo_Customer.Service.ProviderService;


/* 
  * @auther  xxx
  * @Date    2019-05-10 10:11
  */
@Service(version = "1.0.0")
@Component
public class ProviderServiceImpl implements ProviderService{

	/* (non-Javadoc)
	 * @see com.study.SpringData.SpringBoot_Dubbo.Service.impl.ProviderService#sayHello(java.lang.String)
	 */
	@Override
	public String sayHello(String name) {
		// TODO Auto-generated method stub
		return "Hello zarf的"+name;
	}}

3.提供者配置`

server.port: 8081
spring.dubbo.application.name=service.provider
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
spring.dubbo.scan=com.study.SpringData.SpringBoot_Dubbo_Customer.Service

4.消费者配置

server.port: 8082
spring.dubbo.application.name=service-consumer
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.scan=com.study.SpringData.SpringBoot_Dubbo_Customer.Service

5.消费者代码

/**
 * 
 */
package com.study.SpringData.SpringBoot_Dubbo_Customer.Controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.study.SpringData.SpringBoot_Dubbo_Customer.Service.ProviderService;

/* 
  * @auther  xxx
  * @Date    2019-05-10 10:14
  */

@RestController
public class DubboController{

    // 使用兼容注入,可以使用dubbo原生注解@Reference注入
    @Reference(version = "1.0.0")
    public ProviderService service;

    @GetMapping(value = "/hello/{name}")
    public String hello(@PathVariable String name){
    	System.out.println("name="+name);
        return service.sayHello(name);
    }
}

6.项目依赖

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.3.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>

		<!--spring-boot-starter-dubbo -->
		<dependency>
			<groupId>com.gitee.reger</groupId>
			<artifactId>spring-boot-starter-dubbo</artifactId>
			<version>1.1.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.4.10</version>
			<exclusions>
				<exclusion>
					<groupId>org.slf4j</groupId>
					<artifactId>slf4j-log4j12</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
			<exclusions>
				<exclusion>
					<groupId>org.springframework.boot</groupId>
					<artifactId>spring-boot-starter-logging</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>

结果
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值