基于springboot搭建dubbo框架(注解和xml配置两种方式)

一.基于SpringBoot的xml开发

1.新建maven工程

新建三个maven工程(工程名字是现在项目的名称随便命名的)

1> 服务提供者

  • - dubbo-provider
  • |- src/main/java
  •     |- com.huawei.hicloud
  •         |- config
  •             DubboConfig.java
  •         |- service.impl
  •             ProductServiceImpl.java
  •             DubboProviderApplication.java
  • |- src/main/resources
  •     |- dubbo
  •         dubbo-provider.xml
  •         dubbo.properties
  •     application.yml
  •     bootstrap.yml
代码内容:
com.huawei.hicloud.config.DubboConfig.java
package com.huawei.hicloud.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource("classpath:dubbo/dubbo.properties")
@ImportResource({ "classpath:dubbo/*.xml" })
public class DubboConfig {

}
com.huawei.hicloud.service.impl.ProductServiceImpl.java
package com.huawei.hicloud.service.impl;

import org.springframework.stereotype.Service;

import com.huaewi.hicloud.pojo.Product;
import com.huaewi.hicloud.service.ProductService;

@Service
public class ProductServiceImpl implements ProductService {

	@Override
	public Product findById(String id) {
		Product product = new Product();
		product.setId(id);
		product.setBrand("HUAWEI");
		product.setCategory("TEL");
		product.setName("Honor V10");
		product.setPrice(2699d);
		return product;
	}

}
com.huawei.hicloud.DubboProviderApplication.java
package com.huawei.hicloud;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DubboProviderApplication implements CommandLineRunner {
	
	private static final Logger logger = LoggerFactory.getLogger(DubboProviderApplication.class);
	
	public static void main(String[] args) {
		logger.info("### DubboProviderApplication starter ...");
		SpringApplication.run(DubboProviderApplication.class, args);
	}

	@Override
	public void run(String... arg0) throws Exception {
		logger.info("### Dubbo provider start ok!");
	}

}
/dubbo-provider/src/main/resources/application.yml
server: 
  port: 8080
/dubbo-provider/src/main/resources/bootstrap.yml
none
/dubbo-provider/src/main/resources/dubbo/dubbo-provider.xml



	
	

	
	

	
	

	
	

	
/dubbo-provider/src/main/resources/dubbo/dubbo.properties
#\u5E94\u7528\u540D\u79F0
dubbo.application.name=dubbo-provider
#\u6CE8\u518C\u4E2D\u5FC3\u7C7B\u578B
dubbo.registry.protocol=zookeeper
#\u6CE8\u518C\u4E2D\u5FC3\u5730\u5740
dubbo.registry.address=127.0.0.1:2181
#\u66B4\u9732\u670D\u52A1\u65B9\u5F0F
dubbo.protocol.name=dubbo
#\u66B4\u9732\u670D\u52A1\u7AEF\u53E3
dubbo.protocol.port=20880
pom文件

  4.0.0
  
    org.springframework.boot
    spring-boot-starter-parent
    1.5.2.RELEASE
  
  com.huawei.hicloud
  dubbo-provider
  0.0.1-SNAPSHOT
  
  
  	
  		org.springframework.boot
  		spring-boot-starter-web
  	
  	
  		org.springframework.boot
  		spring-boot-starter-test
  	
  	
  		com.alibaba
  		dubbo
  		2.5.3
  		
  			
  				org.springframework
  				spring
  			
  		
  	
  	
  		org.apache.zookeeper
  		zookeeper
  		3.4.7
  		pom
  	
  	
  		com.github.sgroschupf
  		zkclient
  		0.1
  	
  	
  		com.huawei.hicloud
  		dubbo-service-api
  		0.0.1-SNAPSHOT
  	
  
  
  
  	
  		
  			org.springframework.boot
  			spring-boot-maven-plugin
  		
  		
  			org.apache.maven.plugins
  			maven-compiler-plugin
  			
  				1.8
  				1.8
  			
  		
  	
  

2> 服务消费者

  • - dubbo-consumer
  • |- src/main/java
  •     |- com.huawei.hicloud
  •         |- config
  •             DubboConfig.java
  •         |- controller
  •             ProductController.java
  •         DubboConsumerApplication.java
  • |- src/main/resources
  •     |- dubbo
  •         dubbo-consumer.xml
  •         dubbo.properties
  •     application.yml
  •     bootstrap.yml
