Loading

springboot整合dubbo

1.准备工作

首先创建一个空的项目,然后再项目里创建三个模块,分别为springboot-dubbo-interface,springboot-dubbo-provider,springboot-dubbo-consumer,其中springboot-dubbo-interface模块只是一个简单的maven工程,用来存放接口,在这里我把实体类也放在了该模块,而其他的两个是springboot项目。三个模块如下:

pCRYoHU.png

2.springboot-dubbo-interface实现

模块结构如下:

pCRYXg1.png

User.java:

package org.example.entity;

import java.io.Serializable;

public class User implements Serializable {
    private String name;
    private Integer age;
    private Integer id;
    private String address;
    private String phone;
    private String email;
    private String sex;


    public User() {
    }

    public User(String name, Integer age, Integer id, String address, String phone, String email, String sex) {
        this.name = name;
        this.age = age;
        this.id = id;
        this.address = address;
        this.phone = phone;
        this.email = email;
        this.sex = sex;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return age
     */
    public Integer getAge() {
        return age;
    }

    /**
     * 设置
     * @param age
     */
    public void setAge(Integer age) {
        this.age = age;
    }

    /**
     * 获取
     * @return id
     */
    public Integer getId() {
        return id;
    }

    /**
     * 设置
     * @param id
     */
    public void setId(Integer id) {
        this.id = id;
    }

    /**
     * 获取
     * @return address
     */
    public String getAddress() {
        return address;
    }

    /**
     * 设置
     * @param address
     */
    public void setAddress(String address) {
        this.address = address;
    }

    /**
     * 获取
     * @return phone
     */
    public String getPhone() {
        return phone;
    }

    /**
     * 设置
     * @param phone
     */
    public void setPhone(String phone) {
        this.phone = phone;
    }

    /**
     * 获取
     * @return email
     */
    public String getEmail() {
        return email;
    }

    /**
     * 设置
     * @param email
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * 获取
     * @return sex
     */
    public String getSex() {
        return sex;
    }

    /**
     * 设置
     * @param sex
     */
    public void setSex(String sex) {
        this.sex = sex;
    }

    public String toString() {
        return "User{name = " + name + ", age = " + age + ", id = " + id + ", address = " + address + ", phone = " + phone + ", email = " + email + ", sex = " + sex + "}";
    }
}

注意:User类必需实现Serializable接口,因为后面User对象会从服务提供者传输到消费者,dubbo会序列化User对象。

UserService接口:

package org.example.service;

import org.example.entity.User;

public interface UserService {
    public User getUser();
}

最后记得把该模块install一下。

3.springboot-dubbo-provider实现

模块结构如下:

pCRtEvt.png

pom.xml引入依赖:

<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<!--引入接口工程-->
		<dependency>
			<groupId>org.example</groupId>
			<artifactId>springboot-dubbo-interface</artifactId>
			<version>1.0-SNAPSHOT</version>
		</dependency>
		<!--SpringBoot框架web项目起步依赖-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!--Dubbo集成SpringBoot起步依赖-->
		<dependency>
			<groupId>com.alibaba.spring.boot</groupId>
			<artifactId>dubbo-spring-boot-starter</artifactId>
			<version>2.0.0</version>
		</dependency>

		<!--zookeeper注册中心依赖-->
		<dependency>
			<groupId>com.101tec</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.11</version>
		</dependency>
</dependencies>

application.properties:

#设置内嵌Tomcat端口号
server.port=8082
#设置上下文根
server.servlet.context-path=/

#设置dubbo的配置
#服务提供者声明名称:必须保证服务名称的唯一性,它的名称是dubbo内部使用的唯一标识
spring.application.name=springboot-integration-dubbo-service-provider
#声明当前工程是一个服务提供者
spring.dubbo.server=true
#设置访问服务的协议及端口号,dubbo官方推荐使用的是dubbo协议,端口号默认20880
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
#指定zookeeper注册中心地址和端口号
spring.dubbo.registry=zookeeper://127.0.0.1:2181

UserServiceImpl.java:

package com.example.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import org.example.entity.User;
import org.example.service.UserService;
import org.springframework.stereotype.Component;

@Component
@Service(timeout = 3000,retries = 2) // 当前服务三秒超时,超时后有两次重试,如果还是不成功,就不再重试
public class UserServiceImpl implements UserService {
    @Override
    public User getUser() {
        User user = new User();
        user.setId(10001);
        user.setAddress("河南****");
        user.setAge(18);
        user.setName("张三");
        user.setEmail("158361217272@163.com");
        user.setPhone("158361217272");
        user.setSex("男");
        return user;
    }
}

此处的@Service不再是以前的了,他是dubbo提供的,不要用错了,最后在启动类加上@EnableDubboConfiguration注解。

4.springboot-dubbo-consumer实现

模块结构如下:

pCRtCUe.png

pom文件和服务提供者一样,然后再引入一个lombok依赖:

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.26</version>
    <scope>provided</scope>
</dependency>

application.properties:

#设置内嵌Tomcat端口号
server.port=8083
#设置上下文根
server.servlet.context-path=/

#设置dubbo的配置
#服务提供者声明名称:必须保证服务名称的唯一性,它的名称是dubbo内部使用的唯一标识
spring.application.name=springboot-integration-dubbo-consumer
#指定zookeeper注册中心地址和端口号
spring.dubbo.registry=zookeeper://127.0.0.1:2181

UserController:

package com.example.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.example.entity.User;
import org.example.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Reference
    private UserService userService;

    @GetMapping
    public User getUser(){
        log.info("请求了一次用户数据!");
        return userService.getUser();
    }
}

注入的时候使用的是@Reference,他是dubbo提供的,不要用错了。

5.测试

  • 启动zookeeper
  • 启动provider
  • 启动consumer

然后在浏览器访问127.0.0.1:8083/user,结果如下:

{"name":"张三","age":18,"id":10001,"address":"河南****","phone":"158361217272","email":"158361217272@163.com","sex":"男"}
posted @ 2023-07-10 17:57  minqiliang  阅读(123)  评论(0编辑  收藏  举报
-->