SpringBoot 整合Dubbo

文章目录

  • 一、工程目录结构
  • 二、创建工程项目
    • 1、创建接口工程(cw-dubbo-api)
      • (1)pom.xml
      • (2)创建接口类(LoginService)
    • 2、创建服务提供者工程(cw-dubbo-provider)
      • (1)pom.xml
      • (2)application.properties
      • (3)主类(CwDubboProviderApplication)
      • (4)LoginServiceImpl
    • 3、创建服务调用者工程(cw-dubbo-consumer)
      • (1)pom.xml
      • (2)application.properties
      • (3)主类(CwDubboConsumerApplication)
      • (4)LoginController
  • 三、测试


注:此项目使用了Zookeeper组件。


一、工程目录结构

在这里插入图片描述


二、创建工程项目

1、创建接口工程(cw-dubbo-api)

此工程就是一个maven工程。

在这里插入图片描述

(1)pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>SpringBootDubboDemo</artifactId><groupId>cw-dubbo-demo</groupId><version>1.0.0</version></parent><modelVersion>4.0.0</modelVersion><artifactId>cw-dubbo-api</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties></project>

(2)创建接口类(LoginService)

package com.cw.dubbo.api;public interface LoginService {String login(String name, String pwd);
}

2、创建服务提供者工程(cw-dubbo-provider)

该工程是一个SpringBoot工程。
在这里插入图片描述


(1)pom.xml

<?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>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.4.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>cw-dubbo-demo</groupId><artifactId>cw-dubbo-provider</artifactId><version>1.0.0</version><name>cw-dubbo-provider</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>cw-dubbo-demo</groupId><artifactId>cw-dubbo-api</artifactId><version>1.0.0</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!--引入springboot整合dubbo的jar包--><dependency><groupId>com.alibaba.boot</groupId><artifactId>dubbo-spring-boot-starter</artifactId><version>0.2.0</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

(2)application.properties

server.port=8078# dubbo项目名
dubbo.application.name=cw-dubbo-providor# 注册中心地址
dubbo.registry.address=127.0.0.1:2181
# 指定注册中心为zookeeper
dubbo.registry.protocol=zookeeper# 指定通信协议
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
# 
dubbo.consumer.timeout=300000
dubbo.provider.timeout=300000# 连接监控中心,去注册中心直接发现,不配地址
dubbo.monitor.protocol=registry

(3)主类(CwDubboProviderApplication)

package com.cw.dubbo.provider;import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@EnableDubbo
@SpringBootApplication
public class CwDubboProviderApplication {public static void main(String[] args) {SpringApplication.run(CwDubboProviderApplication.class, args);}}

(4)LoginServiceImpl

package com.cw.dubbo.provider;import com.alibaba.dubbo.config.annotation.Service;
import com.cw.dubbo.api.LoginService;@Service
public class LoginServiceImpl implements LoginService {@Overridepublic String login(String name, String pwd) {return "name = " + name + ", pwd = " + pwd;}
}

3、创建服务调用者工程(cw-dubbo-consumer)

该工程是一个SpringBoot工程。

在这里插入图片描述


(1)pom.xml

<?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>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.4.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>cw-dubbo-demo</groupId><artifactId>cw-dubbo-consumer</artifactId><version>1.0.0</version><name>cw-dubbo-consumer</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>cw-dubbo-demo</groupId><artifactId>cw-dubbo-api</artifactId><version>1.0.0</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!--引入springboot整合dubbo的jar包--><dependency><groupId>com.alibaba.boot</groupId><artifactId>dubbo-spring-boot-starter</artifactId><version>0.2.0</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

(2)application.properties

server.port=8079#应用名称
dubbo.application.name=cw-dubbo-consumer#注册中心地址(可以直接全地址)
dubbo.registry.address=zookeeper://127.0.0.1:2181dubbo.consumer.timeout=300000
dubbo.provider.timeout=300000#在注册中心找监控
dubbo.monitor.protocol=registry

(3)主类(CwDubboConsumerApplication)

package com.cw.dubbo.consumer;import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@EnableDubbo
@SpringBootApplication
public class CwDubboConsumerApplication {public static void main(String[] args) {SpringApplication.run(CwDubboConsumerApplication.class, args);}}

(4)LoginController

