SpringBoot学习10.3-springboot2.0.0集成Dubbo2.7.3

目录

1.使用版本

2.项目结构

3.创建父工程

4.创建服务api工程

5.创建服务提供者工程

5.1.pom依赖

5.2.application.yml配置注册中心

5.3.dubbo功能开启

5.4.服务api实现

6.创建服务消费者工程

6.1.pom依赖

6.2.application.yml配置注册中心

6.3.dubbo功能开启

6.4.调用服务api

6.5.测试


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安装 
服务apidubbo-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】

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值