spring cloud集成grpc(Eureka版本)
这个demo的本质是先搭建一套简单的微服务,然后在注册的服务中引入grpc。
说明:使用的Spring Cloud版本不新,是Eureka支持的版本。
-
项目工程规划
目录结果如下图:
- 父工程导入依赖
在父工程的pom.xml中导入公共依赖,包括grpc框架、spring boot。
<?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>org.example</groupId>
<artifactId>cloud-grpc</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>cloud-eureka-server</module>
<module>grpc-lib</module>
<module>cloud-grpc-server</module>
<module>cloud-grpc-client</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<lombok.version>1.18.0</lombok.version>
<grpc.version>1.14.0</grpc.version>
<protobuf.version>3.5.1</protobuf.version>
<netty.version>4.1.17.Final</netty.version>
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
<spring-boot.version>2.0.4.RELEASE</spring-boot.version>
<spring-cloud.consul.version>2.0.1.RELEASE</spring-cloud.consul.version>
<spring-cloud.eureka.version>2.0.1.RELEASE</spring-cloud.eureka.version>
<spring-cloud.sleuth.version>2.0.1.RELEASE</spring-cloud.sleuth.version>
<brave.instrumentation.grpc>5.1.2</brave.instrumentation.grpc>
<grpc.server.version>2.0.1.RELEASE</grpc.server.version>
<grpc-server-spring-boot-starter-version>2.10.1.RELEASE</grpc-server-spring-boot-starter-version>
</properties>
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.1</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>net.devh</groupId>
<artifactId>grpc-client-spring-boot-starter</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>net.devh</groupId>
<artifactId>grpc-server-spring-boot-starter</artifactId>
<version>2.0.1.RELEASE</version>
</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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>net.devh</groupId>
<artifactId>grpc-client-spring-boot-starter</artifactId>
<version>${grpc.server.version}</version>
</dependency>
<dependency>
<groupId>net.devh</groupId>
<artifactId>grpc-server-spring-boot-starter</artifactId>
<version>${grpc.server.version}</version>
</dependency>
<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>io.netty</groupId>
<artifactId>netty-common</artifactId>
<version>${netty.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
- cloud-eureka-server模块
引入Eureka相关依赖,配置服务注册,让后续服务能注册。
pom.xm内容如下:
<?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>cloud-grpc</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-eureka-server</artifactId>
<packaging>jar</packaging>
<name>cloud-eureka-server</name>
<description>demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<!--
<goal>repackage</goal>
-->
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
在resources
目录下创建application.yml文件,配置内容如下:
server:
port: 8761
spring:
application:
name: eureka-server
eureka:
instance:
hostname: localhost
prefer-ip-address: true
status-page-url-path: /actuator/info
health-check-url-path: /actuator/health
lease-expiration-duration-in-seconds: 30
lease-renewal-interval-in-seconds: 30
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
enable-self-preservation: false
endpoints:
shutdown:
enabled: true
创建SpringBoot的启动类,并使用@EnableEurekaServer
:
package org.example.grpc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
运行该类的main方法,在启动成功后,用浏览器防伪localhost:8761
。
- grpc-lib模块导入依赖和插件
在grpc-lib模块的pom.xml中导入如下依赖、插件:
<?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>cloud-grpc</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>grpc-lib</artifactId>
<packaging>jar</packaging>
<name>grpc-lib</name>
<description>demo project fro Spring Boot</description>
<properties>
<grpc.version>1.6.1</grpc.version>
<os.plugin.version>1.6.0</os.plugin.version>
<protobuf.plugin.version>0.5.1</protobuf.plugin.version>
<protoc.version>3.3.0</protoc.version>
</properties>
<dependencies>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
</dependency>
</dependencies>
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>${os.plugin.version}</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>${protobuf.plugin.version}</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>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
- grpc-lib中创建proto文件
在grpc-lib模块的src/main
目录下创建proto目录,用于存放后面编写的proto文件。在proto目录中创建helloworld.proto
文件,内容如下:
syntax = "proto3";
option java_multiple_files = true;
option java_package = "org.example.grpc.lib";
option java_outer_classname = "HelloWorldProto";
// The greeting service definition.
service Simple {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {
}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
- grpc-lib模块生成java文件
选择右边侧栏的maven->grpc-lib-Plugins->protobuf->protobuf:compiler,右键后选择run [grpc-lib]...
。 等待一会,在模块的target/
下检查是否有grpc-lib-1.0-SNAPSHOT.jar,供其他模块使用 - cloud-grpc-server声明、注册服务
cloud-grpc-server模块的pom.xml引入前面的grpc-lib、Eureka和grpc相关的依赖:
<?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>cloud-grpc</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-grpc-server</artifactId>
<packaging>jar</packaging>
<name>cloud-grpc-server</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<dependency>
<groupId>net.devh</groupId>
<artifactId>grpc-server-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-instrumentation-grpc</artifactId>
<version>${brave.instrumentation.grpc}</version>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>grpc-lib</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<!--
<goal>repackage</goal>
-->
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
在resources
目录下创建application.yml文件,用于配置服务注册,配置内容如下:
spring:
application:
name: cloud-grpc-server
sleuth:
sampler:
probability: 1
server:
port: 8081
grpc:
server:
port: 0
eureka:
instance:
prefer-ip-address: true
instanceId: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8761/eureka/
在src/main/java/
,创建grpc服务,并使用@GrpcService
注册:
@GrpcService(SimpleGrpc.class)
public class GrpcServerService extends SimpleGrpc.SimpleImplBase{
@Override
public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver){
HelloReply reply = HelloReply.newBuilder()
.setMessage("Hello ========== " + req.getName())
.build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
}
}
在src/main/java/
下创建SpringBoot启动的Application,并使用Eureka相关的注解:
package org.example.cloudgrpcserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@EnableDiscoveryClient
@SpringBootApplication
public class CloudGrpcServerApplication {
public static void main(String[] args) {
SpringApplication.run(CloudGrpcServerApplication.class, args) ;
}
}
运行该类的main方法,然后刷新localhost:8761的网页,检查是否有服务出现。
- cloud-grpc-client编写
引入前面的grpc-lib、Eureka、grpc相关的依赖,pom.xml内容如下:
<?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>cloud-grpc</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-grpc-client</artifactId>
<packaging>jar</packaging>
<name>cloud-grpc-client</name>
<description>demo project for spring boot</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<dependency>
<groupId>net.devh</groupId>
<artifactId>grpc-client-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave-instrumentation-grpc</artifactId>
<version>${brave.instrumentation.grpc}</version>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>grpc-lib</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<!--
<goal>repackage</goal>
-->
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
在resources
目录下创建application.yml文件,配置服务注册,内容如下:
server:
port: 8080
spring:
application:
name: cloud-grpc-client
sleuth:
sampler:
probability: 1
eureka:
instance:
prefer-ip-address: true
status-page-url-path: /actuator/info
health-check-url-path: /actuator/health
instanceId: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8761/eureka/
创建调用grpc的客户端,使用@GrpcClient
声明服务的地址:
@Service
public class GrpcClientService {
@GrpcClient("cloud-grpc-server")
private Channel serverChannel;
public String sendMessage(String name) {
SimpleGrpc.SimpleBlockingStub stub = SimpleGrpc.newBlockingStub(serverChannel);
HelloReply response = stub.sayHello(HelloRequest.newBuilder().setName(name).build());
return response.getMessage();
}
}
创建控制器,方便测试:
@RestController
public class GrpcClientController {
@Autowired
private GrpcClientService grpcClientService;
@RequestMapping("/")
public String printMessage(@RequestParam(defaultValue = "Spring Cloud") String name){
return grpcClientService.sendMessage(name);
}
}
创建SpringBoot的启动类,并注册为服务:
@EnableEurekaClient
@EnableDiscoveryClient
@SpringBootApplication
public class CloudGrpcClientApplication {
public static void main(String[] args) {
SpringApplication.run(CloudGrpcClientApplication.class, args);
}
}
启动该类的main方法,然后刷新浏览器检查localhost:8761
上是否有新增服务。
- grpc调用测试
在前面步骤都正确的情况下,浏览器中访问localhost:8080
,看显示的信息。