sa-token-dubbo3 插件的 demo 示例

This commit is contained in:
click33 2023-06-21 05:33:23 +08:00
parent 1b5b0d8775
commit 9ca214c016
24 changed files with 423 additions and 6 deletions

View File

@ -12,11 +12,11 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
*/
@EnableDubbo
@SpringBootApplication
public class ConsumerApplication {
public class DubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
System.out.println("ConsumerApplication 启动成功");
SpringApplication.run(DubboConsumerApplication.class, args);
System.out.println("DubboConsumerApplication 启动成功");
}
}

View File

@ -12,11 +12,11 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
*/
@EnableDubbo
@SpringBootApplication
public class ProviderApplication {
public class DubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
System.out.println("ProviderApplication 启动成功");
SpringApplication.run(DubboProviderApplication.class, args);
System.out.println("DubboProviderApplication 启动成功");
}
}

View File

@ -0,0 +1,13 @@
target/
.project
.classpath
.settings
/.idea/
node_modules/
bin/
.settings/
unpackage/
/.apt_generated/
/.apt_generated_tests/

View File

@ -0,0 +1,79 @@
<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>
<groupId>com.pj</groupId>
<artifactId>sa-token-demo-dubbo3-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- SpringBoot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<!--<version>2.3.1.RELEASE</version>-->
<version>2.5.15</version>
</parent>
<!-- 指定一些属性 -->
<properties>
<java.version>1.8</java.version>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
<sa-token.version>1.34.1</sa-token.version>
<dubbo.version>3.1.5</dubbo.version>
<nacos.version>2.1.2</nacos.version>
</properties>
<dependencies>
<!-- SpringBoot Web模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Sa-Token -->
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-spring-boot-starter</artifactId>
<version>${sa-token.version}</version>
</dependency>
<!-- Sa-Token整合 Redis (使用jackson序列化方式) -->
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-redis-jackson</artifactId>
<version>${sa-token.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<!-- Dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<!-- Dubbo 注册到 Nacos -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>${nacos.version}</version>
</dependency>
<!-- Sa-Token 整合 Dubbo -->
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-dubbo3</artifactId>
<version>${sa-token.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,22 @@
package com.pj;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Dubbo3 服务消费端
*
* @author click33
*
*/
@EnableDubbo
@SpringBootApplication
public class Dubbo3ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(Dubbo3ConsumerApplication.class, args);
System.out.println("Dubbo3ConsumerApplication 启动成功");
}
}

View File

@ -0,0 +1,16 @@
package com.pj.more;
public interface DemoService {
/**
* 登录
* @param loginId 账号id
*/
void doLogin(Object loginId);
/**
* 判断是否登录打印状态
*/
void isLogin(String str);
}

View File

@ -0,0 +1,72 @@
package com.pj.more;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.dev33.satoken.stp.StpUtil;
@RestController
public class TestController {
@DubboReference
private DemoService demoService;
// Consumer端登录状态传播到Provider端
@RequestMapping("test")
public String test() {
demoService.isLogin("----------- 登录前 ");
StpUtil.login(10001);
demoService.isLogin("----------- 登录后 ");
return "ok";
}
// Provider端登录状态回传到Consumer端
@RequestMapping("test2")
public String test2() {
System.out.println("----------- 登录前 ");
System.out.println("Token值" + StpUtil.getTokenValue());
System.out.println("是否登录:" + StpUtil.isLogin());
demoService.doLogin(10002);
System.out.println("----------- 登录后 ");
System.out.println("Token值" + StpUtil.getTokenValue());
System.out.println("是否登录:" + StpUtil.isLogin());
return "ok";
}
// Consumer端登录状态在Consumer端保持
@RequestMapping("test3")
public String test3() {
System.out.println("----------- 登录前 ");
System.out.println("Token值" + StpUtil.getTokenValue());
System.out.println("是否登录:" + StpUtil.isLogin());
StpUtil.login(10003);
demoService.isLogin("----------- Provider状态");
System.out.println("----------- 登录后 ");
System.out.println("Token值" + StpUtil.getTokenValue());
System.out.println("是否登录:" + StpUtil.isLogin());
return "ok";
}
// Provider端登录状态在Provider端保持
@RequestMapping("test4")
public String test4() {
// 登录
demoService.doLogin(10004);
// 打印一下
demoService.isLogin("----------- 会话信息 ");
return "ok";
}
}