package com.cw.dubbo.consumer;import com.alibaba.dubbo.config.annotation.Reference;
import com.cw.dubbo.api.LoginService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@RestController
public class LoginController {@Referenceprivate LoginService loginService;@RequestMapping(value = "/login", method = RequestMethod.GET)public String login(@RequestParam(name = "name") String name,@RequestParam(name = "pwd") String pwd){long start = System.currentTimeMillis();String str = loginService.login(name, pwd);long end = System.currentTimeMillis();return "duration = " + (end - start) + "ms, " + str;}
}

三、测试

先启动 cw-dubbo-provider 工程,然后在启动 cw-dubbo-consumer 工程。

启动成功后,在浏览器访问:http://localhost:8079/login?name=admin&pwd=123456

出现如下界面说明远程Dubbo调用成功。
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/446688.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C++primer第一章 开始

运算符打印endl,这是一个被称为操纵符(manipulator)的特殊值。写入endl 的效果是结束当前行&#xff0c;并将与设备关联的缓冲区(buffer)中的内容刷到设备中。缓冲刷新操作可以保证到目前为止程序所产生的所有输出都真正写入输出流中&#xff0c;而不是仅停留在内存中等待写入流…

硬盘 相关知识

磁盘存储数据于轨道上&#xff0c;为了防止数据不被干扰&#xff0c;轨道之间是存在间隙的。如果间隙越小存储的数据越多&#xff0c;但是对数据的写入和读取所使用的磁头是不一样的&#xff0c;写入的磁头比较宽&#xff0c;读取的磁头比较窄。叠瓦式硬盘&#xff0c;将轨道和…

C++primer 第 2 章 变量和基本类型

2.1 基本内置类型 算术类型&#xff08;arithmetictype&#xff09;和空类型&#xff08;void&#xff09;在内的基本数据类型。其中算术类型包含了字符、整型数、布尔值和浮点数。空类型不对应具体的值&#xff0c;仅用于一些特殊的场合&#xff0c;例如最常见的是&#xff0…

SpringBoot 集成Mybatis

文章目录一、创建SpringBoot项目二、添加Mybatis相关依赖三、数据源配置四、创建事务的模型实体类五、创建和数据库交互联系的映射关系类六、创建业务接口和实现类七、创建控制器类八、请求验证一、创建SpringBoot项目 如何创建详见&#xff1a;IDEA 创建 SpringBoot 项目 二、…

C++primer 第 3 章 字符串、向量和数组 3.1 命名空间的using声明 3.2标准库类型string

引言 除了第2章介绍的内置类型之外,C语言还定义了 -个内容丰富的抽象数据类型库。其中,string和 vector是两种最重耍的标准库类型&#xff0c;前者支持可变长字符串&#xff0c;后者则 表示可变长的集合。还有…种标准库类型是迭代器&#xff0c;它是string和vector的配套类型…

C++primer 第 3 章 字符串、向量和数组 3 . 3 标准库类型vector

标准库类型vector表示对象的集合&#xff0c;其中所有对象的类型都相同。集合中的每个对象都有一个与之对应的索引&#xff0c;索引用于访问对象。因为vector"容纳着”其他对象&#xff0c;所以它也常被称作容器(container).第 II部将对容器进行更为详细的介绍。 要想使用…

SpringBoot AOP切面实现

文章目录一、AOP简介二、AOP体系与概念三、AOP实例1、创建SpringBoot工程2、添加依赖3、AOP相关注解3.1、Aspect3.2、Pointcut3.2.1、execution()3.2.2、annotation()3.3、Around3.4、Before3.5、After3.6、AfterReturning3.7、AfterThrowing一、AOP简介 AOP&#xff08;Aspec…

英语口语-文章朗读Week8 Friday

文章 It is a phenomenon that people are losing trust in each other in today’s society. Some people become selfish,and for interest, they are likely to betray their colleagues,friends, and even their relatives. They tend to cater to those who can benefit …

C++primer 第 3 章 字符串、向量和数组 3 . 4 迭代器介绍

3.4迭代器介绍 我们已经知道可以使用下标运算符来访问string对象的字符或vector对象的元素&#xff0c;还有另外一种更通用的机制也可以实现同样的目的&#xff0c;这就是迭代器&#xff08;iterator&#xff09;。在第II部分中将要介绍&#xff0c;除了vector之外&#xff0c…

英语口语-文章朗读Week9 TuesDay

