基于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
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans.xsd  
    http://code.alibabatech.com/schema/dubbo  
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

	<!-- 提供方应用信息,用于计算依赖关系 -->
	<dubbo:application name="${dubbo.application.name}" />

	<!-- 注册中心暴露服务地址 -->
	<!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->

	<!-- 
		<dubbo:registry protocol="zookeeper" address="10.170.219.98:2181,10.173.55.173:2181" />
	-->
	<dubbo:registry protocol="${dubbo.registry.protocol}" address="${dubbo.registry.address}" />

	<!-- 暴露服务 -->
	<dubbo:protocol name="${dubbo.protocol.name}" port="${dubbo.protocol.port}" />

	<dubbo:service interface="com.chengli.springboot.example.service.ExampleService"
		ref="exampleServiceImpl" retries="0" timeout="6000" />
</beans>
/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文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
  </parent>
  <groupId>com.huawei.hicloud</groupId>
  <artifactId>dubbo-provider</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <dependencies>
  	<dependency>
  		<groupId>org.springframework.boot</groupId>
  		<artifactId>spring-boot-starter-web</artifactId>
  	</dependency>
  	<dependency>
  		<groupId>org.springframework.boot</groupId>
  		<artifactId>spring-boot-starter-test</artifactId>
  	</dependency>
  	<dependency>
  		<groupId>com.alibaba</groupId>
  		<artifactId>dubbo</artifactId>
  		<version>2.5.3</version>
  		<exclusions>
  			<exclusion>
  				<groupId>org.springframework</groupId>
  				<artifactId>spring</artifactId>
  			</exclusion>
  		</exclusions>
  	</dependency>
  	<dependency>
  		<groupId>org.apache.zookeeper</groupId>
  		<artifactId>zookeeper</artifactId>
  		<version>3.4.7</version>
  		<type>pom</type>
  	</dependency>
  	<dependency>
  		<groupId>com.github.sgroschupf</groupId>
  		<artifactId>zkclient</artifactId>
  		<version>0.1</version>
  	</dependency>
  	<dependency>
  		<groupId>com.huawei.hicloud</groupId>
  		<artifactId>dubbo-service-api</artifactId>
  		<version>0.0.1-SNAPSHOT</version>
  	</dependency>
  </dependencies>
  
  <build>
  	<plugins>
  		<plugin>
  			<groupId>org.springframework.boot</groupId>
  			<artifactId>spring-boot-maven-plugin</artifactId>
  		</plugin>
  		<plugin>
  			<groupId>org.apache.maven.plugins</groupId>
  			<artifactId>maven-compiler-plugin</artifactId>
  			<configuration>
  				<source>1.8</source>
  				<target>1.8</target>
  			</configuration>
  		</plugin>
  	</plugins>
  </build>
</project>

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
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans.xsd  
    http://code.alibabatech.com/schema/dubbo  
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

	<!-- 提供方应用信息,用于计算依赖关系 -->
	<dubbo:application name="${dubbo.application.name}" />

	<!-- 注册中心暴露服务地址 -->
	<dubbo:registry protocol="${dubbo.registry.protocol}" address="${dubbo.registry.address}" />

	<dubbo:reference id="productService" interface="com.huaewi.hicloud.service.ProductService" />
</beans>
/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文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
  </parent>
  <groupId>com.huawei.hicloud</groupId>
  <artifactId>dubbo-consume</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <dependencies>
  	<dependency>
  		<groupId>org.springframework.boot</groupId>
  		<artifactId>spring-boot-starter-web</artifactId>
  	</dependency>
  	<dependency>
  		<groupId>org.springframework.boot</groupId>
  		<artifactId>spring-boot-starter-test</artifactId>
  	</dependency>
  	<dependency>
  		<groupId>com.alibaba</groupId>
  		<artifactId>dubbo</artifactId>
  		<version>2.5.3</version>
  		<exclusions>
  			<exclusion>
  				<groupId>org.springframework</groupId>
  				<artifactId>spring</artifactId>
  			</exclusion>
  		</exclusions>
  	</dependency>
  	<dependency>
  		<groupId>org.apache.zookeeper</groupId>
  		<artifactId>zookeeper</artifactId>
  		<version>3.4.7</version>
  		<type>pom</type>
  	</dependency>
  	<dependency>
  		<groupId>com.github.sgroschupf</groupId>
  		<artifactId>zkclient</artifactId>
  		<version>0.1</version>
  	</dependency>
  	<dependency>
  		<groupId>com.huawei.hicloud</groupId>
  		<artifactId>dubbo-service-api</artifactId>
  		<version>0.0.1-SNAPSHOT</version>
  	</dependency>
  	
  </dependencies>
  
  <build>
  	<plugins>
  		<plugin>
  			<groupId>org.springframework.boot</groupId>
  			<artifactId>spring-boot-maven-plugin</artifactId>
  		</plugin>
  		<plugin>
  			<groupId>org.apache.maven.plugins</groupId>
  			<artifactId>maven-compiler-plugin</artifactId>
  			<configuration>
  				<source>1.8</source>
  				<target>1.8</target>
  			</configuration>
  		</plugin>
  	</plugins>
  </build>
  
