目录
1.使用版本
jdk1.8、springboot2.0.0、Dubbo2.7.3、zookeeper3.6.0、curator4.2.0。
Dubbo2.7.3是Apache上的项目,而不是alibaba上的项目(使用注解的时候请使用apache的,alibaba的会显示过时)。
2.项目结构
组件名称 | 工程 | 备注 |
注册中心 | zookeeper3.6.0安装 | |
服务api | dubbo-interf | |
服务提供方 | dubbo-provider | |
服务消费方 | dubbo-consumer |
3.创建父工程
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>2.0.0.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.zyf</groupId>
<artifactId>dubbo-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>dubbo-parent</name>
<description>dubbo微服务父工程</description>
<modules><!-- dubbo微服务子工程 -->
<module>dubbo-provider</module>
<module>dubbo-consumer</module>
<module>dubbo-interf</module>
</modules>
</project>
4.创建服务api工程
pom:
普通的springboot工程,定义一个接口,无配置。可单独打包,接口能被提供者和消费者引用。
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent><!-- 父工程坐标 -->
<groupId>com.zyf</groupId>
<artifactId>dubbo-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>dubbo-interf</artifactId>
<name>dubbo-interf</name>
<description>dubbo微服务接口工程</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
接口:
package com.zyf.dubbointerf.service;
/**
* 定义dubbo api,单独打包,被提供者和消费者共通引用
*/
public interface ApiService {
public String sayHello(String name);
}
5.创建服务提供者工程
5.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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent><!-- 父工程坐标 -->
<groupId>com.zyf</groupId>
<artifactId>dubbo-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>dubbo-provider</artifactId>
<name>dubbo-provider</name>
<description>Ddubbo微服务提供者</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency><!-- 引入父工程下服务api工程 -->
<groupId>${project.groupId}</groupId>
<artifactId>dubbo-interf</artifactId>
<version>${project.version}</version>
</dependency>
<!-- 集成dubbo所需依赖 start -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.2.0</version>
</dependency>
<!-- 集成dubbo所需依赖 end -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
5.2.application.yml配置注册中心
server:
port: 8001 #服务端口
dubbo:
application:
name: provider #应用名
registry:
address: 127.0.0.1:2181 # zookeeper地址和端口
protocol: zookeeper
check: false
protocol:
name: dubbo
port: 30003
monitor:
protocol: register
consumer:
check: false
timeout: 3000
5.3.dubbo功能开启
@EnableDubbo开启apache dubbo
package com.zyf.dubboprovider;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo // 开启apache dubbo
public class DubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class, args);
}
}
5.4.服务api实现
@Service是apache dubbo的servcie注解
package com.zyf.dubboprovider.service;
import org.apache.dubbo.config.annotation.Service;
import com.zyf.dubbointerf.service.ApiService;
@Service // apache dubbo的servcie注解
public class ApiServiceImpl implements ApiService {
@Override
public String sayHello(String name) {
System.out.println("来自服务提供者的问候:hello " + name);
return "来自服务提供者的问候:hello " + name;
}
}
6.创建服务消费者工程
6.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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent><!-- 父工程坐标 -->
<groupId>com.zyf</groupId>
<artifactId>dubbo-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>dubbo-consumer</artifactId>
<name>dubbo-consumer</name>
<description>Ddubbo微服务消费者</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency><!-- 引入父工程下服务api工程 -->
<groupId>${project.groupId}</groupId>
<artifactId>dubbo-interf</artifactId>
<version>${project.version}</version>
</dependency>
<!-- 集成dubbo所需依赖 start -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.2.0</version>
</dependency>
<!-- 集成dubbo所需依赖 end -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
6.2.application.yml配置注册中心
server:
port: 9001 #服务端口
dubbo:
application:
name: consumer #应用名
registry:
address: 127.0.0.1:2181 #zookeeper地址和端口
protocol: zookeeper
check: false
monitor:
protocol: register
consumer:
check: false
timeout: 3000
6.3.dubbo功能开启
package com.zyf.dubboconsumer;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo // 开启apache dubbo
public class DubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DubboConsumerApplication.class, args);
}
}
6.4.调用服务api
@Service是spring的servcie注解。
@Reference是apache dubbo的注解,注入服务接口实例。
package com.zyf.dubboconsumer.service;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;
import com.zyf.dubbointerf.service.ApiService;
@Service // spring的servcie注解
public class ConsumerService {
@Reference // apache dubbo的@Reference注解,注入服务接口实例
private ApiService apiService;
public String sayHello(String name) {
return apiService.sayHello(name);
}
}
6.5.测试
package com.zyf.dubboconsumer.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zyf.dubboconsumer.service.ConsumerService;
@RestController
@RequestMapping("/ConsumerController")
public class ConsumerController {
@Autowired
ConsumerService consumerService;
@RequestMapping("/sayHello")
public String sayHello(String name) {
return consumerService.sayHello(name);
}
}
访问:http://localhost:9001/ConsumerController/sayHello?name=zhangsan
成功!
--------------------------------------------------------
另外可以通过dubb admin控制台查看是否注册成功:
可以看到提供者和消费者的应用名。
github:https://github.com/zhangyangfei/dubbo-learn种的dubbo-parent工程。
【END】