eclipse 使用maven 构建springboot+dubbo+zookeeper 构建soa服务(面向服务架构)分析

你猜 阅读:241 2021-03-31 21:20:51 评论:0

本文参考:http://www.bysocket.com/?p=1681

一、为啥整合 Dubbo 实现 SOA

Dubbo 不单单只是高性能的 RPC 调用框架,更是 SOA 服务治理的一种方案。
核心
1. 远程通信,向本地调用一样调用远程方法。
2. 集群容错
3. 服务自动发现和注册,可平滑添加或者删除服务提供者。
我们常常使用 Springboot 暴露 HTTP 服务,并走 JSON 模式。但慢慢量大了,一种 SOA 的治理方案。这样可以暴露出 Dubbo 服务接口,提供给 Dubbo 消费者进行 RPC 调用。下面我们详解下如何集成 Dubbo。

二、windows10 搭建zookeeper 和启动zookeeper 服务

运行环境:JDK 7 或 8,Maven 3.0+
技术栈:SpringBoot 1.5+、Dubbo 2.5+、ZooKeeper 3.3+

1.ZooKeeper 服务注册中心
ZooKeeper 是一个分布式的,开放源码的分布式应用程序协调服务。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。

Step1:下载最新版本且稳定的zookeeper,在http://apache.fayea.com/zookeeper/里就可以下载


step2:解压下载的文件,zookeeper不需要安装的,解压即可。 然后把解压后文件放到你要的位置,如:D:\zookeeper\zookeeper-3.3.6


Step3:进入D:\zookeeper\zookeeper-3.3.6\conf目录,将里面的zoo_sample.cfg文件,做一个备份,然后改名为zoo.cfg,因为zookeeper启动后,只认识zoo.cfg中的所有设置和配置

Step4:打开zoo.cfg,输入以下内容(如何打开,大家挑自己喜欢的编辑软件,我用的是Notepad++)。
# The number of milliseconds of each tick 
tickTime=2000 
# The number of ticks that the initial  
# synchronization phase can take 
initLimit=10 
# The number of ticks that can pass between  
# sending a request and getting an acknowledgement 
syncLimit=5 
# the directory where the snapshot is stored. 
dataDir=D:\\zookeeper\\data 
dataLogDir=D:\\zookeeper\\log 
# the port at which the clients will connect 
clientPort=2181
之所以上面用“\\”,下面用“/”,只是为了告诉大家,只有这2中斜杠的用法是正确的,其他都是错误的。

Step5:进入D:\zookeeper,文件夹下创建2个子目录,目录名分别为data、logs

Step6: 使用cmd,进入D:\zookeeper\zookeeper-3.3.6\bin,运行zkServer.cmd,启动服务,如下图(port:2181是zookeeper的专用监听端口)。


三、springboot-dubbo-server 和 springboot-dubbo-client 工程配置详解

1.详解 springboot-dubbo-server Dubbo 服务提供者工程
springboot-dubbo-server 工程目录结构

2、springboot-dubbo-server pom.xml配置文件
<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>com.zzg</groupId> 
  <artifactId>springboot-dubbo-server</artifactId> 
  <version>0.0.1-SNAPSHOT</version> 
   
<!-- 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> 
  
        <!-- 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> 
  
        <!-- Spring Boot Test 依赖 --> 
        <dependency> 
            <groupId>org.springframework.boot</groupId> 
            <artifactId>spring-boot-starter-test</artifactId> 
            <scope>test</scope> 
        </dependency> 
  
        <!-- Junit --> 
        <dependency> 
            <groupId>junit</groupId> 
            <artifactId>junit</artifactId> 
            <version>4.12</version> 
        </dependency> 
    </dependencies> 
</project>

2、springbooot-server-dubbo 资源配置文件 application.properties

## Dubbo \u670D\u52A1\u63D0\u4F9B\u8005\u914D\u7F6E 
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=com.spring.springboot.dubbo   //dubbo 资源扫描文件

3、springboot-server-dubbo  功能源代码
package com.spring.springboot.domain; 
 
import java.io.Serializable; 
 
public class City implements Serializable { 
 
	 private static final long serialVersionUID = -1L; 
 
	    /** 
	     * 城市编号 
	     */ 
	    private Long id; 
 
	    /** 
	     * 省份编号 
	     */ 
	    private Long provinceId; 
 
	    /** 
	     * 城市名称 
	     */ 
	    private String cityName; 
 
	    /** 
	     * 描述 
	     */ 
	    private String description; 
 
	    public City() { 
	    } 
 
	    public City(Long id, Long provinceId, String cityName, String description) { 
	        this.id = id; 
	        this.provinceId = provinceId; 
	        this.cityName = cityName; 
	        this.description = 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; 
	    } 
 
} 

package com.spring.springboot.dubbo; 
 
import com.spring.springboot.domain.City; 
 
/** 
 * 城市业务 Dubbo 服务层 
 * 
 * Created by zzg on 01/04/2017. 
 */ 
