Skip to content

Commit 81b6ceb

Browse files
committed
feat: samples
1 parent 15b2bf6 commit 81b6ceb

30 files changed

+861
-1
lines changed

pom.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
<module>spring-server-sent-events</module>
3737
<module>spring-testcontainers-support</module>
3838
<module>spring-zookeeper</module>
39+
<module>spring-restful-version</module>
40+
<module>spring-grpc</module>
3941
</modules>
4042

4143
<properties>
@@ -132,6 +134,30 @@
132134
<type>pom</type>
133135
<scope>import</scope>
134136
</dependency>
137+
138+
<dependency>
139+
<groupId>com.google.protobuf</groupId>
140+
<artifactId>protobuf-bom</artifactId>
141+
<version>4.32.0</version>
142+
<type>pom</type>
143+
<scope>import</scope>
144+
</dependency>
145+
146+
<dependency>
147+
<groupId>io.grpc</groupId>
148+
<artifactId>grpc-bom</artifactId>
149+
<version>1.75.0</version>
150+
<type>pom</type>
151+
<scope>import</scope>
152+
</dependency>
153+
154+
<dependency>
155+
<groupId>org.springframework.grpc</groupId>
156+
<artifactId>spring-grpc-dependencies</artifactId>
157+
<version>0.10.0</version>
158+
<type>pom</type>
159+
<scope>import</scope>
160+
</dependency>
135161
</dependencies>
136162
</dependencyManagement>
137163

