源码: gitee
1.8.0_281
2.1.2
创建一个简单的springboot或者maven项目, 或者代码库(gitee/github/其他代码库管理平台)创建一个空白的拉下来, 最后只保留一个pom.xml
即可.
版本控制参考: 版本说明
其中有一句话
Spring Cloud Dubbo 从 2021.0.1.0 起已被移除出主干,不再随主干演进
, 虽然停止维护了, 但是用户体量大, 而且还有当当的dubbox在更新. 在有明显漏洞或者其他的问题时候还有其他选择, 暂时选作dubbo学习时候使用
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
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>
<groupId>com.cloud.nacos.dubbo.demo</groupId>
<artifactId>demo-parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<!-- spring boot 版本控制 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath/>
</parent>
<!-- 版本说明: https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E-->
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<spring-cloud-clibaba.version>2021.0.1.0</spring-cloud-clibaba.version>
<spring-cloud.version>2021.0.1</spring-cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- spring cloud 版本控制 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- spring cloud alibaba 版本控制 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-clibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
右键项目名 -> new -> Module, 分别创建:
<dependencies>
<!-- nacos 注册发现 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- nacos 配置 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- dubbo -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
public class LoginUser implements Serializable {
/**
* 用户id/用户名/昵称/角色/权限等等基本信息
*/
String username;
/**
* 登录时间/设备/ip/失效等等属性
*/
Date accessTime;
}
public interface UserService {
/**
* 访问
*
* @return 结果对象
*/
LoginUser access();
}
实现接口, 将接口推送到nacos上
<dependencies>
<!-- 公共模块 -->
<dependency>
<groupId>com.cloud.nacos.dubbo.demo</groupId>
<artifactId>common-module</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
spring:
main:
# spring boot 2.6.3默认不允许循环依赖, dubbo的这个版本存在循环依赖
allow-circular-references: true
application:
name: demo-server
cloud:
nacos:
config:
enable: false
discovery:
server-addr: 192.168.0.110:8848
namespace: 5467517c-4121-4275-b459-b92a7a12f508
dubbo:
consumer:
retries: 5
protocol:
port: -1
name: dubbo
scan:
# 扫描实现类的包路径(可配置多个或数组)
base-packages: com.cloud.nacos.dubbo.demo
registry:
address: nacos://${spring.cloud.nacos.discovery.server-addr}?namespace=${spring.cloud.nacos.discovery.namespace}
cloud:
# 默认*订阅所有, 所以在此处写一个不存在的提供者
# (可订阅多个用,隔开或者用数组的配置方式 -: name)
subscribed-services: "-"
@DubboService
public class UserServiceImpl implements UserService {
/**
* 访问
*
* @return 结果对象
*/
@Override
public LoginUser access() {
return LoginUser.builder().username("赵先生").accessTime(new Date()).build();
}
}
@SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
调用接口, 抛出rest接口
<dependencies>
<!-- 公共模块 -->
<dependency>
<groupId>com.cloud.nacos.dubbo.demo</groupId>
<artifactId>common-module</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
spring:
main:
# spring boot 2.6.3默认不允许循环依赖, dubbo的这个版本存在循环依赖
allow-circular-references: true
application:
name: demo-client
cloud:
nacos:
config:
enable: false
discovery:
server-addr: 192.168.0.110:8848
namespace: 5467517c-4121-4275-b459-b92a7a12f508
dubbo:
consumer:
retries: 5
protocol:
port: -1
name: dubbo
subscribed-services: nacos-server
@RestController
public class UserController {
@DubboReference
UserService userService;
@RequestMapping
public LoginUser access() {
return userService.access();
}
}
@SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
{"username":"赵先生","accessTime":"2023-08-14T05:54:11.582+00:00"}
的结果算正常我没有用到下面的注解, 应该是配置了相关内容后会隐式生效
@EnableDubbo
: 开启注解Dubbo功能, 用在主方法入口@EnableDiscoveryClient
: 开启服务注册发现功能, 用在主方法入口@RefreshScope
: 实现配置自动更新, 用在配置实体类今天给大家分享一个SpringBoot整合Tess4j库实现图片文字识别的小案例,希望xdm喜欢。文末有案例代码的Git地址,可以自己下载了去玩玩儿或继续扩展也行。