目錄
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】