sofastack / sofa-rpc

SOFARPC is a high-performance, high-extensibility, production-level Java RPC framework.
https://www.sofastack.tech/sofa-rpc/docs/Home
Apache License 2.0
3.81k stars 1.16k forks source link

Extension kubernetes registry #1395

Closed wangchengming666 closed 3 months ago

wangchengming666 commented 5 months ago

总体设计

image

样例

apiVersion: v1
kind: Pod
metadata:
  annotations:
   com.alipay.sofa.rpc.registry.kubernetes.TestService:1.0@DEFAULT: 'bolt://192.168.1.100:12200?accepts=100000&appName=TestAppName&weight=100&language=java&pid=41517&interface=com.alipay.sofa.rpc.registry.kubernetes.TestService&timeout=0&serialization=hessian2&protocol=bolt&delay=20&dynamic=true&startTime=1711021435028&id=rpc-cfg-0&uniqueId=&rpcVer=51300'
  creationTimestamp: "2024-02-02T08:47:54Z"
  generateName: sofa-rpc-k8s-provider-86f8565dcb-
  labels:
    com.alipay.sofa.rpc.registry.kubernetes.TestService:1.0@DEFAULT: ''
    app: sofa-rpc-k8s-provider
    pod-template-hash: 86f8565dcb
  name: sofa-rpc-k8s-provider-86f8565dcb-m7r6v
  namespace: sofa-rpc-k8s

环境准备

  1. 安装docker https://www.docker.com/
  2. 安装minikube https://kubernetes.io/zh-cn/docs/tutorials/hello-minikube/

代码准备

https://github.com/wangchengming666/sofa-rpc-registry-kubernetes-demo

测试手法

Provider部分

  1. 打包provider的镜像 docker build -t sofa-rpc-k8s-provider:1.0.0 .
  2. 加载镜像 minikube image load sofa-rpc-k8s-provider:1.0.0
  3. apply sa kubectl apply -f ServiceAccount.yml
  4. apply Deployment kubectl apply -f Deployment.yml
  5. 查看provider的pod kubectl get pods -n sofa-rpc-k8s
chengming@chengmingdeMacBook-Pro k8s % kubectl get pods -n sofa-rpc-k8s                                          
NAME                                     READY   STATUS    RESTARTS   AGE
sofa-rpc-k8s-provider-86f8565dcb-9z7hw   1/1     Running   0          7s
sofa-rpc-k8s-provider-86f8565dcb-jw4wg   1/1     Running   0          7s
sofa-rpc-k8s-provider-86f8565dcb-m7r6v   1/1     Running   0          7s
  1. 查看provider的日志 kubectl logs -f sofa-rpc-k8s-provider-86f8565dcb-m7r6v -n sofa-rpc-k8s
chengming@chengmingdeMacBook-Pro k8s % kubectl logs -f sofa-rpc-k8s-provider-86f8565dcb-m7r6v  -n sofa-rpc-k8s    

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::               (v2.4.13)

2024-02-02 08:48:02.078  INFO 1 --- [           main] com.example.appboot.AppbootApplication   : Starting AppbootApplication v0.0.1-SNAPSHOT using Java 1.8.0_342 on sofa-rpc-k8s-provider-86f8565dcb-m7r6v with PID 1 (/app/appboot-0.0.1-SNAPSHOT.jar started by root in /app)
2024-02-02 08:48:02.098  INFO 1 --- [           main] com.example.appboot.AppbootApplication   : No active profile set, falling back to default profiles: default
2024-02-02 08:48:12.561  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2024-02-02 08:48:12.753  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2024-02-02 08:48:12.757  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.55]
2024-02-02 08:48:13.470  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/myapp]  : Initializing Spring embedded WebApplicationContext
2024-02-02 08:48:13.470  INFO 1 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 10590 ms
2024-02-02 08:48:17.359  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path '/myapp'
2024-02-02 08:48:17.380  INFO 1 --- [           main] com.example.appboot.AppbootApplication   : Started AppbootApplication in 18.826 seconds (JVM running for 21.29)
AppbootApplication start
AppbootApplication start success

Consumer部分

  1. 打包consumer的镜像 docker build -t sofa-rpc-k8s-consumer:1.0.0 .
  2. 加载镜像 minikube image load sofa-rpc-k8s-consumer:1.0.0
  3. apply Deployment kubectl apply -f Deployment.yml
  4. 查看consumer的pod kubectl get pods -n sofa-rpc-k8s
chengming@chengmingdeMacBook-Pro k8s % kubectl get pods -n sofa-rpc-k8s                                                      
NAME                                     READY   STATUS    RESTARTS   AGE
sofa-rpc-k8s-consumer-5d6cd8698d-pf7wd   1/1     Running   0          4s
  1. 查看consumer的日志 kubectl logs -f sofa-rpc-k8s-consumer-5d6cd8698d-pf7wd -n sofa-rpc-k8s
chengming@chengmingdeMacBook-Pro k8s % kubectl logs -f sofa-rpc-k8s-consumer-5d6cd8698d-pf7wd -n sofa-rpc-k8s   
Listening for transport dt_socket at address: 5005

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::               (v2.4.13)

2024-02-02 08:58:31.304  INFO 1 --- [           main] c.s.e.s.SofaRpcK8sConsumerApplication    : Starting SofaRpcK8sConsumerApplication v0.0.1-SNAPSHOT using Java 1.8.0_342 on sofa-rpc-k8s-consumer-5d6cd8698d-pf7wd with PID 1 (/app/sofa-rpc-k8s-consumer-0.0.1-SNAPSHOT.jar started by root in /app)
2024-02-02 08:58:31.308  INFO 1 --- [           main] c.s.e.s.SofaRpcK8sConsumerApplication    : No active profile set, falling back to default profiles: default
2024-02-02 08:58:34.300  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8082 (http)
2024-02-02 08:58:34.320  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2024-02-02 08:58:34.320  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.55]
2024-02-02 08:58:34.590  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/myapp]  : Initializing Spring embedded WebApplicationContext
2024-02-02 08:58:34.590  INFO 1 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3165 ms
2024-02-02 08:58:36.019  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8082 (http) with context path '/myapp'
2024-02-02 08:58:36.077  INFO 1 --- [           main] c.s.e.s.SofaRpcK8sConsumerApplication    : Started SofaRpcK8sConsumerApplication in 5.676 seconds (JVM running for 6.578)
  1. 发起调用,在控制台可以看到如下日志,代表调用成功
    start call sofa-rpc-k8s-provider
    2024-02-04 02:12:40.364  WARN 1 --- [           main] io.fury.config.FuryBuilder               : Class registration isn't forced, unknown classes can be deserialized. If the environment isn't secure, please enable class registration by `FuryBuilder#requireClassRegistration(true)` or configure ClassChecker by `ClassResolver#setClassChecker`
    2024-02-04 02:12:41.593  INFO 1 --- [           main] io.fury.Fury                             : Created new fury io.fury.Fury@4d02f94e
    2024-02-04 02:12:41.675  WARN 1 --- [           main] .c.h.i.AbstractStringBuilderDeserializer : coder field not found or not accessible, will skip coder check, error is coder
    call provider success chengming
    call provider success chengming
    call provider success chengming
    call provider success chengming
    call provider success chengming
    call provider success chengming
    call provider success chengming
    call provider success chengming
    call provider success chengming
    call provider success chengming
    call provider success chengming
    call provider success chengming

Summary by CodeRabbit

codecov[bot] commented 5 months ago

Codecov Report

Attention: Patch coverage is 66.66667% with 1 lines in your changes are missing coverage. Please review.

Project coverage is 72.00%. Comparing base (df2dcae) to head (d6704a9). Report is 1 commits behind head on master.

Files Patch % Lines
...ava/com/alipay/sofa/rpc/config/RegistryConfig.java 66.66% 0 Missing and 1 partial :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #1395 +/- ## ============================================ - Coverage 72.02% 72.00% -0.03% Complexity 791 791 ============================================ Files 422 422 Lines 17813 17816 +3 Branches 2769 2769 ============================================ - Hits 12830 12828 -2 - Misses 3568 3570 +2 - Partials 1415 1418 +3 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

nobodyiam commented 5 months ago

Looks interesting. Would you please briefly describe the behavior of the register and unRegister? It seems the information is written to the pod's annotation and label?

wangchengming666 commented 5 months ago

Looks interesting. Would you please briefly describe the behavior of the register and unRegister? It seems the information is written to the pod's annotation and label?

sure, I will add the doc on this feature

wangchengming666 commented 4 months ago

related https://github.com/sofastack/sofa-boot/pull/1289

Lihus commented 4 months ago

朱立焕收到,谢谢

coderabbitai[bot] commented 3 months ago

Walkthrough

The update introduces a Kubernetes registry to the SOFA RPC framework, enhancing service discovery and registration within Kubernetes environments. It adds support for configuring Kubernetes client settings, converting Kubernetes pods to provider information, and managing service provider updates based on pod changes. Additionally, it includes testing functionality for these new features, ensuring reliability and performance in Kubernetes deployments.

Changes

Files Change Summary
core/api/src/main/java/com/alipay/sofa/rpc/config/RegistryConfig.java Added overloaded getParameter methods for retrieving parameters with default values.
registry/registry-kubernetes/src/main/java/com/alipay/sofa/rpc/registry/kubernetes/... Introduced Kubernetes registry support, including service registration, discovery, and provider management.
registry/registry-kubernetes/src/main/java/com/alipay/sofa/rpc/registry/kubernetes/constant/KubernetesClientConstants.java
registry/registry-kubernetes/src/main/java/com/alipay/sofa/rpc/registry/kubernetes/utils/KubernetesClientUtils.java
registry/registry-kubernetes/src/main/java/com/alipay/sofa/rpc/registry/kubernetes/utils/KubernetesConfigUtils.java
Added Kubernetes client configuration utilities and constants.
registry/registry-kubernetes/src/main/resources/META-INF/services/sofa-rpc/com.alipay.sofa.rpc.registry.Registry Registered Kubernetes registry in service loader.
registry/registry-kubernetes/src/test/java/com/alipay/sofa/rpc/registry/kubernetes/... Provided tests for Kubernetes registry functionality.
registry/registry-kubernetes/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker Enabled mock-maker-inline for advanced mocking capabilities.

"In the land of code and Kubernetes pods,
A rabbit hopped through, leaving digital nods.
🐰🌟 With each leap and bound,
New registries were found,
And services danced, free from their clods.
So here's to changes, both big and small,
May our code run smoothly, bugs none at all."

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code)

Tips ### Chat There are 3 ways to chat with CodeRabbit: - Review comments: Directly reply to a review comment made by CodeRabbit. Example: - `I pushed a fix in commit .` - `Generate unit-tests for this file.` - `Open a follow-up GitHub issue for this discussion.` - Files and specific lines of code (under the "Files changed" tab): Tag `@coderabbitai` in a new review comment at the desired location with your query. Examples: - `@coderabbitai generate unit tests for this file.` - `@coderabbitai modularize this function.` - PR comments: Tag `@coderabbitai` in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples: - `@coderabbitai generate interesting stats about this repository and render them as a table.` - `@coderabbitai show all the console.log statements in this repository.` - `@coderabbitai read src/utils.ts and generate unit tests.` - `@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.` Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. ### CodeRabbit Commands (invoked as PR comments) - `@coderabbitai pause` to pause the reviews on a PR. - `@coderabbitai resume` to resume the paused reviews. - `@coderabbitai review` to trigger a review. This is useful when automatic reviews are disabled for the repository. - `@coderabbitai resolve` resolve all the CodeRabbit review comments. - `@coderabbitai help` to get help. Additionally, you can add `@coderabbitai ignore` anywhere in the PR description to prevent this PR from being reviewed. ### CodeRabbit Configration File (`.coderabbit.yaml`) - You can programmatically configure CodeRabbit by adding a `.coderabbit.yaml` file to the root of your repository. - The JSON schema for the configuration file is available [here](https://coderabbit.ai/integrations/coderabbit-overrides.v2.json). - If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: `# yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json` ### CodeRabbit Discord Community Join our [Discord Community](https://discord.com/invite/GsXnASn26c) to get help, request features, and share feedback.