public interface CityDubboService { 
	/** 
     * 根据城市名称,查询城市信息 
     * @param cityName 
     */ 
    City findCityByName(String cityName); 
} 

package com.spring.springboot.dubbo.impl; 
 
import com.alibaba.dubbo.config.annotation.Service; 
import com.spring.springboot.domain.City; 
import com.spring.springboot.dubbo.CityDubboService; 
 
/** 
 * 城市业务 Dubbo 服务层实现层 
 * 
 * Created by zzg on 01/04/2017. 
 */ 
// 注册为 Dubbo 服务 
@Service(version = "1.0.0") 
public class CityDubboServiceImpl implements CityDubboService { 
 
	public City findCityByName(String cityName) { 
		// TODO Auto-generated method stub 
		return new City(1L,2L,"南县","是我的故乡"); 
	} 
 
} 

package com.spring.springboot; 
 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
 
/** 
 * Spring Boot 应用启动类 
 * 
 * Created by zzg on 01/04/2017. 
 */ 
// Spring Boot 应用的标识 
@SpringBootApplication 
public class ServerApplication { 
	public static void main(String[] args) { 
		// 程序启动入口 
        // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件 
        SpringApplication.run(ServerApplication.class,args); 
    } 
} 

2.详解 springboot-dubbo-client Dubbo 服务消费者工程
springboot-dubbo-client 工程目录结构


2、springboot-dubbo-client pom.xml配置文件

<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>com.zzg</groupId> 
  <artifactId>springboot-dubbo-client</artifactId> 
  <version>0.0.1-SNAPSHOT</version> 
   
    <!-- 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> 
 
        <!-- 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> 
 
        <!-- Spring Boot Test 依赖 --> 
        <dependency> 
            <groupId>org.springframework.boot</groupId> 
            <artifactId>spring-boot-starter-test</artifactId> 
            <scope>test</scope> 
        </dependency> 
 
        <!-- Junit --> 
        <dependency> 
            <groupId>junit</groupId> 
            <artifactId>junit</artifactId> 
            <version>4.12</version> 
        </dependency> 
 
    </dependencies> 
</project>



3、springboot-dubbo-client 资源配置文件application.properties

## \u907F\u514D\u548C server \u5DE5\u7A0B\u7AEF\u53E3\u51B2\u7A81 
server.port=8081   //防止服务提供者与服务消费者端口冲突 
 
## Dubbo \u670D\u52A1\u6D88\u8D39\u8005\u914D\u7F6E 
spring.dubbo.application.name=consumer 
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181 
spring.dubbo.scan=com.spring.springboot.dubbo  //dubbo 服务扫描



3、springboot-server-client  功能源代码

package com.spring.springboot.domain; 
 
import java.io.Serializable; 
 
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 + '\'' + 
                '}'; 
    } 
 
} 


package com.spring.springboot.dubbo; 
 
import org.springframework.stereotype.Component; 
 
import com.alibaba.dubbo.config.annotation.Reference; 
import com.spring.springboot.domain.City; 
 
/** 
 * 城市 Dubbo 服务消费者 
 * 
 * Created by zzg on 01/04/2017. 
 */ 
@Component 
public class CityDubboConsumerService { 
 
	@Reference(version = "1.0.0") 
	CityDubboService cityDubboService;	 
 
	public CityDubboService getCityDubboService() { 
		return cityDubboService; 
	} 
	 
	public void setCityDubboService(CityDubboService cityDubboService) { 
		this.cityDubboService = cityDubboService; 
	} 
 
 
 
	public void printCity() { 
		String cityName = "南县"; 
		if(cityDubboService == null){ 
			System.out.println("cityDubboService 服务对象 为空"); 
		}else{ 
			System.out.println("cityDubboService 服务对象 不 为空"); 
		} 
		City city = cityDubboService.findCityByName(cityName); 
		System.out.println(city.toString()); 
	} 
 
} 


package com.spring.springboot.dubbo; 
 
import com.spring.springboot.domain.City; 
 
/** 
 * 城市业务 Dubbo 服务层 
 * 
 * Created by zzg on 01/04/2017. 
 */ 
public interface CityDubboService { 
 
    /** 
     * 根据城市名称,查询城市信息 
     * @param cityName 
     */ 
    City findCityByName(String cityName); 
} 


package com.spring.springboot; 
 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.context.ConfigurableApplicationContext; 
 
import com.spring.springboot.dubbo.CityDubboConsumerService; 
 
/** 
 * Spring Boot 应用启动类 
 * 
 * Created by zzg on 01/04/2017. 
 */ 
// Spring Boot 应用的标识 
@SpringBootApplication 
public class ClientApplication { 
 
	public static void main(String[] args) { 
		// TODO Auto-generated method stub 
		// 程序启动入口 
        // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件 
        ConfigurableApplicationContext run = SpringApplication.run(ClientApplication.class, args); 
        CityDubboConsumerService cityService = run.getBean(CityDubboConsumerService.class); 
        cityService.printCity(); 
	} 
 
} 


运行截图:








声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
KIKK导航

KIKK导航

排行榜
关注我们

一个IT知识分享的公众号