springboot整合dubbo

1、创建三个工程:api(jar)、service(jar)、test(war)。

2、img-api:

springboot整合dubbo_第1张图片

package com.demo.service;

import java.util.List;
import com.demo.module.Img;

public interface ImgService {
	List selectAll();

}

3、img-service:

       springboot整合dubbo_第2张图片

首先pom加上依赖:


	4.0.0
	com.demo
	img-service
	0.0.1-SNAPSHOT

	
	
		org.springframework.boot
		spring-boot-starter-parent
		1.4.1.RELEASE
	
	
		
			org.springframework.boot
			spring-boot-starter-web
		
		
		
			com.demo
			img-api
			0.0.1-SNAPSHOT
		

		
		
			org.mybatis.spring.boot
			mybatis-spring-boot-starter
			1.1.1
		
		
			mysql
			mysql-connector-java
			5.1.21
		


		
		
			com.alibaba.spring.boot
			dubbo-spring-boot-starter
			2.0.0
		
	

  实现层:


package com.demo.serviceImpl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.demo.mapper.ImgMapper;
import com.demo.module.Img;
import com.demo.service.ImgService;

@Service("imgService")
public class ImgServiceImpl implements ImgService{
	
	@Autowired
	private ImgMapper imgMapper;

	@Override
	public List selectAll() {		
		return imgMapper.selectAll();
	}

}

  properties配置数据库和Mybatis:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/aaa?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=wtyy

mybatis.mapper-locations=classpath*:mapper/*Mapper.xml

#spring.dubbo.application.name=img-service
#spring.dubbo.registry.address=zookeeper://127.0.0.1
#spring.dubbo.registry.port=2181
#spring.dubbo.base-package=com.demo.serviceImpl
#spring.dubbo.protocol.name=dubbo
server.port=8081

  dubbo配置文件provider.xml:



    
    
    
    
    
   
	
	
	
    
    
    

  启动类:


package com.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubboConfig;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;

 @SpringBootApplication
 @MapperScan("com.demo.mapper")
 @ImportResource("classpath:provider.xml")
public class ServiceStart {
	 public static void main(String[] args) {
		SpringApplication.run(ServiceStart.class, args);
	}

}

4、img-test应用:

    springboot整合dubbo_第3张图片

  添加pom依赖:


  4.0.0
  com.demo
  img-test
  0.0.1-SNAPSHOT
  war
  
  
	
		org.springframework.boot
		spring-boot-starter-parent
		1.4.1.RELEASE
	
	
		
			org.springframework.boot
			spring-boot-starter-web
		
		
		
			com.demo
			img-api
			0.0.1-SNAPSHOT
		

	
		
			com.alibaba.spring.boot
			dubbo-spring-boot-starter
			2.0.0
		


	


  properties配置文件:

#spring.dubbo.application.name=img-test
#spring.dubbo.registry.address=zookeeper://127.0.0.1
#spring.dubbo.registry.port=2181
#spring.dubbo.base-package=com.demo.service
#spring.dubbo.protocol.name=dubbo
server.port=8888

配置port是防止先启动service再启动rest出现端口占用问题,但有时候只配port会报错:

springboot整合dubbo_第4张图片

解决方法是加上数据库的配置(虽然没有用,但是不报错了):

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/aaa?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=wtyy

 dubbo配置文件consumer.xml:



    
    
  
   

    
    
    
    

  启动类:


package com.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubboConfig;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;

 @SpringBootApplication
 @EnableDubboConfiguration
 @ImportResource("classpath:consumer.xml")
public class RestStart {
	public static void main(String[] args) {
		SpringApplication.run(RestStart.class, args);
	}

}

先启动service,再启动test,运行如下:

你可能感兴趣的:(spring,boot,dubbo,后端)