朗读文章 People living in ancient times had no alternative but to do housework manually. They fire the wood when they cook,they hand wash clothes with hands; they sweep the floor with brooms. Now, modern inventions come as a great relief to people. We co…

C++primer 第 3 章 字符串、向量和数组 3 . 5 数组

3.5数组 数组是一种类似于标准库类型vector&#xff08;参见3.3节&#xff0c;第86页&#xff09;的数据结构&#xff0c;但是在性能和灵活性的权衡上又与vector有所不同。与vector相似的地方是&#xff0c;数组也是存放类型相同的对象的容器&#xff0c;这些对象本身没有名字…

C++primer 第 4 章 表达式 4.1基础 4 . 2 算术运算符 4 .3 逻辑和关系运算符 4 . 4 赋值运算符 4 .5 递增和递减运算符 4.6成员访问运算符

表达式由一个或多个运算对象(operand)组成&#xff0c;对表达式求值将得到一个结果(result)字面值和变量是最简单的表达式(expression),其结果就是字面值和变量的值。把一个运算符(operator)和一个或多个运算对象组合起来可以生成较复杂的表达式 4.1基础 有几个基础概念对表达…

codeforces 266B-C语言解题报告

266B题目网址 题目解析 输入n,t,排队情况s,输出第t次循环后,排队情况 举例: 输入: 5 1 BGGBG 输出: GBGGB 2.输入的n代表排队的人数,t代表整个循环t次之后再输出结果 3.注意点: 使用while()大循环去控制t次的循环,使用for()内层循环去遍历整个字符串 如果if(s[j]‘B’&…

英语口语-文章朗读Week9 Wednesday

英语文章 Birds of the same species flock together&#xff0c; People tend to look for someone like themselves to be friends. But having the same interests is not the only standard when we are seeking friends. In most cases, especially for adults, people l…

C++primer 第 4 章 表达式 4.7条件运算符 4.8位运算符 4.9 sizeof运算符 4.10逗号运算符 4.11类型转换 4 . 1 2 运算符优先级表

4.7条件运算符 条件运算符(?&#xff1a;)允许我们把简单的if else逻辑嵌入到单个表达式当中&#xff0c;条件运算符按照如下形式使用&#xff1a;cond ? expr1 : expr2;其中cond是判断条件的表达式&#xff0c;而expr1和expr2是两个类型相同或可能转换为某个公共类型的表达…

Git 之 git tag标签使用

目录一、简介二、本地tag操作1、创建tag标签&#xff08;1&#xff09;创建轻量标签&#xff08;2&#xff09;创建附注标签2、查看tag标签&#xff08;1&#xff09;查看标签列表&#xff08;2&#xff09;查看标签提交信息&#xff08;3&#xff09;在提交历史中查看标签3、删…

C++primer 第 5 章语句 5.2语句作用域 5.3条件语句 5 . 4 迭代语句 5.5跳转语句 5.6 try语句块和异常处理

5 . 1 简单语句 C语言中的大多数语句都以分号结束&#xff0c;一个表达式&#xff0c;比如ival 5 , 末尾加上分号就变成了表达式语句(expression statement)。表达式语句的作用是执行表达式并丢弃掉求值结果&#xff1a;ival 5&#xff1b; // 一条没什么实际用处的表达式语…

英语口语-文章朗读Week9Thursday

英语文章 Everyone has his or her own dreams. Some people wants to be millionaires so they can give many generous donations later; some people want to be scientists so they can bring many conveniences to the world; some people only want to be bus-drivers s…

操作系统 内存管理相关知识

cpu执行程序的基本过程 译码器 输入为n管脚&#xff0c;输出为2^n根管脚&#xff0c;编号为从0到2^(n-1)&#xff0c;用少的输入端控制更多的输出端最常用的是三八译码器AD(Address bus)地址总线: 选中一行数据每一行 8bit 组成8吧B cpu输入端32根线&#xff0c;输出端就可以控…

Chrome浏览器必装插件!尤其程序猿!

Chrome 浏览器有一个好处&#xff0c;就是插件极其丰富&#xff0c;只有你想不到的&#xff0c;没有你找不到的&#xff0c;这恐怕是 Chrome 浏览器被众多爱好者钟爱的原因吧。 言归正传&#xff0c;今天来给大家推荐 10 款我自己珍藏的 Chrome 浏览器插件。 1、crxMouse Ch…