文件内容:
com.huawei.hicloud.config.DubboConfig.java
package com.huawei.hicloud.config;


import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource("classpath:dubbo/dubbo.properties")
@ImportResource({ "classpath:dubbo/*.xml" })
public class DubboConfig {
	
}
com.huawei.hicloud.controller.ProductController.java
package com.huawei.hicloud.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.huaewi.hicloud.pojo.Product;
import com.huaewi.hicloud.service.ProductService;

@RestController
public class ProductController {
	
	@Autowired
	private ProductService productService;

	@RequestMapping(value="/products/{id}", method=RequestMethod.GET)
	public Product findById(@PathVariable(value="id") String id) {
		Product product = productService.findById("10001");
		return product;
	}
}
com.huawei.hicloud.DubboConsumerApplication.java
package com.huawei.hicloud;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DubboConsumerApplication {
	
	private static final Logger logger = LoggerFactory.getLogger(DubboConsumerApplication.class);
	
	public static void main(String[] args) {
		logger.info("### DubboProviderApplication starter ...");
		SpringApplication.run(DubboConsumerApplication.class, args);
	}

}

/dubbo-consume/src/main/resources/dubbo/dubbo-consumer.xml



	
	

	
	

	
/dubbo-consume/src/main/resources/dubbo/dubbo.properties
#\u5E94\u7528\u540D\u79F0
dubbo.application.name=dubbo-consumer
#\u6CE8\u518C\u4E2D\u5FC3\u7C7B\u578B
dubbo.registry.protocol=zookeeper
#\u6CE8\u518C\u4E2D\u5FC3\u5730\u5740
dubbo.registry.address=127.0.0.1:2181
/dubbo-consume/src/main/resources/application.yml
server: 
  port: 8081
/dubbo-consume/src/main/resources/bootstrap.yml
none
pom文件:

  4.0.0
  
    org.springframework.boot
    spring-boot-starter-parent
    1.5.2.RELEASE
  
  com.huawei.hicloud
  dubbo-consume
  0.0.1-SNAPSHOT
  
  
  	
  		org.springframework.boot
  		spring-boot-starter-web
  	
  	
  		org.springframework.boot
  		spring-boot-starter-test
  	
  	
  		com.alibaba
  		dubbo
  		2.5.3
  		
  			
  				org.springframework
  				spring
  			
  		
  	
  	
  		org.apache.zookeeper
  		zookeeper
  		3.4.7
  		pom
  	
  	
  		com.github.sgroschupf
  		zkclient
  		0.1
  	
  	
  		com.huawei.hicloud
  		dubbo-service-api
  		0.0.1-SNAPSHOT
  	
  	
  
  
  
  	
  		
  			org.springframework.boot
  			spring-boot-maven-plugin
  		
  		
  			org.apache.maven.plugins
  			maven-compiler-plugin
  			
  				1.8
  				1.8
  			
  		
  	
  
  

3> api存放接口和pojo

  • - dubbo-service-api
  • |- src/main/java
  •     |- com.huawei.hicloud
  •         |- pojo
  •             Product.java
  •         |- service
  •             ProductService.java
com.huaewi.hicloud.pojo.Product.java
package com.huaewi.hicloud.pojo;

import java.io.Serializable;

public class Product implements Serializable {

	private static final long serialVersionUID = -7442539232954496779L;
	
	private String id;
	private String name;
	private Double price;
	
	private String category;
	private String brand;
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Double getPrice() {
		return price;
	}
	public void setPrice(Double price) {
		this.price = price;
	}
	public String getCategory() {
		return category;
	}
	public void setCategory(String category) {
		this.category = category;
	}
	public String getBrand() {
		return brand;
	}
	public void setBrand(String brand) {
		this.brand = brand;
	}
	
	@Override
	public String toString() {
		return "Product [id=" + id + ", name=" + name + ", price=" + price + ", category=" + category + ", brand="
				+ brand + "]";
	}
	
}
com.huaewi.hicloud.service.ProductService.java

