`先说遇到的问题
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>
结果