</project>

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文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
  </parent>
  <groupId>com.huawei.hicloud</groupId>
  <artifactId>dubbo-service-api</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <build>
  	<plugins>
  		<plugin>
  			<groupId>org.springframework.boot</groupId>
  			<artifactId>spring-boot-maven-plugin</artifactId>
  		</plugin>
  		<plugin>
  			<groupId>org.apache.maven.plugins</groupId>
  			<artifactId>maven-compiler-plugin</artifactId>
  			<configuration>
  				<source>1.8</source>
  				<target>1.8</target>
  			</configuration>
  		</plugin>
  	</plugins>
  </build>
</project>

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
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
  </parent>
  <groupId>com.huawei.hicloud</groupId>
  <artifactId>boot-dubbo-provider</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <dependencies>
  	<dependency>
  		<groupId>org.springframework.boot</groupId>
  		<artifactId>spring-boot-starter-web</artifactId>
  	</dependency>
  	<dependency>
  		<groupId>io.dubbo.springboot</groupId>
  		<artifactId>spring-boot-starter-dubbo</artifactId>
  		<version>1.0.0</version>
  	</dependency>
  	<dependency>
  		<groupId>com.huawei.hicloud</groupId>
  		<artifactId>boot-dubbo-service-api</artifactId>
  		<version>0.0.1-SNAPSHOT</version>
  	</dependency>
  	<dependency>
  		<groupId>org.springframework.boot</groupId>
  		<artifactId>spring-boot-starter-test</artifactId>
  	</dependency>
  </dependencies>
  <build>
  	<plugins>
  		<plugin>
  			<groupId>org.springframework.boot</groupId>
  			<artifactId>spring-boot-maven-plugin</artifactId>
  		</plugin>
  		<plugin>
  			<groupId>org.apache.maven.plugins</groupId>
  			<artifactId>maven-compiler-plugin</artifactId>
  			<version>3.7.0</version><!--$NO-MVN-MAN-VER$-->
  			<configuration>
  				<target>1.8</target>
  				<source>1.8</source>
  			</configuration>
  		</plugin>
  	</plugins>
  </build>
</project>

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
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
  </parent>
  <groupId>com.huawei.hicloud</groupId>
  <artifactId>boot-dubbo-consumer</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <dependencies>
  	<dependency>
  		<groupId>org.springframework.boot</groupId>
  		<artifactId>spring-boot-starter-web</artifactId>
  	</dependency>
  	<dependency>
  		<groupId>io.dubbo.springboot</groupId>
  		<artifactId>spring-boot-starter-dubbo</artifactId>
  		<version>1.0.0</version>
  	</dependency>
  	<dependency>
  		<groupId>com.huawei.hicloud</groupId>
  		<artifactId>boot-dubbo-service-api</artifactId>
  		<version>0.0.1-SNAPSHOT</version>
  	</dependency>
  	<dependency>
  		<groupId>org.springframework.boot</groupId>
  		<artifactId>spring-boot-starter-test</artifactId>
  	</dependency>
  </dependencies>
  
  <build>
  	<plugins>
  		<plugin>
  			<groupId>org.springframework.boot</groupId>
  			<artifactId>spring-boot-maven-plugin</artifactId>
  		</plugin>
  		<plugin>
  			<groupId>org.apache.maven.plugins</groupId>
  			<artifactId>maven-compiler-plugin</artifactId>
  			<configuration>
  				<target>1.8</target>
  				<source>1.8</source>
  			</configuration>
  		</plugin>
  	</plugins>
  </build>
</project>

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文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
  </parent>
  <groupId>com.huawei.hicloud</groupId>
  <artifactId>dubbo-service-api</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <build>
  	<plugins>
  		<plugin>
  			<groupId>org.springframework.boot</groupId>
  			<artifactId>spring-boot-maven-plugin</artifactId>
  		</plugin>
  		<plugin>
  			<groupId>org.apache.maven.plugins</groupId>
  			<artifactId>maven-compiler-plugin</artifactId>
  			<configuration>
  				<source>1.8</source>
  				<target>1.8</target>
  			</configuration>
  		</plugin>
  	</plugins>
  </build>
</project>

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依赖,否则启动失败

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值