添加grpc版二级上下文

This commit is contained in:
lym
2022-08-26 16:16:05 +08:00
parent c7968826b6
commit fb43bd81c9
27 changed files with 945 additions and 0 deletions

View File

@@ -0,0 +1,20 @@
<?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>sa-token-demo-grpc</artifactId>
<groupId>com.lym</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>client</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

View File

@@ -0,0 +1,18 @@
package com.lym;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @author lym
* @description
* @date 2022/8/26 10:58
**/
@SpringBootApplication
@EnableDiscoveryClient
public class Client {
public static void main(String[] args) {
SpringApplication.run(Client.class);
}
}

View File

@@ -0,0 +1,48 @@
package com.lym.controller;
import cn.dev33.satoken.stp.StpUtil;
import com.lym.grpc.client.GrpcAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author lym
* @description
* @date 2022/8/26 11:01
**/
@RestController
public class TestController {
@Autowired
private GrpcAuthService grpcAuthService;
// 客户端登录,状态带到服务端。
@RequestMapping("test")
public void test() {
System.out.println("登录前:" + grpcAuthService.isLogin());
System.out.println("登录前:" + StpUtil.isLogin());
StpUtil.login(1);
System.out.println("登录后:" + grpcAuthService.isLogin());
System.out.println("登录后:" + StpUtil.getTokenValue());
System.out.println("登录后:" + StpUtil.getLoginId());
}
// 服务端登录,登录状态带回客户端
@RequestMapping("test2")
public String test2() {
System.out.println("登录前:" + grpcAuthService.isLogin());
System.out.println("登录前:" + StpUtil.isLogin());
String token = grpcAuthService.login(3);
System.out.println("登录后:" + grpcAuthService.isLogin());
System.out.println("登录后:" + StpUtil.getTokenValue());
System.out.println("登录后:" + StpUtil.getLoginId());
assert StpUtil.getTokenValue().equals(token);
return token;
}
}

View File

@@ -0,0 +1,28 @@
package com.lym.grpc.client;
import com.google.protobuf.Empty;
import com.lym.grpc.auth.Auth;
import com.lym.grpc.auth.AuthServiceGrpc;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Service;
/**
* @author lym
* @description
* @date 2022/8/26 11:02
**/
@Service
public class GrpcAuthService {
@GrpcClient("test-server")
private AuthServiceGrpc.AuthServiceBlockingStub grpcAuthService;
public boolean isLogin() {
Auth.GrpcBool resp = grpcAuthService.isLogin(Empty.getDefaultInstance());
return resp.getVal();
}
public String login(Integer id) {
Auth.GrpcString resp = grpcAuthService.login(Auth.GrpcInt.newBuilder().setVal(id).build());
return resp.getVal();
}
}

View File

@@ -0,0 +1,24 @@
syntax = "proto3";
package auth;
option java_package = "com.lym.grpc.auth";
import "google/protobuf/empty.proto";
message GrpcBool{
bool val = 1;
}
message GrpcInt{
int32 val = 1;
}
message GrpcString{
string val = 1;
}
service AuthService{
rpc isLogin(google.protobuf.Empty) returns (GrpcBool);
rpc login(GrpcInt) returns (GrpcString);
}

View File

@@ -0,0 +1,18 @@
server:
port: 2222
spring:
application:
name: test-client
redis:
host: localhost
cloud:
nacos:
server-addr: localhost:8848
sa-token:
is-read-cookie: false
grpc:
server:
port: 2223
client:
test-server:
negotiation-type: PLAINTEXT

View File

@@ -0,0 +1,122 @@
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<modules>
<module>client</module>
<module>server</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath/>
</parent>
<groupId>com.lym</groupId>
<artifactId>sa-token-demo-grpc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sa-token-demo-grpc</name>
<description>sa-token-demo-grpc</description>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<lombok.version>1.18.10</lombok.version>
<sa-token-version>1.30.0</sa-token-version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-spring-boot-starter</artifactId>
<version>${sa-token-version}</version>
</dependency>
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-dao-redis-jackson</artifactId>
<version>${sa-token-version}</version>
</dependency>
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-context-grpc</artifactId>
<version>${sa-token-version}</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2021.0.1.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2021.0.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.1.0:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.12.0:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,20 @@
<?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>sa-token-demo-grpc</artifactId>
<groupId>com.lym</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>server</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

View File

@@ -0,0 +1,18 @@
package com.lym;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @author lym
* @description
* @date 2022/8/26 10:58
**/
@SpringBootApplication
@EnableDiscoveryClient
public class Server {
public static void main(String[] args) {
SpringApplication.run(Server.class);
}
}

View File

@@ -0,0 +1,37 @@
package com.lym.grpc.server;
import cn.dev33.satoken.stp.StpUtil;
import com.google.protobuf.Empty;
import com.lym.grpc.auth.Auth;
import com.lym.grpc.auth.AuthServiceGrpc;
import com.lym.service.AuthService;
import io.grpc.stub.StreamObserver;
import net.devh.boot.grpc.server.service.GrpcService;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @author lym
* @description
* @date 2022/8/26 11:29
**/
@GrpcService
public class GrpcAuthService extends AuthServiceGrpc.AuthServiceImplBase {
@Autowired
private AuthService authService;
@Override
public void isLogin(Empty request, StreamObserver<Auth.GrpcBool> responseObserver) {
boolean isLogin = authService.isLogin();
responseObserver.onNext(Auth.GrpcBool.newBuilder().setVal(isLogin).build());
responseObserver.onCompleted();
}
@Override
public void login(Auth.GrpcInt request, StreamObserver<Auth.GrpcString> responseObserver) {
StpUtil.login(request.getVal());
Auth.GrpcString resp = Auth.GrpcString.newBuilder().setVal(StpUtil.getTokenValue()).build();
responseObserver.onNext(resp);
responseObserver.onCompleted();
}
}

View File

@@ -0,0 +1,23 @@
package com.lym.service;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.stereotype.Service;
/**
* @author lym
* @description
* @date 2022/8/26 11:30
**/
@Service
public class AuthService {
public boolean isLogin() {
if (StpUtil.isLogin()) {
System.out.println("id:" + StpUtil.getLoginIdAsInt());
System.out.println("token" + StpUtil.getTokenValue());
} else {
System.out.println("未登录");
}
return StpUtil.isLogin();
}
}

View File

@@ -0,0 +1,24 @@
syntax = "proto3";
package auth;
option java_package = "com.lym.grpc.auth";
import "google/protobuf/empty.proto";
message GrpcBool{
bool val = 1;
}
message GrpcInt{
int32 val = 1;
}
message GrpcString{
string val = 1;
}
service AuthService{
rpc isLogin(google.protobuf.Empty) returns (GrpcBool);
rpc login(GrpcInt) returns (GrpcString);
}

View File

@@ -0,0 +1,16 @@
server:
port: 5555
spring:
application:
name: test-server
redis:
host: localhost
cloud:
nacos:
server-addr: localhost:8848
sa-token:
is-read-cookie: false
grpc:
server:
port: 5556