首页 > 其他分享 >Springboot整合Apache Dubbo

Springboot整合Apache Dubbo

时间:2022-12-19 14:07:55浏览次数:48  
标签:Dubbo Springboot dubbo zookeeper boot apache import Apache org


​Springboot​​​整合​​Apache Dubbo​

一、 通过​​Docker​​​安装​​Zookeeper​

​docker-compose​​​的​​yaml​​文件

version: '3'
services:
zookeeper:
image: zookeeper:3.6.1
container_name: zookeeper
restart: always
ports:
- 2181:2181
volumes:
- ./zookeeper/data:/data
- ./zookeeper/log:/datalog
networks:
- zk-esnet
networks:
zk-esnet:
driver: bridge

启动执行:

docker-compose up -d # 后台启动

二、整合​​springboot​​​和​​Apache Dubbo​

2.1、创建一个​​maven​​项目

项目结果如图:

Springboot整合Apache Dubbo_dubbo-admin

2.2、引入依赖

​root​​​模块的​​pom​​文件

<?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">
<modelVersion>4.0.0</modelVersion>

<!--添加SpringBoot parent支持-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
</parent>

<groupId>com.code</groupId>
<artifactId>code-one</artifactId>
<packaging>pom</packaging>
<version>1.0.0</version>

<!--添加两个子模块-->
<modules>
<module>dubbo-provider</module>
<module>dubbo-consumer</module>
</modules>

<dependencies>
<!--添加SpringBoot测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-dependencies-zookeeper -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>2.7.8</version>
<type>pom</type>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

服务提供者模块的​​pom​​文件

<?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>code-one</artifactId>
<groupId>com.code</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>dubbo-provider</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>
</project>

服务消费者模块的​​pom​​文件

<?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>code-one</artifactId>
<groupId>com.code</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>dubbo-consumer</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.code</groupId>
<artifactId>dubbo-provider</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>
</project>
2.3、添加服务提供者相关代码

添加一个实体类:(必须实现序列化接口)

package com.dubbo.entity;

import java.io.Serializable;

/**
* @author 墨龙吟之天问
*/
public class User implements Serializable {

private static final long serialVersionUID = 565515108558415476L;

private Long id;

private String username;

private String password;

private Integer age;

// ... set / get ...

@Override
public String toString() {
return "User[" +
"id:" + id +
", username:'" + username + '\'' +
", password:'" + password + '\'' +
", age:" + age +
']';
}
}

添加​​service​​接口和实现

import com.dubbo.entity.User;
import java.util.List;

/**
* @author 墨龙吟之天问
*/
public interface UserService {
/**
* 查询列表
* @return
*/
List<User> queryLists();
}


import org.apache.commons.lang3.RandomStringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import com.dubbo.entity.User;
import com.dubbo.service.UserService;
import java.util.ArrayList;
import java.util.List;

/**
* UserService的实现类
* @author 墨龙吟之天问
*/
@DubboService(version = "${dubbo.service.version}")
public class UserServiceImpl implements UserService {
@Override
public List<User> queryLists() {
// 构造一手假数据
List<User> users = new ArrayList<>();
for (int i = 0; i < 10; i++) {
User user = new User();
user.setAge(i * 10);
user.setId((long) i);
user.setPassword(RandomStringUtils.randomAlphanumeric(10));
user.setUsername(RandomStringUtils.randomAlphabetic(5));
users.add(user);
}
return users;
}
}

添加启动类和配置文件

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

/**
* @author 墨龙吟之天问
*/
@SpringBootApplication
public class ProviderApplication {

public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class);
}
}
server:
port: 7777
spring:
application:
name: dubbo-provider-service
dubbo:
service:
version: 1.0.0
application:
name: dubbo-provider-service
scan:
base-packages: com.dubbo.service.impl
# 注册中心的地址
registry:
address: zookeeper://192.168.31.25:2181
protocol:
name: dubbo
port: 20880
2.4、添加服务消费者者相关代码

编写一个​​controller​​​来调用远程​​service​​:

import com.dubbo.entity.User;
import com.dubbo.service.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
* @author 墨龙吟之天问
*/
@RestController
public class IndexController {

@DubboReference(version = "${dubbo.service.version}", loadbalance = "roundrobin")
private UserService userService;


@GetMapping("/user")
public List<User> index() {
return userService.queryLists();
}

}

编写配置文件将服务提供者的服务注册到​​zookeeper​​中,然后写一个配置文件

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

/**
* @author 墨龙吟之天问
*/
@SpringBootApplication
public class ConsumerApplication {

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

}
server:
port: 7778
spring:
application:
name: dubbo-consumer-service
dubbo:
service:
version: 1.0.0
application:
name: dubbo-consumer-service
registry:
address: zookeeper://192.168.31.25:2181
2.5、启动服务

将IDEA的仪表板(​​dashboard​​)启动,这个可以方便我们去统一管理服务。

Springboot整合Apache Dubbo_docker-compose_02


访问接口:​​localhost:7778/user​

