SpringBoot中集成Dubbo

简单实现springboot中使用dubbo

行动前准备:

1.搭建zookeeper集群:https://blog.csdn.net/ws_kfxd/article/details/94554018
2.安装dubbo控制台和监控中心:https://blog.csdn.net/ws_kfxd/article/details/87902693

搭建项目:

总共三部分:中间暴露接口项目(API)、服务提供者项目(provider)、消费者项目(customer)
三步走:
一、先搭建API项目:
(1)创建maven项目;
(2)pom文件引入以下依赖:

<parent>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-parentartifactId>
    <version>2.1.0.RELEASEversion>
parent>
  
<dependencies>
	
  <dependency>
       <groupId>com.alibaba.spring.bootgroupId>
       <artifactId>dubbo-spring-boot-starterartifactId>
       <version>2.0.0version>
  dependency>
  
  <dependency>
       <groupId>org.apache.zookeepergroupId>
       <artifactId>zookeeperartifactId>
       <version>3.4.6version>
       <exclusions>
           <exclusion>
               <groupId>org.slf4jgroupId>
               <artifactId>slf4j-log4j12artifactId>
           exclusion>
           <exclusion>
               <groupId>log4jgroupId>
               <artifactId>log4jartifactId>
           exclusion>
       exclusions>
  dependency>
  
  <dependency>
       <groupId>com.github.sgroschupfgroupId>
       <artifactId>zkclientartifactId>
       <version>0.1version>
  dependency> 
dependencies>

(3)新建一个普通接口:

package com.ws.test;
public interface TestAPI {
	String sayHello(String name);
}

二、接着搭建provider项目:
(1)创建简单的springboot项目;
(2)pom文件引入第一步中的API项目的依赖:
解释:provider项目也得引入dubbo、zookeeper等依赖,但我自己的api项目中已经有这些依赖,provider又有api的依赖,所以我不用再写了;各位根据自己实际情况定!

<parent>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-parentartifactId>
    <version>2.1.0.RELEASEversion>
parent>

<dependencies>
	
	<dependency>
		<groupId>org.springframework.bootgroupId>
		<artifactId>spring-boot-starter-webartifactId>
	dependency>
    
    <dependency>
       <groupId>com.ws.testgroupId>
       <artifactId>test-apiartifactId>
       <version>0.0.1-SNAPSHOTversion>
    dependency>
dependencies>

(3)创建application.properties文件,配置dubbo信息

#WEB端口
server.port=8081

# dubbo
#作为消费者时的应用名
spring.dubbo.application.id=test01-id-provider
#作为提供者时的应用名
spring.dubbo.application.name=test01-name-provider
#zookeeper集群地址
spring.dubbo.registry.address=zookeeper://192.168.176.130:2182?backup=192.168.176.130:2181,192.168.176.130:2183
spring.dubbo.server=true
spring.dubbo.protocol.name=dubbo
#提供服务的端口
spring.dubbo.protocol.port=20880
#连接监控中心  法2:spring.dubbo.monitor.address=监控中心ip:监控中心通信端口
spring.dubbo.monitor.protocol=registry

(4)项目启动类加入@EnableDubboConfiguration注解,用来加载dubbo配置

package com.ws.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;

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

(5)在provider中新建接口实现类:

package com.ws.test.dubbo;
import org.springframework.stereotype.Component;
import com.alibaba.dubbo.config.annotation.Service;
import com.ws.test.TestAPI;

@Component
@Service    //dubbo包里的
public class TestAPIImpl implements TestAPI{
	@Override
	public String sayHello(String name) {
		// TODO Auto-generated method stub
		System.out.println("--------------------------------Hello," + name + "------------------------------");
		return "Hello," + name;
	}
}

三、最后搭建customer项目:
(1)创建简单的springboot项目;
(2)pom文件引入第一步中的API项目的依赖;
(3)创建application.properties文件,配置dubbo信息:

server.port=8082
spring.dubbo.application.name=test01-id-consumer
spring.dubbo.application.id=test01-name-consumer
spring.dubbo.protocol.port=20800
spring.dubbo.protocol.name=dubbo
spring.dubbo.registry.address=zookeeper://192.168.176.130:2182?backup=192.168.176.130:2181,192.168.176.130:2183
spring.dubbo.monitor.protocol=registry

(4)直接创建类调用api项目接口:

@Reference
private TestAPI testAPI;

String dubbore = testAPI.sayHello(username);
log.info("dubbo--------------------{}",dubbore);

测试:

先启动zookeeper三个节点,在启动provider项目,最后启动customer项目:

provider:
在这里插入图片描述
customer:
在这里插入图片描述
控制台:
SpringBoot中集成Dubbo_第1张图片
SpringBoot中集成Dubbo_第2张图片
监控中心:(发了5次请求)
SpringBoot中集成Dubbo_第3张图片
在这里插入图片描述
SpringBoot中集成Dubbo_第4张图片

你可能感兴趣的:(springboot,Dubbo,Zookeeper)