package com.huaewi.hicloud.service;

import com.huaewi.hicloud.pojo.Product;

public interface ProductService {
	
	public Product findById(String id);
	
}
pom文件:

  4.0.0
  
    org.springframework.boot
    spring-boot-starter-parent
    1.5.2.RELEASE
  
  com.huawei.hicloud
  dubbo-service-api
  0.0.1-SNAPSHOT
  
  
  	
  		
  			org.springframework.boot
  			spring-boot-maven-plugin
  		
  		
  			org.apache.maven.plugins
  			maven-compiler-plugin
  			
  				1.8
  				1.8
  			
  		
  	
  

2.本地启动zookeeper

3.启动provider和consumer应用

访问http://localhost:8081/products/10001
浏览器返回
{
	"id": "10001",
	"name": "Honor V10",
	"price": 2699.0,
	"category": "TEL",
	"brand": "HUAWEI"
}

二.基于SpringBoot的注解开发

新建三个maven工程(工程名字是现在项目的名称随便命名的)

1> 服务提供者

  • - boot-dubbo-provider
  • |- src/main/java
  •     |- com.huawei.hicloud
  •         |- service.impl
  •             ProductServiceImpl.java
  •         BootDubboProviderApplication.java
  • |- src/main/resources
  •     application.yml
  •     bootstrap.yml
代码内容:
com.huawei.hicloud.service.impl.ProductServiceImpl
package com.huawei.hicloud.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.huaewi.hicloud.pojo.Product;
import com.huaewi.hicloud.service.ProductService;

@Service(version="1.0.0")
public class ProductServiceImpl implements ProductService {

	@Override
	public Product findById(String id) {
		Product product = new Product();
		product.setId(id);
		product.setBrand("HUAWEI");
		product.setCategory("TEL");
		product.setName("Honor V10");
		product.setPrice(2499d);
		return product;
	}

}

com.huawei.hicloud.BootDubboProviderApplication
package com.huawei.hicloud;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class BootDubboProviderApplication implements CommandLineRunner {
	
	private static final Logger logger = LoggerFactory.getLogger(BootDubboProviderApplication.class);
	
	public static void main(String[] args) {
		logger.info("### Spring boot DubboProviderApplication starter ...");
		SpringApplication.run(BootDubboProviderApplication.class, args);
	}

	@Override
	public void run(String... arg0) throws Exception {
		logger.info("### Spring boot Dubbo provider start ok!");
	}

}

/boot-dubbo-provider/src/main/resources/application.yml

server: 
  port: 8080

spring: 
  dubbo: 
    application: 
      name: boot-dubbo-provider
    registry: 
      address: zookeeper://127.0.0.1:2181
    protocol: 
      name: dubbo
      port: 20880
    scan: 
      com.huawei.hicloud.service.impl
  
/boot-dubbo-provider/src/main/resources/bootstrap.yml
none

pom

  4.0.0
  
    org.springframework.boot
    spring-boot-starter-parent
    1.5.2.RELEASE
  
  com.huawei.hicloud
  boot-dubbo-provider
  0.0.1-SNAPSHOT
  
  
  	
  		org.springframework.boot
  		spring-boot-starter-web
  	
  	
  		io.dubbo.springboot
  		spring-boot-starter-dubbo
  		1.0.0
  	
  	
  		com.huawei.hicloud
  		boot-dubbo-service-api
  		0.0.1-SNAPSHOT
  	
  	
  		org.springframework.boot
  		spring-boot-starter-test
  	
  
  
  	
  		
  			org.springframework.boot
  			spring-boot-maven-plugin
  		
  		
  			org.apache.maven.plugins
  			maven-compiler-plugin
  			3.7.0
  			
  				1.8
  				1.8
  			
  		
  	
  

2> 服务消费者

  • - boot-dubbo-consumer
  • |- src/main/java
  •     |- com.huawei.hicloud
  •         |- controller
  •             ProductController.java
  •         BootDubboConsumerApplication.java
  • |- src/main/resources
  •     application.yml
  •     bootstrap.yml
代码内容:
com.huawei.hicloud.controller.ProductController

package com.huawei.hicloud.controller;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.huaewi.hicloud.pojo.Product;
import com.huaewi.hicloud.service.ProductService;

