本文使用K8s当做服务注册与发现、配置管理,使用gRpc用做服务间的远程通讯
一、先准备K8s
我在本地有个K8s单机
二、准备service-provider
pom
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.14</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>service-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>service-provider</name>
<description>service-provider</description>
<properties>
<java.version>11</java.version>
<!-- <os.detected.classifier>osx-aarch_64</os.detected.classifier>-->
<os.detected.classifier>osx-x86_64</os.detected.classifier>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<type>pom</type>
<scope>import</scope>
<version>2.5.14</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2020.0.6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-kubernetes-dependencies</artifactId>
<version>1.1.10.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<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-kubernetes-client-all</artifactId>
</dependency>
<!-- gRpc -->
<dependency>
<groupId>net.devh</groupId>
<artifactId>grpc-spring-boot-starter</artifactId>
<version>2.13.1.RELEASE</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>1.52.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<version>1.18.24</version>
</dependency>
</dependencies>
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.7.1</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.21.7:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.52.1:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
配置文件 application.yml
grpc:
server:
port: 9090
server:
port: 30000
spring:
application:
name: service-provider
cloud:
kubernetes:
reload:
enabled: true
mode: polling
period: 5000
logging:
level:
org.springframework.cloud.loadbalancer: debug
主类 ServiceProviderApplication
package com.example.service.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class ServiceProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceProviderApplication.class, args);
}
}
在 src/main/proto文件夹下新增PB文件 HelloFacade.proto
syntax = "proto3";
option java_multiple_files = true;
option java_package = "com.example.service.provider.api";
option java_outer_classname = "HelloServiceProto";
service HelloService {
rpc SayHello (SayHelloRequest) returns (SayHelloResponse) {
}
}
message SayHelloRequest {
string name = 1;
}
message SayHelloResponse {
int32 code = 1;
string message = 2;
bool success = 3;
SayHelloData data = 4;
}
message SayHelloData {
string name = 1;
string content = 2;
}
接口实现类
package com.example.service.provider.facade;
import com.example.service.provider.api.HelloServiceGrpc;
import com.example.service.provider.api.SayHelloData;
import com.example.service.provider.api.SayHelloRequest;
import com.example.service.provider.api.SayHelloResponse;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.server.service.GrpcService;
@Slf4j
@GrpcService
public class HelloServiceImpl extends HelloServiceGrpc.HelloServiceImplBase {
@Override
public void sayHello(SayHelloRequest request, io.grpc.stub.StreamObserver<SayHelloResponse> responseObserver) {
log.info("接收consumer的say hello grpc 请求");
SayHelloData helloData = SayHelloData.newBuilder()
.setName("maple")
.setContent("say hello")
.build();
SayHelloResponse.Builder builder = SayHelloResponse.newBuilder()
.setCode(0)
.setMessage("success")
.setSuccess(true)
.setData(helloData);
responseObserver.onNext(builder.build());
responseObserver.onCompleted();
}
}
Dockerfile
FROM openjdk:11-jdk-slim
ENV jarName="service-provider.jar"
ADD service-provider-0.0.1-SNAPSHOT-exec.jar $jarName
ENTRYPOINT java -Duser.timezone=GMT+8 -jar $jarName
K8s声明yaml文件 service-provider-deploy.yaml
apiVersion: v1
kind: Namespace
metadata:
name: service-k8s-demo
labels:
name: service-k8s-demo
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: service-k8s-demo
namespace: service-k8s-demo
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
namespace: service-k8s-demo
name: service-k8s-demo
rules:
- apiGroups:
- ""
resources:
- services
- configmaps
- endpoints
- nodes
- pods
- secrets
- namespaces
verbs:
- get
- list
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: service-k8s-demo
namespace: service-k8s-demo
subjects:
- kind: ServiceAccount
name: service-k8s-demo
namespace: service-k8s-demo
roleRef:
kind: ClusterRole
name: service-k8s-demo
apiGroup: rbac.authorization.k8s.io
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: service-provider
namespace: service-k8s-demo
labels:
app: service-provider
spec:
replicas: 3
template:
metadata:
name: service-provider
labels:
app: service-provider
spec:
containers:
- name: service-provider
image: service-provider:1.0
ports:
- name: http
protocol: TCP
containerPort: 30000
- name: grpc
protocol: TCP
containerPort: 9090
imagePullPolicy: IfNotPresent
serviceAccountName: service-k8s-demo
restartPolicy: Always
selector:
matchLabels:
app: service-provider
---
apiVersion: v1
kind: Service
metadata:
name: service-provider
namespace: service-k8s-demo
spec:
selector:
app: service-provider
ports:
- port: 80
targetPort: 30000
name: http
- port: 9090
targetPort: 9090
name: grpc
clusterIP: None
本地启动测试
三、准备service-consumer
pom
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.14</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>service-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>service-consumer</name>
<description>service-consumer</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<type>pom</type>
<scope>import</scope>
<version>2.5.14</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2020.0.6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-kubernetes-dependencies</artifactId>
<version>1.1.10.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>service-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
</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-kubernetes-client-all</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<version>1.18.24</version>
</dependency>
<!-- gRpc -->
<dependency>
<groupId>net.devh</groupId>
<artifactId>grpc-spring-boot-starter</artifactId>
<version>2.13.1.RELEASE</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>1.52.1</version>
</dependency>
</dependencies>
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.7.1</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.21.7:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.52.1:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
配置文件 application.yml
grpc:
server:
port: 9091
client:
service-provider:
negotiation-type: plaintext
enable-keep-alive: true
keep-alive-without-calls: true
address: 'static://service-provider:9090'
server:
port: 30001
spring:
application:
name: service-consumer
cloud:
kubernetes:
reload:
enabled: true
mode: polling
period: 5000
logging:
level:
org.springframework.cloud.loadbalancer: debug
主类 ServiceConsumerApplication
package com.example.service.consumer;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@Slf4j
@RestController
@EnableDiscoveryClient
@SpringBootApplication
@RequiredArgsConstructor
public class ServiceConsumerApplication {
private final DiscoveryClient discoveryClient;
private final ProviderServiceGrpcClient providerServiceGrpcClient;
public static void main(String[] args) {
SpringApplication.run(ServiceConsumerApplication.class, args);
}
@GetMapping("/grpc/hello")
public String sayHello() {
log.info("消费服务:service-consumer grpc 调用 service-provider");
return providerServiceGrpcClient.sayHello();
}
@GetMapping("/consumers/services")
public List<String> findServices() {
log.info("当前注册中心下所有服务");
List<String> services = discoveryClient.getServices();
services.stream().map(discoveryClient::getInstances).forEach(v ->
v.forEach(s -> System.out.printf("%s:%s uri:%s%n", s.getHost(), s.getPort(), s.getUri())));
return services;
}
}
gRpc调用Provider ProviderServiceGrpcClient
package com.example.service.consumer;
import com.example.service.provider.api.HelloServiceGrpc;
import com.example.service.provider.api.SayHelloRequest;
import com.example.service.provider.api.SayHelloResponse;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Service;
@Service
public class ProviderServiceGrpcClient {
@GrpcClient("service-provider")
private HelloServiceGrpc.HelloServiceBlockingStub helloServiceBlockingStub;
public String sayHello() {
SayHelloRequest request = SayHelloRequest.newBuilder().setName("maple123").build();
SayHelloResponse sayHelloResponse = helloServiceBlockingStub.sayHello(request);
return sayHelloResponse.toString();
}
}
Dockerfile
FROM openjdk:11-jdk-slim
ENV jarName="service-consumer.jar"
ADD service-consumer-0.0.1-SNAPSHOT-exec.jar $jarName
ENTRYPOINT java -Duser.timezone=GMT+8 -jar $jarName
K8s声明yaml文件 service-consumer-deploy.yaml
apiVersion: v1
kind: Namespace
metadata:
name: service-k8s-demo
labels:
name: service-k8s-demo
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: service-k8s-demo
namespace: service-k8s-demo
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
namespace: service-k8s-demo
name: service-k8s-demo
rules:
- apiGroups:
- ""
resources:
- services
- configmaps
- endpoints
- nodes
- pods
- secrets
- namespaces
verbs:
- get
- list
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: service-k8s-demo
namespace: service-k8s-demo
subjects:
- kind: ServiceAccount
name: service-k8s-demo
namespace: service-k8s-demo
roleRef:
kind: ClusterRole
name: service-k8s-demo
apiGroup: rbac.authorization.k8s.io
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: service-consumer
namespace: service-k8s-demo
labels:
app: service-consumer
spec:
replicas: 1
template:
metadata:
name: service-consumer
labels:
app: service-consumer
spec:
containers:
- name: service-consumer
image: service-consumer:1.0
ports:
- name: http
protocol: TCP
containerPort: 30001
- name: grpc
protocol: TCP
containerPort: 9091
imagePullPolicy: IfNotPresent
serviceAccountName: service-k8s-demo
restartPolicy: Always
selector:
matchLabels:
app: service-consumer
---
apiVersion: v1
kind: Service
metadata:
name: service-consumer
namespace: service-k8s-demo
spec:
selector:
app: service-consumer
ports:
- port: 80
targetPort: 30001
name: http
- port: 9091
targetPort: 9091
name: grpc
type: NodePort
本地启动测试
四、打镜像
docker build -t service-provider:1.0 .
docker build -t service-consumer:1.0 .
五、执行K8s yaml文件 创建命名空间、账号、角色、部署应用、暴露端口等
kubetl apply -f service-provider-deploy.yaml
kubetl apply -f service-consumer-deploy.yaml
六、查看与测试
gRpc正常访问,但是发现一个问题,下面的图片,上面三个是Provider,发现负载均衡失效了,请求全都打在了一个Provider里,搜了下发现和 gRpc基于HTTP2.0 多路复用 有关,之后再处理吧。