纵有疾风起
人生不言弃

SpringBoot整合Dubbo

通过三个工程演示整合过程

工程一:API工程:服务端与客户端共同需要引入的工程

 

SpringBoot整合Dubbo插图

 pom:无特殊依赖,下边内容供服务端与客户端引用此工程使用

  <groupId>com.boot.dubbo.demo</groupId>  <artifactId>dubbo-api</artifactId>  <version>1.0-SNAPSHOT</version>

实体类:

package com.boot.dubbo.demo.domain;import java.io.Serializable;/** * 用户信息类 */public class UserInfo implements Serializable {    private String account;    private String password;    public String getAccount() {        return account;    }    public void setAccount(String account) {        this.account = account;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }}

API:

package com.boot.dubbo.demo.api;import com.boot.dubbo.demo.domain.UserInfo;public interface UserService {    // 定义用户登录的api    UserInfo login(UserInfo user);}

工程二:生产者工程:服务端

SpringBoot整合Dubbo插图(1)

pom

 <!--依赖API-->        <dependency>            <groupId>com.boot.dubbo.demo</groupId>            <artifactId>boot-dubbo-api</artifactId>            <version>1.0-SNAPSHOT</version>        </dependency>

dubbo必须依赖项:(使用zk)

<dependency>    <groupId>com.alibaba.spring.boot</groupId>    <artifactId>dubbo-spring-boot-starter</artifactId>    <version>2.0.0</version></dependency><dependency>    <groupId>com.101tec</groupId>    <artifactId>zkclient</artifactId>    <version>0.10</version></dependency><dependency>    <groupId>org.apache.zookeeper</groupId>    <artifactId>zookeeper</artifactId>    <version>3.4.10</version>    <exclusions>        <exclusion>            <groupId>org.slf4j</groupId>            <artifactId>slf4j-log4j12</artifactId>        </exclusion>        <exclusion>            <groupId>log4j</groupId>            <artifactId>log4j</artifactId>        </exclusion>    </exclusions></dependency>

application.yml:配置dubbo配置项

spring:  dubbo:    application:      name: dubbo-provider    protocol:      name: dubbo      port: 20880    registry:      address: zookeeper://192.168.3.101:2181

 

package com.boot.dubbo.demo.dubboprovider.service;import com.alibaba.dubbo.config.annotation.Service;import com.boot.dubbo.demo.api.UserService;import com.boot.dubbo.demo.domain.UserInfo;import org.springframework.stereotype.Component;@Component@Service(interfaceClass = UserService.class) //注意是dubbo的注解,向外暴露接口public class UserServiceImpl implements UserService {    public UserInfo login(UserInfo user) {        UserInfo reUser = new UserInfo();        reUser.setAccount("登录的账号为:"+user.getAccount());        reUser.setPassword("登录的密码为:"+user.getPassword());        return reUser;    }}

 

package com.boot.dubbo.demo.dubboprovider;import com.alibaba.dubbo.spring.boot.annotation.EnableDubbo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication@EnableDubbo // 启用dubbopublic class DubboProviderApplication {    public static void main(String[] args) {        SpringApplication.run(DubboProviderApplication.class, args);    }}

 

工程三:消费者工程:客户端

SpringBoot整合Dubbo插图(2)

pom依赖与工程二相同

application.yml配置:

spring:  dubbo:    application:      name: dubbo-provider    protocol:      name: dubbo      port: 20880    registry:      address: zookeeper://192.168.3.101:2181server:  port: 8081

Controller

package com.boot.dubbo.demo.dubboconsumer.controller;import com.alibaba.dubbo.config.annotation.Reference;import com.boot.dubbo.demo.api.UserService;import com.boot.dubbo.demo.domain.UserInfo;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class UserController {    @Reference // 引用dubbo服务器提供服务器接口    private UserService userService;    @GetMapping("/login")    public UserInfo login(UserInfo userInfo) {        return userService.login(userInfo);    }}

 

package com.boot.dubbo.demo.dubboconsumer;import com.alibaba.dubbo.spring.boot.annotation.EnableDubbo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication@EnableDubbopublic class DubboConsumerApplication {    public static void main(String[] args) {        SpringApplication.run(DubboConsumerApplication.class, args);    }}

 

这样客户端就可以通过Dubbo调用服务端提供的方法了

文章转载于:https://www.cnblogs.com/lm970585581/p/9845746.html

原著是一个有趣的人,若有侵权,请通知删除

未经允许不得转载:起风网 » SpringBoot整合Dubbo

分享到: 生成海报
avatar

评论 抢沙发

评论前必须登录!

立即登录   注册

切换注册

登录

忘记密码 ?

切换登录

注册

我们将发送一封验证邮件至你的邮箱, 请正确填写以完成账号注册和激活