Dubbo之整合SpringBoot

一、搭建项目总体结构

     新建一个父级Maven项目,Dubbo,在此父级Maven项目下,再新建三个Maven子模块项目,Dubbo-api,Dubbo-provider,Dubbo-consumer。

1、项目总体结构,如下图所示

2、Dubbo的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.yj</groupId>
	<artifactId>Dubbo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>

	<name>Dubbo</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.4.RELEASE</version>
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<dependency>
			<groupId>com.alibaba.boot</groupId>
			<artifactId>dubbo-spring-boot-starter</artifactId>
			<version>0.2.0</version>
		</dependency>
	</dependencies>
	<modules>
		<module>Dubbo-Api</module>
		<module>Dubbo-Consumer</module>
		<module>Dubbo-Provider</module>
	</modules>
</project>

二、Dubbo-Api项目

1、项目总体结构,如下图所示

2、Dubbo-Api的pom.xml文件

<?xml version="1.0"?>
<project
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
	xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>
	
	<parent>
		<groupId>com.yj</groupId>
		<artifactId>Dubbo</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	
	<artifactId>Dubbo-Api</artifactId>
	<name>Dubbo-Api</name>
	<url>http://maven.apache.org</url>
	
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
</project>

3、Dubbo-Api的UserService接口

在dubbo中,group,version,interface是服务的匹配条件,也只有这三个参数才能确定是同一个服务,其他的配置均为调优和治理参数

package com.yj.api.user;

public interface UserService {
    String sayHello(String name);
}

三、Dubbo-Provider项目

项目总体结构,如下图所示

UserServiceImpl类

package com.yj.provider.user;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.alibaba.dubbo.config.annotation.Service;
import com.yj.api.user.UserService;

@Service
@Component
public class UserServiceImpl implements UserService {
	
	@Value("${server.port}")
	private String port;
	
    @Override
    public String sayHello(String content) {
        return "providerPort:"+port+",content:" + content;
    }
}

application.properties配置文件

server.port=8003
#dubbo配置
dubbo.application.name=dubbo-provider
dubbo.registry.address=zookeeper://192.168.190.129:2181
dubbo.protocol.name=dubbo
#默认端口20880,设置为-1则会分配一个没有被占用的端口
dubbo.protocol.port=-1
dubbo.monitor.protocol=registry

ProviderApplication类

package com.yj.provider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

@SpringBootApplication
@EnableDubbo
public class ProviderApplication {
	public static void main(String[] args) {
		SpringApplication.run(ProviderApplication.class, args);
	}
}

四、Dubbo-Consumer项目

项目总体结构,如下图所示

UserController类

package com.yj.consumer.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yj.api.user.UserService;

@RestController
public class UserController {
	
	@Reference
	private UserService  userService;
	
	@RequestMapping("/sayHello")
	public String sayHello(@RequestParam String content){
		return userService.sayHello(content);
	}
}

application.properties配置文件

server.port=8001
#dubbo配置
dubbo.application.name=dubbo-consumer
dubbo.registry.address=zookeeper://192.168.190.129:2181
dubbo.protocol.name=dubbo
dubbo.monitor.protocol=registry

5、ConsumerApplication类

package com.yj.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

@SpringBootApplication
@EnableDubbo
public class ConsumerApplication {
	public static void main(String[] args) {
		SpringApplication.run(ConsumerApplication.class, args);
	}
}

五、运行项目

1、启动zookeeper(3.5版本,Zookeeper AdminServer,默认使用8080端口,为避免与后面要启动的dubbo-monitor的jetty的8080端口冲突,此处将Zookeeper AdminServer的端口改为8888)

# 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.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
dataDir=/opt/zookeeper/zookeeper-3.5.4-beta/data
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the 
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
#修改AdminServer的端口(默认8080)
admin.serverPort=8888

2、启动dubbo控制台(dubbo2.6之后的版本maven打包方式从war改为了jar,就不需要tomcat来部署war包)

java -jar dubbo-admin-0.0.1-SNAPSHOT &

3、启动dubbo监控台

sh start.sh &

4、分别启动8002,8003端口的Dubbo-Provider集群

server.port=8002
dubbo.protocol.port=-1
server.port=8003
dubbo.protocol.port=-1

5、启动8001端口的dubbo-consumer项目

6、访问Dubbo-admin控制台,使用roor/root或者guest/guest登录

http://192.168.190.129:7001/dubbo-admin-jdk1.8

查看Dubbo-Provider,Dubbo-Consumer,如下图所示

查看Dubbo-Provider,Dubbo-Consumer,如下图所示

7、访问Dubbo监控台(基于jetty)

http://192.168.190.129:8080/

8、访问

通过浏览器访问

http://127.0.0.1:8001/sayHello?content=hello

网页交替显示

providerPort:8002,content:hello
providerPort:8003,content:hello

Dubbo基于Zookeeper,实现了服务治理与负载均衡(Zookeeper只是Dubbo开源版本的注册中心的实现,阿里内部并没有采用Zookeeper作为注册中心)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猎户星座。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值