spring-grpc/pom.xml

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.livk</groupId>
8+
<artifactId>spring-boot-extension-samples</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>spring-grpc</artifactId>
13+
14+
<properties>
15+
<maven.compiler.source>21</maven.compiler.source>
16+
<maven.compiler.target>21</maven.compiler.target>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
</properties>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>com.google.protobuf</groupId>
23+
<artifactId>protobuf-java</artifactId>
24+
</dependency>
25+
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-webflux</artifactId>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-data-jpa</artifactId>
34+
</dependency>
35+
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-aop</artifactId>
39+
</dependency>
40+
41+
<dependency>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-starter-validation</artifactId>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>io.github.livk-cloud</groupId>
48+
<artifactId>mapstruct-spring-boot-starter</artifactId>
49+
</dependency>
50+
51+
<dependency>
52+
<groupId>org.mapstruct</groupId>
53+
<artifactId>mapstruct-processor</artifactId>
54+
<scope>provided</scope>
55+
</dependency>
56+
57+
<dependency>
58+
<groupId>org.projectlombok</groupId>
59+
<artifactId>lombok-mapstruct-binding</artifactId>
60+
<scope>provided</scope>
61+
</dependency>
62+
63+
<dependency>
64+
<groupId>com.h2database</groupId>
65+
<artifactId>h2</artifactId>
66+
</dependency>
67+
68+
<dependency>
69+
<groupId>org.springframework.grpc</groupId>
70+
<artifactId>spring-grpc-spring-boot-starter</artifactId>
71+
</dependency>
72+
73+
<dependency>
74+
<groupId>io.grpc</groupId>
75+
<artifactId>grpc-services</artifactId>
76+
</dependency>
77+
78+
<dependency>
79+
<groupId>io.grpc</groupId>
80+
<artifactId>grpc-netty</artifactId>
81+
</dependency>
82+
83+
<dependency>
84+
<groupId>org.springframework.grpc</groupId>
85+
<artifactId>spring-grpc-test</artifactId>
86+
<scope>test</scope>
87+
</dependency>
88+
</dependencies>
89+
90+
<build>
91+
<plugins>
92+
<plugin>
93+
<groupId>io.github.ascopes</groupId>
94+
<artifactId>protobuf-maven-plugin</artifactId>
95+
<version>3.8.0</version>
96+
<configuration>
97+
<protocVersion>4.32.0</protocVersion>
98+
<binaryMavenPlugins>
99+
<binaryMavenPlugin>
100+
<groupId>io.grpc</groupId>
101+
<artifactId>protoc-gen-grpc-java</artifactId>
102+
<version>1.75.0</version>
103+
</binaryMavenPlugin>
104+
</binaryMavenPlugins>
105+
</configuration>
106+
107+
<executions>
108+
<execution>
109+
<goals>
110+
<goal>generate</goal>
111+
</goals>
112+
</execution>
113+
</executions>
114+
</plugin>
115+
116+
<plugin>
117+
<groupId>org.apache.maven.plugins</groupId>
118+
<artifactId>maven-compiler-plugin</artifactId>
119+
<version>3.14.0</version>
120+
<configuration>
121+
<!-- Java 版本 -->
122+
<source>${maven.compiler.source}</source>
123+
<target>${maven.compiler.target}</target>
124+
<parameters>true</parameters>
125+
<!-- 添加参数 -->
126+
<compilerArgs>
127+
<arg>-parameters</arg>
128+
<arg>-Xlint:unchecked</arg>
129+
<arg>-Xlint:-options</arg>
130+
<arg>-Xlint:varargs</arg>
131+
<arg>-Xlint:rawtypes</arg>
132+
<arg>-Xlint:deprecation</arg>
133+
</compilerArgs>
134+
<annotationProcessorPaths>
135+
<path>
136+
<groupId>org.projectlombok</groupId>
137+
<artifactId>lombok</artifactId>
138+
</path>
139+
<path>
140+
<groupId>io.github.livk-cloud</groupId>
141+
<artifactId>spring-auto-service</artifactId>
142+
</path>
143+
<path>
144+
<groupId>org.mapstruct</groupId>
145+
<artifactId>mapstruct-processor</artifactId>
146+
</path>
147+
<path>
148+
<groupId>org.projectlombok</groupId>
149+
<artifactId>lombok-mapstruct-binding</artifactId>
150+
</path>
151+
</annotationProcessorPaths>
152+
</configuration>
153+
</plugin>
154+
</plugins>
155+
</build>
156+
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.livk.grpc;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.scheduling.annotation.EnableScheduling;
7+
8+
/**
9+
* @author livk
10+
*/
11+
@Slf4j
12+
@EnableScheduling
13+
@SpringBootApplication
14+
public class GrpcApp {
15+
16+
public static void main(String[] args) {
17+
SpringApplication.run(GrpcApp.class, args);
18+
}
19+
20+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.livk.grpc.client;
2+
3+
import com.livk.grpc.proto.DeviceServiceGrpc;
4+
import com.livk.grpc.proto.entity.ProtoDevice;
5+
import jakarta.validation.Valid;
6+
import lombok.RequiredArgsConstructor;
7+
import lombok.extern.slf4j.Slf4j;
8+
import org.springframework.core.convert.ConversionService;
9+
import org.springframework.http.HttpEntity;
10+
import org.springframework.http.ResponseEntity;
11+
import org.springframework.web.bind.annotation.GetMapping;
12+
import org.springframework.web.bind.annotation.PostMapping;
13+
import org.springframework.web.bind.annotation.RequestBody;
14+
import org.springframework.web.bind.annotation.RequestMapping;
15+
import org.springframework.web.bind.annotation.RequestParam;
16+
import org.springframework.web.bind.annotation.RestController;
17+
18+
/**
19+
* @author livk
20+
*/
21+
@Slf4j
22+
@RestController
23+
@RequestMapping("/grpc/device")
24+
@RequiredArgsConstructor
25+
public class DeviceController {
26+
27+
private final DeviceServiceGrpc.DeviceServiceBlockingStub blockingStub;
28+
29+
private final ConversionService conversionService;
30+
31+
@GetMapping
32+
public HttpEntity<DeviceVO> query(@RequestParam String name) {
33+
log.info("query param:{}", name);
34+
var query = ProtoDevice.DeviceQuery.newBuilder().setName(name).build();
35+
var device = blockingStub.query(query);
36+
return ResponseEntity.ok(conversionService.convert(device, DeviceVO.class));
37+
}
38+
39+
@PostMapping
40+
public HttpEntity<Boolean> add(@RequestBody @Valid DeviceDTO dto) {
41+
log.info("{}", dto);
42+
var device = conversionService.convert(dto, ProtoDevice.Device.class);
43+
var result = blockingStub.add(device).getValue();
44+
return ResponseEntity.ok(result);
45+
}
46+
47+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.livk.grpc.client;
2+
3+
import jakarta.validation.constraints.NotBlank;
4+
import lombok.Data;
5+
6+
/**
7+
* @author livk
8+
*/
9+
@Data
10+
public class DeviceDTO {
11+
12+
@NotBlank(message = "name is required")
13+
private String name;
14+
15+
@NotBlank(message = "mac is required")
16+
private String mac;
17+
18+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.livk.grpc.client;
2+
3+
import com.livk.grpc.proto.entity.ProtoDevice;
4+
import org.mapstruct.Mapper;
5+
import org.mapstruct.MappingConstants;
6+
import org.springframework.core.convert.converter.Converter;
7+
8+
/**
9+
* @author livk
10+
*/
11+
@Mapper(componentModel = MappingConstants.ComponentModel.SPRING)
12+
public interface DeviceDTOConverter extends Converter<DeviceDTO, ProtoDevice.Device> {
13+
14+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.livk.grpc.client;
2+
3+
import lombok.Builder;
4+
import lombok.Data;
5+
6+
/**
7+
* @author livk
8+
*/
9+
@Data
10+
@Builder
11+
public class DeviceVO {
12+
13+
private String name;
14+
15+
private String mac;
16+
17+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.livk.grpc.client;
2+
3+
import com.livk.grpc.proto.entity.ProtoDevice;
4+
import org.mapstruct.Mapper;
5+
import org.mapstruct.MappingConstants;
6+
import org.springframework.core.convert.converter.Converter;
7+
8+
/**
9+
* @author livk
10+
*/
11+
@Mapper(componentModel = MappingConstants.ComponentModel.SPRING)
12+
public interface DeviceVOConverter extends Converter<ProtoDevice.Device, DeviceVO> {
13+
14+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.livk.grpc.server;
2+
3+
import jakarta.persistence.Entity;
4+
import jakarta.persistence.GeneratedValue;
5+
import jakarta.persistence.GenerationType;
6+
import jakarta.persistence.Id;
7+
import jakarta.persistence.Table;
8+
import lombok.Data;
9+
10+
/**
11+
* @author livk
12+
*/
13+
@Data
14+
@Entity
15+
@Table(name = "device_info")
16+
public class Device {
17+
18+
@Id
19+
@GeneratedValue(strategy = GenerationType.IDENTITY)
20+
private Long id;
21+
22+
private String name;
23+
24+
private String mac;
25+
26+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.livk.grpc.server;
2+
3+
import com.livk.context.mapstruct.converter.Converter;
4+
import com.livk.grpc.proto.entity.ProtoDevice;
5+
import org.mapstruct.Mapper;
6+
import org.mapstruct.MappingConstants;
7+
8+
/**
9+
* @author livk
10+
*/
11+
@Mapper(componentModel = MappingConstants.ComponentModel.SPRING)
12+
public interface DeviceConverter extends Converter<Device, ProtoDevice.Device> {
13+
14+
}

0 commit comments

Comments
 (0)