B站学习视频

基于Dubbo3.2.9、Nacos2.3.0、SpringBoot 2.7.17、Dubbo-Admin 0.6.0、Jdk1.8 搭建的Dubbo学习Demo

一、前置安装

1-1、Nacos 安装

我本地是通过docker-compose来安装nacos的,如果需要其它方式安装可以去百度找下教程,版本是2.3.0的

docker-compose 安装Nacos

1-1-1、创建 namespase

1-1-2、新增配置文件

dubbo3-consumer

dubbo:

application:

id: xdx-dubbo3-consumer

name: xdx-dubbo3-consumer

serialize-check-status: WARN

registry:

address: nacos://${nacos.config.server-addr}

parameters.namespace: ${nacos.config.namespace}

dubbo3-provider

server:

port: 5656

dubbo:

application:

id: xdx-dubbo3-provider

name: xdx-dubbo3-provider

serialize-check-status: WARN

protocol:

id: dubbo

name: dubbo

host: 127.0.0.1

port: 7788

serialization: hessian2

registry:

address: nacos://${nacos.config.server-addr}

parameters.namespace: ${nacos.config.namespace}

dubboParams: xdx971

dubbo3-provider2

server:

port: 5657

dubbo:

application:

id: xdx-dubbo3-provider

name: xdx-dubbo3-provider

serialize-check-status: WARN

protocol:

id: dubbo

name: dubbo

host: 127.0.0.1

port: 7789

serialization: hessian2

registry:

address: nacos://${nacos.config.server-addr}

parameters.namespace: ${nacos.config.namespace}

dubboParams: xdx

1-2、Dubbo-Admin 安装

### 1-2-1、下载代码

https://github.com/apache/dubbo-admin/releases

1-2-2、环境安装

Dubbo-Admin 是基于 Vue和 Java开发的,下载源码后想要运行就需要依赖以下环境

Node v16.20.2 【18会报错】Jdk 1.8Maven

1-2-3、服务启动

1-2-3-1、前端启动

进入下面的路径执行npm 或 yarn 命令 /dubbo-admin-0.6.0/dubbo-admin-ui

npm install / yarn installnpm run dev / yarn run dev

1-2-3-2、后端启动

配置文件修改为如下(主要是指定 nacos地址),然后用 idea 打开 dubbo-admin-server 项目,下载maven依赖后启动

server.port=38080

dubbo.protocol.port=30880

dubbo.application.qos-port=32222

# centers in dubbo, if you want to add parameters, please add them to the url

admin.registry.address=nacos://127.0.0.1:8848?namespace=70b158a6-7e64-478d-8c5b-699089aa81d1

admin.config-center=nacos://127.0.0.1:8848?namespace=70b158a6-7e64-478d-8c5b-699089aa81d1

admin.metadata-report.address=nacos://127.0.0.1:8848?namespace=70b158a6-7e64-478d-8c5b-699089aa81d1

# dubbo-admin 登录账号密码

admin.root.user.name=root

admin.root.user.password=root

#session timeout, default is one hour

admin.check.sessionTimeoutMilli=3600000

#compress

server.compression.enabled=true

server.compression.mime-types=text/css,text/javascript,application/javascript

server.compression.min-response-size=10240

#token timeout, default is one hour

admin.check.tokenTimeoutMilli=3600000

#Jwt signingKey

admin.check.signSecret=86295dd0c4ef69a1036b0b0c15158d77

#dubbo config

dubbo.application.name=dubbo-admin

dubbo.registry.address=${admin.registry.address}

#dubbo.registry.parameters.namespace=70b158a6-7e64-478d-8c5b-699089aa81d1

# id generate type

mybatis-plus.global-config.db-config.id-type=none

dubbo.application.logger=slf4j

二、搭建Dubbo-Demo

项目整体结构图

2-1、父工程项目依赖

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">

4.0.0

com.xdx97

dubbo3-demo

pom

1.0-SNAPSHOT

dubbo3-provider

dubbo3-consumer

dubbo3-api

dubbo3-provider2

8

8

1.8

2.7.17

0.2.12

3.2.9

