Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions developer/back-end/liuliang20200312/des_sample.md

This file was deleted.

Binary file not shown.
25 changes: 25 additions & 0 deletions developer/back-end/wen20200323/Java开发第三期作业.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Java开发第三期作业

## 目的

- 学会使用restful的风格开发接口

- 了解RPC的基本概念及思想,熟悉gRPC的开发流程

## 内容

### 改造以前的作业为Restful风格形式

- 名词,尽量不出现动词

- 小写,以短横线‘-’连接

- get/post/put/delete方法的使用

### 完成一个gRPC服务器和客户端的搭建,熟悉proto文件格式

- Web

- Client

- Proto
Binary file not shown.
114 changes: 114 additions & 0 deletions developer/back-end/wen20200323/yuanmao/grpc-demo/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?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">
<modelVersion>4.0.0</modelVersion>

<groupId>com.reuben</groupId>
<artifactId>grpc-demo</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<grpc.version>1.6.1</grpc.version>
<jdk.version>1.8</jdk.version>
<protoc.version>3.3.0</protoc.version>
<kr.motd.version>1.5.0.Final</kr.motd.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<!--grpc工程依赖-->
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>

<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>${kr.motd.version}</version>
</extension>
</extensions>

<plugins>
<!-- protobuf插件 -->
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.0</version>
<!--在命令行直接编译即可-->
<configuration>
<protocArtifact>com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}
</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}
</pluginArtifact>
<!--protobuf文件路径-->
<protoSourceRoot>src/main/proto</protoSourceRoot>
<!--protoc可执行文件据对路径-->
<!-- <protocExecutable>D:/Users/Circle_clearly/Downloads/protoc-3.11.4-win64/bin/protoc</protocExecutable>-->
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>

<!--jdk插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
<version>3.8.1</version>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.reuben.rpc.client;

import com.reuben.grpc.service.AccountGrpc;
import com.sun.javafx.binding.StringFormatter;
import com.reuben.grpc.entity.Account;
import com.reuben.grpc.entity.AccountResponse;
import lombok.extern.slf4j.Slf4j;


import java.util.List;

/**
* @program: grpc-demo
* @description:
* @author: reuben
* @create: 2020-03-24 11:31
**/
@Slf4j
public class AccountClient {

private final AccountGrpc.AccountBlockingStub accountBlockingStub;

private final BaseClient client;

public AccountClient(BaseClient client) {
this.client = client;
this.accountBlockingStub = client.getAccountBlockingStub();
}

public void addAccount(String name, String sex, int age) {

Account account = Account.getDefaultInstance().toBuilder()
.setName(name)
.setSex(sex)
.setAge(age)
.build();

AccountResponse response = this.accountBlockingStub.addAccount(account);

//System.out.println(StringFormatter.format("返回消息:%s\n状态:%d", response.getMsg(), response.getCode()).get());
log.info(StringFormatter.format("返回消息:%s\n状态:%d", response.getMsg(), response.getCode()).get());

}

public void queryAccount(String name) {
Account account = Account.getDefaultInstance().toBuilder()
.setName(name).build();
AccountResponse response = this.accountBlockingStub.getAccountByName(account);

//System.out.println(StringFormatter.format("返回消息:%s\n状态:%d", response.getMsg(), response.getCode()).getValue());
log.info(StringFormatter.format("返回消息:%s\n状态:%d", response.getMsg(), response.getCode()).getValue());
//System.out.println("查询结果:");
log.info("查询结果:");
List<Account> list = response.getResultsList();
for (Account acc : list) {
log.info(StringFormatter.format("姓名:%s,性别:%s,年龄:%d", acc.getName(), acc.getSex(), acc.getAge()).get());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.reuben.rpc.client;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import com.reuben.grpc.service.AccountGrpc;
import com.reuben.grpc.service.GreeterGrpc;

import java.util.concurrent.TimeUnit;

/**
* @program: grpc-demo
* @description:
* @author: reuben
* @create: 2020-03-24 11:31
**/
public class BaseClient {

private final ManagedChannel channel;
private final GreeterGrpc.GreeterBlockingStub greeterBlockingStub;
private final AccountGrpc.AccountBlockingStub accountBlockingStub;

private BaseClient(ManagedChannel channel) {
this.channel = channel;
this.greeterBlockingStub = GreeterGrpc.newBlockingStub(channel);
this.accountBlockingStub = AccountGrpc.newBlockingStub(channel);
}

/**
* 构造客户端与Greeter 服务端连接 {@code host:port}
*
* @param host 主机地址
* @param port 端口
*/
public BaseClient(String host, int port) {
this(ManagedChannelBuilder.forAddress(host, port)
// Channels are secure by default (via SSL/TLS). For the example we disable TLS to avoid
// needing certificates.
.usePlaintext(true)
.build());
}

/**
* 关闭函数
*
* @throws InterruptedException
*/
public void shutdown() throws InterruptedException {
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
}

public GreeterGrpc.GreeterBlockingStub getGreeterBlockingStub() {
return greeterBlockingStub;
}

public AccountGrpc.AccountBlockingStub getAccountBlockingStub() {
return accountBlockingStub;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.reuben.rpc.client;

import io.grpc.StatusRuntimeException;
import com.reuben.grpc.entity.HelloReply;
import com.reuben.grpc.entity.HelloRequest;
import com.reuben.grpc.service.GreeterGrpc;
import lombok.extern.slf4j.Slf4j;

/**
* @program: grpc-demo
* @description:
* @author: reuben
* @create: 2020-03-24 11:31
**/
@Slf4j
public class GreeterClient {

private final GreeterGrpc.GreeterBlockingStub blockingStub;


public GreeterClient(BaseClient client) {
blockingStub = client.getGreeterBlockingStub();
}

public void greet(String name) {
HelloRequest request = HelloRequest.newBuilder().setName(name).build();
HelloReply response;
try {
response = blockingStub.sayHello(request);
String msg = response.getMessage();
//接收到服务端返回的消息
log.info("客户端收到消息:" + msg);
} catch (StatusRuntimeException e) {
return;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.reuben.rpc.service;

import com.reuben.rpc.service.account.AccountImpl;
import com.reuben.rpc.service.geeter.GreeterImpl;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

/**
* @program: grpc-demo
* @description:
* @author: reuben
* @create: 2020-03-24 11:32
**/
@Slf4j
public class GrpcDemo {

private Server server;

/**
* 服务启动类
* @param port 端口
* @throws IOException
*/
private void start(int port) throws IOException {
server = ServerBuilder.forPort(port)
//注册服务
.addService(new GreeterImpl())
.addService(new AccountImpl())
.build()
.start();

Runtime.getRuntime().addShutdownHook(new Thread(() -> {
log.error("*** JVM 关闭,导致gRPC服务关闭!");
GrpcDemo.this.stop();
log.error("*** 服务关闭");
}));
}

/**
* RPC 服务关闭
*/
private void stop() {
if (server != null) {
server.shutdown();
}
}

/**
* 设置守护进程
*/
private void blockUntilShutdown() throws InterruptedException {
if (server != null) {
server.awaitTermination();
}
}

/**
* RPC服务启动main函数
*/
public static void main(String[] args) throws IOException, InterruptedException {
final GrpcDemo server = new GrpcDemo();
server.start(50051);
server.blockUntilShutdown();
}
}
Loading