@RestController
public class ProductController {
	
	@Reference(version="1.0.0")
	private ProductService productService;

	@RequestMapping(value="/products/{id}", method=RequestMethod.GET)
	public Product findById(@PathVariable(value="id") String id) {
		Product product = productService.findById("10001");
		return product;
	}
}

com.huawei.hicloud.BootDubboConsumerApplication
package com.huawei.hicloud;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class BootDubboConsumerApplication {
	
	private static final Logger logger = LoggerFactory.getLogger(BootDubboConsumerApplication.class);
	
	public static void main(String[] args) {
		logger.info("### Spring boot DubboProviderApplication starter ...");
		SpringApplication.run(BootDubboConsumerApplication.class, args);
	}

}

/boot-dubbo-consumer/src/main/resources/application.yml
server: 
  port: 8081
  
spring: 
  dubbo: 
    application: 
      name: boot-dubbo-consumer
    registry: 
      address: zookeeper://127.0.0.1:2181
    protocol: 
      name: dubbo
      port: 20880
    scan: 
      com.huawei.hicloud

/boot-dubbo-consumer/src/main/resources/bootstrap.yml
none

  4.0.0
  
    org.springframework.boot
    spring-boot-starter-parent
    1.5.2.RELEASE
  
  com.huawei.hicloud
  boot-dubbo-consumer
  0.0.1-SNAPSHOT
  
  
  	
  		org.springframework.boot
  		spring-boot-starter-web
  	
  	
  		io.dubbo.springboot
  		spring-boot-starter-dubbo
  		1.0.0
  	
  	
  		com.huawei.hicloud
  		boot-dubbo-service-api
  		0.0.1-SNAPSHOT
  	
  	
  		org.springframework.boot
  		spring-boot-starter-test
  	
  
  
  
  	
  		
  			org.springframework.boot
  			spring-boot-maven-plugin
  		
  		
  			org.apache.maven.plugins
  			maven-compiler-plugin
  			
  				1.8
  				1.8
  			
  		
  	
  

3> api存放接口和pojo

  • - boot-dubbo-service-api
  • |- src/main/java
  •     |- com.huawei.hicloud
  •         |- pojo
  •             Product.java
  •         |- service
  •             ProductService.java
com.huaewi.hicloud.pojo.Product.java
package com.huaewi.hicloud.pojo;

import java.io.Serializable;

public class Product implements Serializable {

	private static final long serialVersionUID = -7442539232954496779L;
	
	private String id;
	private String name;
	private Double price;
	
	private String category;
	private String brand;
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Double getPrice() {
		return price;
	}
	public void setPrice(Double price) {
		this.price = price;
	}
	public String getCategory() {
		return category;
	}
	public void setCategory(String category) {
		this.category = category;
	}
	public String getBrand() {
		return brand;
	}
	public void setBrand(String brand) {
		this.brand = brand;
	}
	
	@Override
	public String toString() {
		return "Product [id=" + id + ", name=" + name + ", price=" + price + ", category=" + category + ", brand="
				+ brand + "]";
	}
	
}
com.huaewi.hicloud.service.ProductService.java

package com.huaewi.hicloud.service;

import com.huaewi.hicloud.pojo.Product;

public interface ProductService {
	
	public Product findById(String id);
	
}
pom文件:

  4.0.0
  
    org.springframework.boot
    spring-boot-starter-parent
    1.5.2.RELEASE
  
  com.huawei.hicloud
  dubbo-service-api
  0.0.1-SNAPSHOT
  
  
  	
  		
  			org.springframework.boot
  			spring-boot-maven-plugin
  		
  		
  			org.apache.maven.plugins
  			maven-compiler-plugin
  			
  				1.8
  				1.8
  			
  		
  	
  

2.本地启动zookeeper

3.启动provider和consumer应用

访问http://localhost:8081/products/10001
浏览器返回
{
	"id": "10001",
	"name": "Honor V10",
	"price": 2699.0,
	"category": "TEL",
	"brand": "HUAWEI"
}

注意: 
第一个xml方式中引入dubbo一定要排除spring依赖,否则启动失败

你可能感兴趣的:(基于springboot搭建dubbo框架(注解和xml配置两种方式))