-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #125 from scy251147/dev
add grpc demo components
- Loading branch information
Showing
35 changed files
with
1,732 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
joylive-demo/joylive-demo-grpc/joylive-demo-grpc-consumer/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<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/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>com.jd.live</groupId> | ||
<artifactId>joylive-demo-grpc</artifactId> | ||
<version>${revision}</version> | ||
</parent> | ||
|
||
<artifactId>joylive-demo-grpc-consumer</artifactId> | ||
|
||
<properties> | ||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version> | ||
<spring.cloud.version>Hoxton.SR1</spring.cloud.version> | ||
<grpc-all.version>1.29.0</grpc-all.version> | ||
<spring.cloud.alibaba.version>2.2.0.RELEASE</spring.cloud.alibaba.version> | ||
<grpc.spring.cloud.version>2.8.0.RELEASE</grpc.spring.cloud.version> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
</properties> | ||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>${spring.boot.version}</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-dependencies</artifactId> | ||
<version>${spring.cloud.version}</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.alibaba.cloud</groupId> | ||
<artifactId>spring-cloud-alibaba-dependencies</artifactId> | ||
<version>${spring.cloud.alibaba.version}</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<dependencies> | ||
<!-- 引入 API 项目 --> | ||
<dependency> | ||
<groupId>com.jd.live</groupId> | ||
<artifactId>joylive-demo-grpc-service-api</artifactId> | ||
<version>${revision}</version> | ||
</dependency> | ||
|
||
<!-- 实现对 SpringMVC 的自动化配置 --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<!-- 引入 gRPC Client Starter 依赖,实现对 gRPC 的自动配置 --> | ||
<dependency> | ||
<groupId>net.devh</groupId> | ||
<artifactId>grpc-client-spring-boot-starter</artifactId> | ||
<version>${grpc.spring.cloud.version}</version> | ||
</dependency> | ||
|
||
<!-- 引入 Spring Cloud Alibaba Nacos Discovery 相关依赖,将 Nacos 作为注册中心,并实现对其的自动配置 --> | ||
<dependency> | ||
<groupId>com.alibaba.cloud</groupId> | ||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<directory>${basedir}/target</directory> | ||
<finalName>joylive-demo-grpc-consumer</finalName> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
<configuration> | ||
<mainClass>com.jd.live.agent.demo.grpc.consumer.GRpcConsumerApplication</mainClass> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
13 changes: 13 additions & 0 deletions
13
...-consumer/src/main/java/com/jd/live/agent/demo/grpc/consumer/GRpcConsumerApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.jd.live.agent.demo.grpc.consumer; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class GRpcConsumerApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(GRpcConsumerApplication.class, args); | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
...pc-consumer/src/main/java/com/jd/live/agent/demo/grpc/consumer/UserServiceController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.jd.live.agent.demo.grpc.consumer; | ||
|
||
import com.jd.live.agent.demo.grpc.service.api.*; | ||
import net.devh.boot.grpc.client.inject.GrpcClient; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/demo") | ||
public class UserServiceController { | ||
|
||
@GrpcClient("user-provider") | ||
private UserServiceGrpc.UserServiceBlockingStub userServiceGrpc; | ||
|
||
@GetMapping("/get") | ||
public String get(@RequestParam("id") Integer id) { | ||
UserGetRequest request = UserGetRequest.newBuilder().setId(id).build(); | ||
UserGetResponse response = userServiceGrpc.get(request); | ||
return response.getName(); | ||
} | ||
|
||
@GetMapping("/create") // 为了方便测试,实际使用 @PostMapping | ||
public Integer create(@RequestParam("name") String name, | ||
@RequestParam("gender") Integer gender) { | ||
UserCreateRequest request = UserCreateRequest.newBuilder() | ||
.setName(name) | ||
.setGender(gender) | ||
.build(); | ||
UserCreateResponse response = userServiceGrpc.create(request); | ||
return response.getId(); | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
joylive-demo/joylive-demo-grpc/joylive-demo-grpc-consumer/src/main/resources/application.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
spring: | ||
application: | ||
name: user-consumer # 应用名 | ||
cloud: | ||
nacos: | ||
# Nacos 作为注册中心的配置项,对应 NacosDiscoveryProperties 配置类 | ||
discovery: | ||
server-addr: 127.0.0.1:8848 # Nacos 服务器地址 | ||
|
||
grpc: | ||
# gRPC 客户端配置,对应 GrpcChannelsProperties 配置类的映射 | ||
client: | ||
user-provider: | ||
#服务发现,注意本地host配置 127.0.0.1 user-provider | ||
address: 'discovery:///user-provider' | ||
#静态地址 | ||
#address: 'static://127.0.0.1:9898' | ||
enableKeepAlive: true | ||
keepAliveWithoutCalls: true | ||
negotiationType: plaintext |
10 changes: 10 additions & 0 deletions
10
...ve-demo/joylive-demo-grpc/joylive-demo-grpc-consumer/src/main/resources/nacos-logback.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<configuration debug="false" scan="false"> | ||
<!-- Console log output --> | ||
<appender name="console" class="ch.qos.logback.core.ConsoleAppender"/> | ||
|
||
<!-- Level: TRACE < DEBUG < INFO < WARN < ERROR < FATAL --> | ||
<root level="info"> | ||
<appender-ref ref="console"/> | ||
</root> | ||
</configuration> |
97 changes: 97 additions & 0 deletions
97
joylive-demo/joylive-demo-grpc/joylive-demo-grpc-provider/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<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/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>com.jd.live</groupId> | ||
<artifactId>joylive-demo-grpc</artifactId> | ||
<version>${revision}</version> | ||
</parent> | ||
|
||
<artifactId>joylive-demo-grpc-provider</artifactId> | ||
|
||
<properties> | ||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version> | ||
<spring.cloud.version>Hoxton.SR1</spring.cloud.version> | ||
<grpc-all.version>1.29.0</grpc-all.version> | ||
<spring.cloud.alibaba.version>2.2.0.RELEASE</spring.cloud.alibaba.version> | ||
<grpc.spring.cloud.version>2.8.0.RELEASE</grpc.spring.cloud.version> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
</properties> | ||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>${spring.boot.version}</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-dependencies</artifactId> | ||
<version>${spring.cloud.version}</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.alibaba.cloud</groupId> | ||
<artifactId>spring-cloud-alibaba-dependencies</artifactId> | ||
<version>${spring.cloud.alibaba.version}</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<dependencies> | ||
<!-- 引入 API 项目 --> | ||
<dependency> | ||
<groupId>com.jd.live</groupId> | ||
<artifactId>joylive-demo-grpc-service-api</artifactId> | ||
<version>${revision}</version> | ||
</dependency> | ||
|
||
<!-- 引入 gRPC --> | ||
<dependency> | ||
<groupId>io.grpc</groupId> | ||
<artifactId>grpc-all</artifactId> | ||
<version>${grpc-all.version}</version> | ||
</dependency> | ||
|
||
<!-- 实现对 SpringMVC 的自动化配置 --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<!-- 引入 gRPC Server Starter 依赖,实现对 gRPC 的自动配置 --> | ||
<dependency> | ||
<groupId>net.devh</groupId> | ||
<artifactId>grpc-server-spring-boot-starter</artifactId> | ||
<version>${grpc.spring.cloud.version}</version> | ||
</dependency> | ||
|
||
<!-- 引入 Spring Cloud Alibaba Nacos Discovery 相关依赖,将 Nacos 作为注册中心,并实现对其的自动配置 --> | ||
<dependency> | ||
<groupId>com.alibaba.cloud</groupId> | ||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<directory>${basedir}/target</directory> | ||
<finalName>joylive-demo-grpc-provider</finalName> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
<configuration> | ||
<mainClass>com.jd.live.agent.demo.grpc.provider.GRpcProviderApplication</mainClass> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
13 changes: 13 additions & 0 deletions
13
...-provider/src/main/java/com/jd/live/agent/demo/grpc/provider/GRpcProviderApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.jd.live.agent.demo.grpc.provider; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class GRpcProviderApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(GRpcProviderApplication.class, args); | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
...grpc-provider/src/main/java/com/jd/live/agent/demo/grpc/provider/UserServiceGrpcImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.jd.live.agent.demo.grpc.provider; | ||
|
||
import com.jd.live.agent.demo.grpc.service.api.*; | ||
import io.grpc.stub.StreamObserver; | ||
import net.devh.boot.grpc.server.service.GrpcService; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
@GrpcService | ||
public class UserServiceGrpcImpl extends UserServiceGrpc.UserServiceImplBase { | ||
|
||
private Logger logger = LoggerFactory.getLogger(getClass()); | ||
|
||
@Override | ||
public void get(UserGetRequest request, StreamObserver<UserGetResponse> responseObserver) { | ||
UserGetResponse.Builder builder = UserGetResponse.newBuilder(); | ||
builder.setId(request.getId()) | ||
.setName("index :" + request.getId() + " time : " + System.currentTimeMillis()) | ||
.setGender(request.getId() % 2 + 1); | ||
responseObserver.onNext(builder.build()); | ||
responseObserver.onCompleted(); | ||
} | ||
|
||
@Override | ||
public void create(UserCreateRequest request, StreamObserver<UserCreateResponse> responseObserver) { | ||
UserCreateResponse.Builder builder = UserCreateResponse.newBuilder(); | ||
builder.setId((int) (System.currentTimeMillis() / 1000)); | ||
responseObserver.onNext(builder.build()); | ||
responseObserver.onCompleted(); | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
joylive-demo/joylive-demo-grpc/joylive-demo-grpc-provider/src/main/resources/application.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
spring: | ||
application: | ||
name: user-provider # 应用名 | ||
cloud: | ||
nacos: | ||
# Nacos 作为注册中心的配置项,对应 NacosDiscoveryProperties 配置类 | ||
discovery: | ||
server-addr: 127.0.0.1:8848 # Nacos 服务器地址 | ||
|
||
grpc: | ||
# gRPC 服务器配置,对应 GrpcServerProperties 配置类 | ||
server: | ||
port: 9898 # gRPC Server 随机端口 | ||
|
||
server: | ||
port: 9899 # Web Server 随机端口 |
10 changes: 10 additions & 0 deletions
10
...ve-demo/joylive-demo-grpc/joylive-demo-grpc-provider/src/main/resources/nacos-logback.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<configuration debug="false" scan="false"> | ||
<!-- Console log output --> | ||
<appender name="console" class="ch.qos.logback.core.ConsoleAppender"/> | ||
|
||
<!-- Level: TRACE < DEBUG < INFO < WARN < ERROR < FATAL --> | ||
<root level="info"> | ||
<appender-ref ref="console"/> | ||
</root> | ||
</configuration> |
Oops, something went wrong.