2-2、api

dubbo提供和调用本地方法一样的体验,所以需要提供api包

2-2-1、pom

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">

dubbo3-demo

com.xdx97

1.0-SNAPSHOT

4.0.0

dubbo3-api

8

8

2-2-2、DemoService —接口

package com.xdx97.dubbo3.api;

public interface DemoService {

String getString();

}

2-3、provider —服务提供者

2-3-1、pom

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">

dubbo3-demo

com.xdx97

1.0-SNAPSHOT

4.0.0

dubbo3-provider

8

8

org.springframework.boot

spring-boot-starter-parent

${spring.boot.version}

pom

import

org.springframework.boot

spring-boot-starter

org.springframework.boot

spring-boot-starter-web

com.alibaba.boot

nacos-config-spring-boot-starter

${nacos.boot.version}

org.apache.dubbo

dubbo-spring-boot-starter

${dubbo.version}

javassist

org.javassist

org.apache.dubbo

dubbo-registry-nacos

${dubbo.version}

com.xdx97

dubbo3-api

1.0-SNAPSHOT

compile

2-3-2、启动类

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

@EnableDubbo

public class App {

public static void main(String[] args) {

SpringApplication.run(App.class, args);

}

}

2-3-3、DemoServiceImpl —服务实现类

这里的代码做了小小的改动

实时读取nacos的配置文件数据验证dubbo超时设置和重试机制

import com.alibaba.nacos.api.config.annotation.NacosValue;

import com.xdx97.dubbo3.api.DemoService;

import org.apache.dubbo.config.annotation.DubboService;

import java.util.concurrent.atomic.AtomicLong;

@DubboService(version = "1.0.0")

public class DemoServiceImpl implements DemoService {

// nacos.config.autoRefresh = true 这个配置要打才可以

@NacosValue(value = "${dubboParams}", autoRefreshed = true)

private String dubboParams;

private AtomicLong atomicLong = new AtomicLong(0);

@Override

public String getString() {

long l = atomicLong.incrementAndGet();

try {

Thread.sleep(1000 - (100 * l));

}catch (Exception e) {

e.printStackTrace();

}

return dubboParams;

}

}

2-3-4、application —配置文件

nacos:

config:

# 指定命名空间

namespace: 70b158a6-7e64-478d-8c5b-699089aa81d1

#配置服务地址

server-addr: 127.0.0.1:8848

accessKey:

secretKey:

#data-ids 为新增加的data-id

data-ids: dubbo3-provider

#配置类型

type: yaml

#是否启动刷新配置

autoRefresh: true

#运行时启用

bootstrap:

enable: true

注:

nacos的 namespace 在nacos上面已经配置好了dubbo3-provider2 的代码和 dubbo3-provider是一样的,创建2个 provider是为了验证它的负载均衡

2-4、consumer —消费者

2-4-1、pom

引入的依赖和【2-3-1】一样

2-4-2、启动类

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class App {

public static void main(String[] args) {

SpringApplication.run(App.class, args);

}

}

2-4-3、DemoConsumer —消费者测试

这里设置了超时时间为 1s,重试2次

import com.xdx97.dubbo3.api.DemoService;

import org.apache.dubbo.config.annotation.DubboReference;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RestController;

@RestController

public class DemoConsumer {

@DubboReference(version = "1.0.0", timeout = 1000, retries = 1)

private DemoService demoService;

@GetMapping("/fun-test")

public String funTest() {

return demoService.getString();

}

}

三、测试

3-1、服务启动

服务有点多,按照下面顺序依次启动

MySQL (nacos持久化的依赖)NacosDubbo-admin-serviceDubbo-admin-uiconsumerproviderprovider2

3-2、验证

多请求几次接口 http://localhost:8080/fun-test ,然后再去把 provider 或 provider2 中的dubboParams 参数修改掉,再次请求几次,可以得出下面的结论

Dubbo 项目搭建成功Dubbo 可以自动负载Dubbo 的重试和超时机制Nacos的热更新

四、源代码获取

关注微信公众号:小道仙97回复:dubbo3-demo-xdx

参考链接

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: