本文使用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.6.7</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.6.7</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2021.0.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-kubernetes-dependencies</artifactId>
<version>1.1.0.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.cloud</groupId>
<artifactId>spring-cloud-starter-kubernetes-client-all</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- gRpc -->
<dependency>
<groupId>net.devh</groupId>
<artifactId>grpc-spring-boot-starter</artifactId>
<version>2.14.0.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>
配置文件 bootstrap.yaml
management:
endpoint:
restart:
enabled: true
health:
enabled: true
info:
enabled: true
endpoints:
web:
exposure:
include: '*'
grpc:
server:
port: 9090
server:
port: 30000
spring:
application:
name: service-provider
cloud:
kubernetes:
reload:
enabled: true
config:
sources:
- namespace: service-k8s-demo
name: service-provider-config
主类 ServiceProviderApplication
package com.example.service.provider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
@SpringBootApplication
@EnableDiscoveryClient
@RefreshScope
public class ServiceProviderApplication {
@Value("${maple-test.message}")
private String mapleTestMessage;
@Autowired
private DemoConfig demoConfig;
public static void main(String[] args) {
SpringApplication.run(ServiceProviderApplication.class, args);
}
@GetMapping("/provider-hello")
public String sayHello() {
log.info("hello world");
return "hello world";
}
@GetMapping("/provider-value-config")
public String valueConfig() {
log.info("直接@Value获取配置:{}", mapleTestMessage);
return mapleTestMessage;
}
@GetMapping("/demo-config")
public String demoConfig() {
log.info("通过ConfigurationProperties注解获取配置:{}", demoConfig.getMessage());
return demoConfig.getMessage();
}
}
在 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.DemoConfig;
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;
import org.springframework.beans.factory.annotation.Autowired;
@Slf4j
@GrpcService
public class HelloServiceImpl extends HelloServiceGrpc.HelloServiceImplBase {
@Autowired
private DemoConfig demoConfig;
@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(demoConfig.getMessage())
.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
ADD service-provider-0.0.1-SNAPSHOT-exec.jar service-provider.jar
ENTRYPOINT ["java", "-jar", "service-provider.jar"]
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: 1
template:
metadata:
name: service-provider
labels:
app: service-provider
spec:
containers:
- name: service-provider
image: service-provider:1.0
imagePullPolicy: IfNotPresent
ports:
- name: http
protocol: TCP
containerPort: 30000
- name: grpc
protocol: TCP
containerPort: 9090
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
type: NodePort
K8s声明yaml文件 service-provider-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: service-provider-config
namespace: service-k8s-demo
data:
bean.message: Hello World!
maple-test.message: maple for dev config map in k8s
redis.ip: 111 in k8s
本地启动测试
三、准备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.6.7</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.6.7</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2021.0.2</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.14.0.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>
配置文件 bootstrap.yaml
management:
endpoint:
restart:
enabled: true
health:
enabled: true
info:
enabled: true
endpoints:
web:
exposure:
include: '*'
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
config:
sources:
- namespace: service-k8s-demo
name: service-consumer-config
主类 ServiceConsumerApplication
package com.example.service.consumer;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
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.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@Slf4j
@RestController
@EnableDiscoveryClient
@SpringBootApplication
@RequiredArgsConstructor
@RefreshScope
public class ServiceConsumerApplication {
private final DiscoveryClient discoveryClient;
private final ProviderServiceGrpcClient providerServiceGrpcClient;
public static void main(String[] args) {
SpringApplication.run(ServiceConsumerApplication.class, args);
}
@Value("${consumer.body}")
private String consumerBody;
@GetMapping("/grpc/hello")
public String sayHello() {
log.info("消费服务:service-consumer grpc 调用 service-provider");
return providerServiceGrpcClient.sayHello();
}
@GetMapping("/consumerBody")
public String consumerBody() {
log.info("获取配置consumerBody:{}", consumerBody);
return consumerBody;
}
@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
ADD service-consumer-0.0.1-SNAPSHOT-exec.jar service-consumer.jar
ENTRYPOINT java -jar service-consumer.jar
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
imagePullPolicy: IfNotPresent
ports:
- name: http
protocol: TCP
containerPort: 30001
- name: grpc
protocol: TCP
containerPort: 9091
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
K8s声明yaml文件 service-consumer-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: service-consumer-config
namespace: service-k8s-demo
data:
consumer.body: "1234567890"
本地启动测试
四、打镜像
docker build -t service-provider:1.0 .
docker build -t service-consumer:1.0 .
五、执行K8s yaml文件 创建命名空间、账号、角色、部署应用、暴露端口等
kubetl apply -f service-provider-config.yaml
kubetl apply -f service-provider-deploy.yaml
kubetl apply -f service-consumer-config.yaml
kubetl apply -f service-consumer-deploy.yaml
六、查看与测试
gRpc正常访问,但是发现一个问题,下面的图片,上面三个是Provider,发现负载均衡失效了,请求全都打在了一个Provider里,搜了下发现和 gRpc基于HTTP2.0 多路复用 有关,之后再处理吧。
配置测试
动态配置测试