Skip to content

Commit

Permalink
Merge pull request #125 from scy251147/dev
Browse files Browse the repository at this point in the history
add grpc demo components
  • Loading branch information
hexiaofeng authored Oct 30, 2024
2 parents 8a49f8f + 746c2a4 commit 7b8a11e
Show file tree
Hide file tree
Showing 35 changed files with 1,732 additions and 0 deletions.
5 changes: 5 additions & 0 deletions joylive-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,11 @@
<artifactId>joylive-router-sofarpc</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>com.jd.live</groupId>
<artifactId>joylive-router-grpc</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>com.jd.live</groupId>
<artifactId>joylive-router-rocketmq5</artifactId>
Expand Down
90 changes: 90 additions & 0 deletions joylive-demo/joylive-demo-grpc/joylive-demo-grpc-consumer/pom.xml
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>
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);
}

}
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();
}

}
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
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 joylive-demo/joylive-demo-grpc/joylive-demo-grpc-provider/pom.xml
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>
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);
}

}
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();
}

}
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 随机端口
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>
Loading

0 comments on commit 7b8a11e

Please sign in to comment.