第一,整合架构说明
1,本次整合内容为springboot+dubbo,包括一个服务端server和一个客户端client
2,示例代码下载地址如下
https://github.com/wanwang2/springboot-learning-example/tree/master/springboot-dubbo-server
https://github.com/wanwang2/springboot-learning-example/tree/master/springboot-dubbo-client
3,本文假设读者已经本地安装了zookeeper,并且配置地址为
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
第二,编写client,创建maven项目命名为 springboot-dubbo-client
1,pom
此处需要的两个关键依赖为
spring-boot-starter-dubbo和spring-boot-starter-web
<?xml version="1.0" encoding="UTF-8"?>
<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>
<groupId>springboot</groupId>
<artifactId>springboot-dubbo-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-dubbo 客户端:: 整合 Dubbo/ZooKeeper 详解 SOA 案例</name>
<!-- Spring Boot 启动父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
<properties>
<dubbo-spring-boot>1.0.0</dubbo-spring-boot>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<!-- Spring Boot Dubbo 依赖 -->
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>${dubbo-spring-boot}</version>
</dependency>
<!-- Spring Boot Web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
2,application.properties
## 避免和 server 工程端口冲突
server.port=8081
## Dubbo 服务消费者配置
spring.dubbo.application.name=consumer
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.scan=org.spring.springboot.dubbo
3,添加service接口
package org.spring.springboot.dubbo;
import org.spring.springboot.domain.City;
/**
* 城市业务 Dubbo 服务层
*
* Created by bysocket on 28/02/2017.
*/
public interface CityDubboService {
/**
* 根据城市名称,查询城市信息
* @param cityName
*/
City findCityByName(String cityName);
}
4,添加service实现类
package org.spring.springboot.dubbo;
import com.alibaba.dubbo.config.annotation.Reference;
import org.spring.springboot.domain.City;
import org.springframework.stereotype.Component;
/**
* 城市 Dubbo 服务消费者
*
* Created by bysocket on 28/02/2017.
*/
@Component
public class CityDubboConsumerService {
@Reference(version = "1.0.0")
CityDubboService cityDubboService;
public void printCity() {
String cityName="温岭";
City city = cityDubboService.findCityByName(cityName);
System.out.println(city.toString());
}
}
5,生成实体类
package org.spring.springboot.domain;
import java.io.Serializable;
/**
* 城市实体类
*
* Created by bysocket on 07/02/2017.
*/
public class City implements Serializable {
private static final long serialVersionUID = -1L;
/**
* 城市编号
*/
private Long id;
/**
* 省份编号
*/
private Long provinceId;
/**
* 城市名称
*/
private String cityName;
/**
* 描述
*/
private String description;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getProvinceId() {
return provinceId;
}
public void setProvinceId(Long provinceId) {
this.provinceId = provinceId;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "City{" +
"id=" + id +
", provinceId=" + provinceId +
", cityName='" + cityName + '\'' +
", description='" + description + '\'' +
'}';
}
}
第三,编写server,创建maven项目命名为 springboot-dubbo-server
1,pom
<?xml version="1.0" encoding="UTF-8"?>
<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>
<groupId>springboot</groupId>
<artifactId>springboot-dubbo-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-dubbo 服务端:: 整合 Dubbo/ZooKeeper 详解 SOA 案例</name>
<!-- Spring Boot 启动父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
<properties>
<dubbo-spring-boot>1.0.0</dubbo-spring-boot>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<!-- Spring Boot Dubbo 依赖 -->
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>${dubbo-spring-boot}</version>
</dependency>
<!-- Spring Boot Web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
2,application.properties
## Dubbo 服务提供者配置
spring.dubbo.application.name=provider
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
spring.dubbo.scan=org.spring.springboot.dubbo
3,创建消费者
package org.spring.springboot.dubbo.impl;
import com.alibaba.dubbo.config.annotation.Service;
import org.spring.springboot.domain.City;
import org.spring.springboot.dubbo.CityDubboService;
import org.springframework.beans.factory.annotation.Autowired;
/**
* 城市业务 Dubbo 服务层实现层
*
* Created by bysocket on 28/02/2017.
*/
// 注册为 Dubbo 服务
@Service(version = "1.0.0")
public class CityDubboServiceImpl implements CityDubboService {
public City findCityByName(String cityName) {
return new City(1L,2L,"温岭","是我的故乡");
}
}
第四,启动服务
1,启动服务端
package org.spring.springboot.dubbo;
import org.spring.springboot.domain.City;
/**
* 城市业务 Dubbo 服务层
*
* Created by bysocket on 28/02/2017.
*/
public interface CityDubboService {
/**
* 根据城市名称,查询城市信息
* @param cityName
*/
City findCityByName(String cityName);
}
2,启动服务端
package org.spring.springboot;
import org.spring.springboot.dubbo.CityDubboConsumerService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
/**
* Spring Boot 应用启动类
*
* Created by bysocket on 16/4/26.
*/
// Spring Boot 应用的标识
@SpringBootApplication
public class ClientApplication {
public static void main(String[] args) {
// 程序启动入口
// 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
ConfigurableApplicationContext run = SpringApplication.run(ClientApplication.class, args);
CityDubboConsumerService cityService = run.getBean(CityDubboConsumerService.class);
cityService.printCity();
}
}
第五,点评
1,本次实验在涉及到service接口上时可以将其单独提取出来作为公共部分。
2,在实际使用中通过将远程service注入到本地service中使用。