Spring Boot + Dubbo + Zookeeper案例

前提是Zookeeper安装好了,然后启动dubbo-admin
首先使用SpringBoot创建是三个项目(DubboProvider,DubboConsumer,DubboCommon)
Spring Boot + Dubbo + Zookeeper案例
首先看看DubboCommon的结构图
Spring Boot + Dubbo + Zookeeper案例
然后是DubboConsumer的结构图
Spring Boot + Dubbo + Zookeeper案例
然后是DubboProvider的结构图
Spring Boot + Dubbo + Zookeeper案例
DubboCommon模块
创建一个DemoService 接口就可以了

public interface DemoService {
	public String getDemoString();
}

pom文件

<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>
		<scope>test</scope>
	</dependency>
	<dependency>
		<groupId>com.alibaba.spring.boot</groupId>
		<artifactId>dubbo-spring-boot-starter</artifactId>
		<version>2.0.0</version>
	</dependency>
	<!--zookeeper依赖 -->
	<dependency>
		<groupId>org.apache.zookeeper</groupId>
		<artifactId>zookeeper</artifactId>
		<version>3.4.8</version>
	</dependency>
	<dependency>
		<groupId>com.101tec</groupId>
		<artifactId>zkclient</artifactId>
		<version>0.10</version>
	</dependency>
</dependencies>

DubboConsumer模块
创建一个DemoController类

package com.example.controller;

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

import com.example.service.DemoService;

@RestController
@RequestMapping("demo")
public class DemoController {
	public DemoController() {
		System.err.println("进入了DemoController方法...");
	}
    @Autowired
    private DemoService demoService;
     
    @RequestMapping("index")
    public String index(){
    	System.err.println("调用了index方法...");
        return demoService.getDemoString();
    }
}

编写启动类DubboConsumerApplication

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubboConfig;

@SpringBootApplication
@EnableDubboConfig
@ComponentScan("com.example")
@ImportResource(locations= {"classpath:spring-dubbo.xml"}) 
public class DubboConsumerApplication {

	public static void main(String[] args) {
		System.err.println("启动了DubboConsumerApplication.....");
		SpringApplication.run(DubboConsumerApplication.class, args);
	}
}

创建spring-dubbo.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-consumer"/>
    <!-- 使用multicast广播注册中心暴露发现服务地址 -->
    <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />
    <dubbo:protocol id="dubbo" name="dubbo" port="12345" />
    <!-- 生成远程服务代理,可以和本地bean一样调用 -->
    <dubbo:reference id="demoService" interface="com.example.service.DemoService" />
</beans>

在application.properties文件配置端口号

server.port=9093

pom文件

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
	<groupId>com.example</groupId>
	<artifactId> DubboCommon</artifactId>
	<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
	<groupId>com.alibaba.spring.boot</groupId>
	<artifactId>dubbo-spring-boot-starter</artifactId>
	<version>2.0.0</version>
</dependency>
<!--zookeeper依赖 -->
<dependency>
	<groupId>org.apache.zookeeper</groupId>
	<artifactId>zookeeper</artifactId>
	<version>3.4.8</version>
</dependency>
<dependency>
	<groupId>com.101tec</groupId>
	<artifactId>zkclient</artifactId>
	<version>0.10</version>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-test</artifactId>
	<scope>test</scope>
</dependency>

DubboProvider模块
创建DemoServiceImpl类实现DemoService接口

package com.example.provider.service;

import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Service;
import com.example.service.DemoService;

@Service//这个是要导入这个 com.alibaba.dubbo.config.annotation.Service;不要弄错了
@Component //这个必须要加
public class DemoServiceImpl implements DemoService {
 
    @Override
    public String getDemoString() {
    	System.err.println("调用了(DubboProvider)里面getDemoString方法...");
        return "Hello Dubbo!";
    }
 
}

编写DubboProviderApplication 启动类

package com.example.provider.service;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;

@SpringBootApplication
@ComponentScan("com.example")
@EnableDubboConfiguration
public class DubboProviderApplication {

	public static void main(String[] args) {
		System.err.println("启动了DubboProviderApplication方法...");
		SpringApplication.run(DubboProviderApplication.class, args);
	}
}

编写application.properties文件

server.port=9092
spring.dubbo.scan.basePackages  = com.example.provider.service
spring.dubbo.application.id = dubbo-dustin-demo
spring.dubbo.application.name = dubbo-dustin-demo
spring.dubbo.protocol.id = dubbo
spring.dubbo.protocol.name = dubbo
spring.dubbo.protocol.port = 12345
spring.dubbo.registry.id = my-registry
spring.dubbo.registry.address = zookeeper://127.0.0.1:2181

在写pom文件

<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
	<dependency>
		<groupId>com.example</groupId>
		<artifactId> DubboCommon</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</dependency>
	<dependency>
		<groupId>com.alibaba.spring.boot</groupId>
		<artifactId>dubbo-spring-boot-starter</artifactId>
		<version>2.0.0</version>
	</dependency>
	<!--zookeeper依赖 -->
	<dependency>
		<groupId>org.apache.zookeeper</groupId>
		<artifactId>zookeeper</artifactId>
		<version>3.4.8</version>
	</dependency>
	<dependency>
		<groupId>com.101tec</groupId>
		<artifactId>zkclient</artifactId>
		<version>0.10</version>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>
</dependencies>

写完就OK了
先启动 DubboProviderApplication
然后启动 DubboConsumerApplication
访问 http://localhost:8080/dubbo-admin/
用户名,密码都是root
Spring Boot + Dubbo + Zookeeper案例
Spring Boot + Dubbo + Zookeeper案例
Spring Boot + Dubbo + Zookeeper案例
在去浏览器输入 http://localhost:9093/demo/index
Spring Boot + Dubbo + Zookeeper案例