springboot整合duubo有三种整合方式
这主要讲用注解和application.yml来配置开发dubbo

添加依赖

<!-- dubbo -->
		<dependency>
		    <groupId>com.alibaba.boot</groupId>
		    <artifactId>dubbo-spring-boot-starter</artifactId>
		    <version>0.2.0</version>
	    </dependency>

只需要添加和这个,你不用再添加比如zookeeperzk客户端,它自己默认添加上了,客户端用的curator

OCP集群 spring boot Golang zookeeper ES springboot连接 zookeeper集群_User

提供者(provider,也可以认为是服务端)

提供者整合的SSM中service层和Mybatis层,暴露的接口是service的接口

1、目录结构

OCP集群 spring boot Golang zookeeper ES springboot连接 zookeeper集群_ide_02

2、application.yml

OCP集群 spring boot Golang zookeeper ES springboot连接 zookeeper集群_User_03


解释的非常详细

下面是实际的代码

#服务器配置
server:
  port: 8090
#spring配置
spring:
  #数据源配置(默认数据源为HikariDataSource)
  datasource:
    #配置mysql数据库
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password:     
#配置mybatis框架
mybatis:
  #定义Mapper接口映射文件位置 
  mapper-locations: classpath:mapper/*.xml
  #定义实体类位置
  type-aliases-package: com.ssm.pojo
demo: 
  service: 
    version: 1.0.0  #自定义属性,版本号
dubbo:
  application:
    name: provider  #服务唯一ID,不要重复
  protocol:
    name: dubbo     #dubbo传输
    port: 20880     #端口默认20880
  registry:
  #zookeeper注册中心,我这是集群,从哪个地址进都一样,当然,为了容错性,把所有的都加上也行
    address: zookeeper://192.168.137.131:2181  
  provider:
    timeout: 1000    #提供者超时时间
#控制台打印sql语句
logging:
  level:
    com.ming.ssm.mapper: debug
3、启动类加注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
@EnableDubbo   //添加注解,知道开启dubbo
@SpringBootApplication()
public class DubboProviderApplication {

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

}
4、serice层的接口和实现类

接口,也就是需要暴露的接口

public interface UserService {
		List<User> getAllUser() throws Exception;
}

实现类

import com.alibaba.dubbo.config.annotation.Service; //这个要特别提醒,是这个包的service,不再是Spring的那个service

@Service(version = "${demo.service.version}")   //加上版本号,不容易错误
@Transactional
public class UserServiceImpl implements UserService{
	@Autowired
	UserMapper userMapper;

	@Override
	public List<User> getAllUser() throws Exception {
		List<User> list = null;
			try {
				 list = userMapper.getAllUser();			
				return list;
			} catch (Exception e) {
				 TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
	
		return list;
	}
	
}

关于mapper类和配置文件这就不写了,自己随便拿来一个就行

传输对象的话,比如上面的User,这个bean要实现Serializable接口

消费者(consumer,也可以叫调用者)

1、目录结构

OCP集群 spring boot Golang zookeeper ES springboot连接 zookeeper集群_ide_04


我这返回的页面是jsp,springboot如果想支持jsp的话需要添加依赖

<!-- 设置tomcat支持jsp -->
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
		</dependency>
		<!-- 定义jsp标准标签库包  -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
		</dependency>

官方不推荐用jsp,我这也就是做个测试用

2、application.yml

OCP集群 spring boot Golang zookeeper ES springboot连接 zookeeper集群_spring_05


配置如下

#服务器配置
server:
  port: 8091
#spring配置
spring: 
  resources:
    static-locations: classpath:/templates/,classpath:/static/ 
      #配置JSP视图
  mvc:
    view:
      prefix: /WEB-INF/jsp/
      suffix: .jsp   
  devtools:   
    livereload:
      port: 35730    #热部署端口换一个,提供者已经用了默认的热部署端口了      
demo: 
  service: 
    version: 1.0.0  #自定义属性,版本号
dubbo:
  application:
    name: consumer  #服务唯一ID,不要重复
  registry:
  #zookeeper注册中心,我这是集群,从哪个地址进都一样,当然,为了容错性,把所有的都加上也行
    address: zookeeper://192.168.137.131:2181
3、启动类加注解(和上面的provider一样)
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
@EnableDubbo
@SpringBootApplication
public class DubboConsumerApplication {

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

}
controller类在接口上加注解
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.alibaba.dubbo.config.annotation.Reference;
import com.ssm.pojo.User;
import com.ssm.service.UserService;

/**
 * @author 作者
 * @data 2019年8月1日 
 */
@Controller
public class UserController {
		@Reference(version = "${demo.service.version}") //这不再用	@Autowired,因为你没实现类怎么注入
		private UserService userService;
		
		@RequestMapping("findAll")
		public String findAll(Model model) throws Exception {
			List<User> list = userService.getAllUser();
			model.addAttribute("list",list);			
			for (int i = 0; i < list.size(); i++) {
			//打印结果,是否传过来了
				System.out.println(list.get(i).toString());
			}

			return "index";
			
		}
}

测试

OCP集群 spring boot Golang zookeeper ES springboot连接 zookeeper集群_spring_06


成功

遇到的问题

在消费者加载的时候出现WARN:Unable to start LiveReload server
解决就是上面给热部署重新弄一个端口

Unable to start LiveReload server

还有一个错误不知道,不过没什么影响
[DUBBO] The BeanDefinition[Root bean: class [com.alibaba.dubbo.config.spring.ServiceBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] of ServiceBean has been registered with name : ServiceBean:userServiceImpl:com.ssm.service.UserService:${demo.service.version}, dubbo version: 2.6.2, current host: 192.168.137.1