View File

@ -0,0 +1,24 @@
server:
# 端口号
port: 8081
spring:
# redis配置
redis:
# Redis数据库索引默认为0
database: 0
# Redis服务器地址
host: 127.0.0.1
# Redis服务器连接端口
port: 6379
# Redis服务器连接密码默认为空
password:
# 连接超时时间
dubbo:
application:
# 服务名称
name: dubbo-consumer-demo
registry:
# 注册中心地址
address: nacos://127.0.0.1:8001

View File

@ -0,0 +1,13 @@
target/
.project
.classpath
.settings
/.idea/
node_modules/
bin/
.settings/
unpackage/
/.apt_generated/
/.apt_generated_tests/

View File

@ -0,0 +1,79 @@
<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>
<groupId>com.pj</groupId>
<artifactId>sa-token-demo-dubbo3-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- SpringBoot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<!--<version>2.3.1.RELEASE</version>-->
<version>2.5.15</version>
</parent>
<!-- 指定一些属性 -->
<properties>
<java.version>1.8</java.version>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
<sa-token.version>1.34.1</sa-token.version>
<dubbo.version>3.1.5</dubbo.version>
<nacos.version>2.1.2</nacos.version>
</properties>
<dependencies>
<!-- SpringBoot Web模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Sa-Token -->
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-spring-boot-starter</artifactId>
<version>${sa-token.version}</version>
</dependency>
<!-- Sa-Token整合 Redis (使用jackson序列化方式) -->
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-redis-jackson</artifactId>
<version>${sa-token.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<!-- Dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<!-- Dubbo 注册到 Nacos -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>${nacos.version}</version>
</dependency>
<!-- Sa-Token 整合 Dubbo -->
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-dubbo3</artifactId>
<version>${sa-token.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,22 @@
package com.pj;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Dubbo3 服务提供端
*
* @author click33
*
*/
@EnableDubbo
@SpringBootApplication
public class Dubbo3ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(Dubbo3ProviderApplication.class, args);
System.out.println("Dubbo3ProviderApplication 启动成功");
}
}

View File

@ -0,0 +1,16 @@
package com.pj.more;
public interface DemoService {
/**
* 登录
* @param loginId 账号id
*/
void doLogin(Object loginId);
/**
* 判断是否登录打印状态
*/
void isLogin(String str);
}

View File

@ -0,0 +1,23 @@
package com.pj.more;
import org.apache.dubbo.config.annotation.DubboService;
import cn.dev33.satoken.stp.StpUtil;
@DubboService()
public class DemoServiceImpl implements DemoService {
@Override
public void doLogin(Object loginId) {
StpUtil.login(loginId);
}
@Override
public void isLogin(String str) {
System.out.println(str);
System.out.println("Token值" + StpUtil.getTokenValue());
System.out.println("是否登录:" + StpUtil.isLogin());
}
}

View File

@ -0,0 +1,38 @@
server:
# 端口号
port: 8080
spring:
# redis配置
redis:
# Redis数据库索引默认为0
database: 0
# Redis服务器地址
host: 127.0.0.1
# Redis服务器连接端口
port: 6379
# Redis服务器连接密码默认为空
password:
# 连接超时时间
timeout: 10s
# Dubbo
dubbo:
# 服务名
application:
name: dubbo-provider-demo
# 扫描包
scan:
base-packages: com.pj
# 注册中心地址
registry:
address: nacos://127.0.0.1:8001
# 协议
protocol:
name: dubbo
port: 12345