[
{"id":0,"username":"oBhxn","password":"5VIogUrCG3","age":0}, {"id":1,"username":"DqYti","password":"XDShtZrszU","age":10}, {"id":2,"username":"FvRTn","password":"S7hug0oRWz","age":20}, {"id":3,"username":"BMVRN","password":"apwhk8llZw","age":30}, {"id":4,"username":"vHnWF","password":"14fp8mWbs8","age":40}, {"id":5,"username":"SNHkl","password":"hA5J0509YY","age":50}, {"id":6,"username":"yOAtC","password":"NQo50bo9mq","age":60}, {"id":7,"username":"QrgpW","password":"S49VOeOrk3","age":70}, {"id":8,"username":"zJSXK","password":"rR31TAuXJA","age":80}, {"id":9,"username":"TwQMM","password":"3Y3c0JlcpJ","age":90}
]

三、​​zookeeper​​​和​​dubbo​​一些工具

3.1、​​zookeeper​​​监控工具:​​ZooInspector​

下载地址:https://issues.apache.org/jira/secure/attachment/12436620/ZooInspector.zip

解压之后的​​build​​​目录下有一个​​start.bat​​的脚本,双击启动。

Springboot整合Apache Dubbo_dubbo-admin_03

连接之后就可以看到我们注册的服务了

Springboot整合Apache Dubbo_spring_04

这个版本比较旧,新版本的下载地址就在后面连接里面。

链接:https://pan.baidu.com/s/1jk5a1Wv_PPkQaJ8rmZERog
提取码:kqh4

3.2、​​Dubbo​​​监控工具​​dubbo-admin​

这个工具我们依然在​​docker​​​中启动,我们在上面那个​​docker-compose.yml​​​文件中增加一个​​apache-dubbo-admin​​的容器。

version: '3'
services:
zookeeper:
image: zookeeper:3.6.1
container_name: zookeeper
restart: always
ports:
- 2181:2181
volumes:
- ./zookeeper/data:/data
- ./zookeeper/log:/datalog
networks:
- zk-esnet
dubbo-admin:
container_name: dubbo-admin
image: apache/dubbo-admin
links:
- zookeeper:zookeeper
depends_on:
- zookeeper
ports:
- "8080:8080"
environment:
- admin.registry.address=zookeeper://zookeeper:2181
- admin.config-center=zookeeper://zookeeper:2181
- admin.metadata-report.address=zookeeper://zookeeper:2181
restart: always
networks:
- zk-net
networks:
zk-esnet:
driver: bridge

启动之后访问:​​IP:8080​​就可以进入管理页面

Springboot整合Apache Dubbo_dubbo-admin_05


标签:Dubbo,Springboot,dubbo,zookeeper,boot,apache,import,Apache,org
From: https://blog.51cto.com/luckyqilin/5952241

相关文章

  • springboot+postgresql集成anyline试水
    anyline是什么简单讲就是一个工具可以让你抛开常规的机械性建mapper、dao、sql,用通用的语句查询和操作数据库表。目前也在初步探索中,感受还不深。官网文档:http://doc.any......
  • 5. MinIO与springboot的集成
    MinIO与springboot的集成搭建一个springboot的项目,集成MinIO实现文件的管理。一、搭建springboot环境IntelliJIDEAJDK17gradle-7.5.1springboot2.7.6项目地址:g......
  • springboot实现AOP切面编程
    概述AOP(AspectOrientedProgramming)即面向切面编程。面向切面是面向对象中的一种方式而已。在代码执行过程中,动态嵌入其他代码,叫做面向切面编程(将交叉业务逻辑封装成成......
  • 基于 Dubbo Admin 临时踢除问题服务实例
    Dubbo提供临时踢除问题服务实例的服务治理能力,可以在无需重启应用的情况下,临时踢除问题服务实例。Dubbo可以通过XML配置,注解配置,动态配置实现临时踢除问题服务实例,这里主......
  • 亲手实现一个springboot默认配置&起步加载
    实现一、默认配置1、创建springboot项目引入spring-boot-dependencies依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-......
  • 【java】(二) SpringBoot 源码解析——run启动加载过程——准备环境
    1.前言深入学习springboot笔记系列,可能会有错误还请指正,互相勉励,互相学习。上一章讲了SpringApplicaiton是如何初始化的,本章讲解后续的run方法的启动过程。本章涉及......
  • SpringBoot3.x中spring.factories功能被移除的解决方案
    背景笔者所在项目组在搭建一个全新项目的时候选用了​​SpringBoot3.x​​​,项目中应用了很多​​SpringBoot2.x​​​时代相关的第三方组件例如​​baomidou​​​出品的​......
  • Dubbo架构设计与源码解析(一) 架构设计
    作者:黄金一、架构演变单应用架构---->垂直架构---->分布式架构---->微服务架构---->云原生架构二、Dubbo总体架构1、角色职能•Container:服务......
  • springboot项目打包报错:Type org.springframework.boot.maven.RepackageMojo not pres
    折磨了好久的一个问题,换过spring-boot-maven-plugin版本,但不见效。今天参考这篇文章:springboot打包RepackageMojonotpresent,版本改成2.6.2,不行。就想,可能是我用的idea......
  • 基于 Dubbo Admin 动态调整服务超时时间
    Dubbo提供动态调整超时时间的服务治理能力,可以在无需重启应用的情况下,动态调整服务超时时间。Dubbo可以通过XML配置,注解配置,动态配置实现动态调整超时时间,这里主要介绍动......