### Getting Started with Dew DevOps Example Project Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/devops/quick-start.adoc This snippet provides the commands to clone, install, and deploy the example To-Do project using Dew DevOps. It also includes instructions for local host mapping and accessing the deployed application. Prerequisites include Java (>=11), Maven, and NodeJS (>=8). ```Shell # ======= 前置准备 ======= # 执行dew-devops.sh根据提示创建项目 sh dew-devops.sh # 添加本地Host映射 - x.x.x.x todo-uat.idealworld.group x.x.x.x todo-api-uat.idealworld.group - # ======= 前置准备 ======= git clone https://github.com/dew-ms/devops-example-todo.git cd devops-example-todo # 执行安装 mvn install -Dmaven.test.skip=true # 部署到Kubernetes mvn -P devops deploy -Ddew_devops_profile=uat ``` -------------------------------- ### Install Git Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/devops/install.adoc Installs Git using yum, a prerequisite for some Kubernetes operations. ```bash yum install -y git ``` -------------------------------- ### Install and Configure NFS Server on Linux Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/appendix/middleware.adoc This snippet provides commands to install NFS utilities, create necessary directories, configure NFS exports in `/etc/exports` for read-write access, and enable/start NFS services. It also includes a command to verify the setup and notes a common Kubernetes mounting error related to missing `nfs-utils`. ```bash yum install -y nfs-utils mkdir -p /data/nfs chmod 755 /data/nfs # Edit /etc/exports and add the following line: # /data/nfs *(rw,sync,no_root_squash,no_all_squash) systemctl enable rpcbind systemctl enable nfs-server systemctl start rpcbind systemctl start nfs-server showmount -e localhost ``` -------------------------------- ### Install Kubectl for K3s Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/devops/install.adoc Installs the standard `kubectl` command-line tool using an Aliyun mirror and configures it to interact with the K3s cluster. ```bash cat < /etc/yum.repos.d/kubernetes.repo [kubernetes] name=Kubernetes baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64 enabled=1 gpgcheck=0 repo_gpgcheck=0 gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg EOF yum install -y kubectl cp /etc/rancher/k3s/k3s.yaml ~/.kube/config ``` -------------------------------- ### Install Harbor Container Registry using Helm Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/devops/install.adoc Steps to add the Harbor Helm repository, update it, and install Harbor with Ingress configuration and node selectors. Includes commands for uninstallation, Docker daemon configuration for insecure registries, and basic Docker commands for logging in and pushing images to Harbor. ```bash helm repo add harbor https://helm.goharbor.io helm repo update helm install dew-harbor harbor/harbor --namespace devops \ --set expose.type=ingress \ --set expose.ingress.hosts.core=harbor.dew.idealworld.group \ --set expose.ingress.hosts.notary=notary.dew.idealworld.group \ --set externalURL=https://harbor.dew.idealworld.group \ --set core.nodeSelector.group=devops \ --set portal.nodeSelector.group=devops \ --set jobservice.nodeSelector.group=devops \ --set registry.nodeSelector.group=devops \ --set chartmuseum.nodeSelector.group=devops \ --set clair.nodeSelector.group=devops \ --set notary.nodeSelector.group=devops \ --set database.internal.nodeSelector.group=devops \ --set redis.internal.nodeSelector.group=devops # 删除 helm uninstall dew-harbor --namespace devops # 添加 ``"insecure-registries": ["harbor.dew.idealworld.group"]`` 到 /etc/docker/daemon.json # 登录Harbor docker login -u -p docker login harbor.dew.idealworld.group -u admin -p Harbor12345<请修改> # e.g. 上传镜像 docker pull dewms/devops:latest docker tag dewms/devops:latest harbor.dew.idealworld.group/dewms/devops:latest docker push harbor.dew.idealworld.group/dewms/devops:latest ``` -------------------------------- ### Verify Metrics Server Installation Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/devops/install.adoc Verifies the successful installation of Metrics Server by checking node and pod resource usage. ```bash kubectl top node kubeadm top pod ``` -------------------------------- ### Install GitLab CE using Omnibus Package Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/devops/install.adoc Instructions for installing GitLab Community Edition on RPM-based systems using the Omnibus package, including repository setup, package installation, configuration file modification for external URL, and reconfigure command. Users are advised to modify the root password via browser after installation. ```bash curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash yum install -y gitlab-ce # 按需修改,可修改说明见: https://docs.gitlab.com/omnibus/settings/ vi /etc/gitlab/gitlab.rb - external_url 'http://gitlab.dew.idealworld.group' ... - gitlab-ctl reconfigure # 浏览器访问并修改root密码 ``` -------------------------------- ### Install Helm 3 Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/devops/install.adoc Installs Helm 3, the Kubernetes package manager, using the official installation script. ```bash curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash ``` -------------------------------- ### Basic Authentication Cache Usage Example in Java Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/framework/user-manual.adoc This Java example illustrates a complete flow of user authentication using Dew's authentication cache. It includes mock implementations for user registration, login (setting OptInfoExt), business operations requiring authenticated user data, and logout (removing OptInfo). It demonstrates how to extend OptInfo and interact with Dew.auth. ```Java /** * 模拟用户注册. */ @PostMapping(value = "user/register") public Resp register(@RequestBody User user) { // 实际注册处理 user.setId($.field.createUUID()); MOCK_USER_CONTAINER.put(user.getId(), user); return Resp.success(null); } /** * 模拟用户登录. */ @PostMapping(value = "auth/login") public Resp login(@RequestBody LoginDTO loginDTO) { // 实际登录处理 User user = MOCK_USER_CONTAINER.values().stream().filter(u -> u.getIdCard().equals(loginDTO.getIdCard())).findFirst().get(); String token = $.field.createUUID(); Dew.auth.setOptInfo(new OptInfoExt() .setIdCard(user.getIdCard()) .setAccountCode($.field.createShortUUID()) .setToken(token) .setName(user.getName()) .setMobile(user.getPhone()) .setRoleInfo(new HashSet<>() { { add(new OptInfo.RoleInfo() .setCode(userDTO.getRole()) .setName(".." ); } })); return Resp.success(token); } /** * 模拟业务操作. */ @GetMapping(value = "business/someopt") public Resp someOpt() { // 获取登录用户信息 Optional optInfoExtOpt = Dew.auth.getOptInfo(); if (!optInfoExtOpt.isPresent()) { return Resp.unAuthorized("用户认证错误"); } // 登录用户的信息 optInfoExtOpt.get(); return Resp.success(null); } /** * 模拟用户注销. */ @DeleteMapping(value = "auth/logout") public Resp logout() { // 实际注册处理 Dew.auth.removeOptInfo(); return Resp.success(null); } ``` -------------------------------- ### Deploy Sonatype Nexus using Helm Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/devops/install.adoc Steps to add the Oteemo Helm repository and install Sonatype Nexus with Ingress enabled and custom host configurations for HTTP and Docker proxies. Default administrator credentials are provided. ```bash helm repo add oteemocharts https://oteemo.github.io/charts helm repo update helm install dew-nexus oteemocharts/sonatype-nexus --namespace devops \ --set ingress.enabled=true \ --set nexusProxy.env.nexusHttpHost=maven.dew.idealworld.group \ --set nexusProxy.env.nexusDockerHost=maven.dew.idealworld.group # 用户名 admin 密码 admin123 ``` -------------------------------- ### Install K3s Server Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/devops/install.adoc Installs the K3s server, configuring it to use Docker and disabling Traefik for custom ingress solutions. ```bash curl -sfL https://docs.rancher.cn/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn INSTALL_K3S_EXEC="server --docker --no-deploy traefik" sh - ``` -------------------------------- ### Install Calico Network Plugin Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/devops/install.adoc Applies the Calico CNI manifest to install the network plugin for pod communication. ```bash kubectl apply -f https://docs.projectcalico.org/v3.14/manifests/calico.yaml ``` -------------------------------- ### Dew Recommended Java Code Package Structure and MQ Listener Example Source: https://github.com/gudaoxuri/dew/blob/master/docs/index.html Details the recommended package structure for a Dew service, including key classes like XApplication (service startup), XConfig (Spring Cloud configuration), XInitiator (initialization for DB/cache/tasks), and controller packages for interaction interfaces like MQ receivers. An example of an MQ listener using Dew's cluster MQ feature is provided. ```APIDOC |- X service |- |- src |- |- |- main |- |- |- |- java |- |- |- |- |- src |- |- |- |- |- |- x.y.z |- |- |- |- |- |- |- XApplication (1) |- |- |- |- |- |- |- XConfig (2) |- |- |- |- |- |- |- XInitiator (3) |- |- |- |- |- |- |- controller |- |- |- |- |- |- |- |- EventController (4) (1) XApplication: 服务启动类 (Service startup class) (2) XConfig: 当前服务的配置文件(Spring Cloud格式) (Current service configuration file (Spring Cloud format)) (3) XInitiator: 当前服务启动初始化器,原则上所有与数据库、缓存、定时任务相关的初始化操作都应该从此类发起,以方便排错 (Current service startup initializer; all database, cache, and scheduled task initializations should originate here for easier troubleshooting) (4) EventController: 事件处理器,名称可以更具象,原则上所有与MQ receive相关的操作都被视为交互接口,应该放到controller包下 (Event handler; name can be more specific; all MQ receive-related operations are considered interaction interfaces and should be placed under the controller package) ``` ```Java @PostConstruct public void processTodoAddEvent() { // 使用Dew的集群MQ功能实现消息点对点接收 Dew.cluster.mq.response(Constants.MQ_NOTIFY_TODO_ADD, todo -> { logger.info("Received add todo event :" + todo); }); } ``` -------------------------------- ### Java Example for MQ Publish-Subscribe and Request-Response Source: https://github.com/gudaoxuri/dew/blob/master/docs/index.html This Java example demonstrates the usage of Dew's ClusterMQ service. It shows how to subscribe to a topic for publish-subscribe messaging, publish messages with and without custom headers, and how to set up a responder and send requests for request-response communication. It highlights the non-blocking nature of subscribe and response methods. Note that topics must exist before publishing; subscribing to a topic will automatically create it. ```Java // pub-sub Dew.cluster.mq.subscribe("test_pub_sub", message -> logger.info("pub_sub>>" + message.getBody())); Dew.cluster.mq.publish("test_pub_sub", "msgA",new HashMap() { { put("h", "001"); } }); Dew.cluster.mq.publish("test_pub_sub", "msgB"); // req-resp Dew.cluster.mq.response("test_rep_resp", message -> logger.info("req_resp>>" + message.getBody())); Dew.cluster.mq.request("test_rep_resp", "msg1",new HashMap() { { put("h", "001"); } }); Dew.cluster.mq.request("test_rep_resp", "msg2"); ``` -------------------------------- ### Install Metrics Server for Kubernetes Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/devops/install.adoc Installs the Metrics Server, required for Horizontal Pod Autoscaling (HPA), by applying the official components YAML. ```bash kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/download/v0.3.7/components.yaml ``` -------------------------------- ### Kubernetes Ingress Configuration Created by Dew DevOps Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/devops/quick-start.adoc This snippet shows an example Kubernetes Ingress configuration generated by `dew-devops.sh`. It defines host-based routing for different services (kernel and frontend) within a Kubernetes namespace, mapping domains to service names and ports. ```Kubernetes YAML apiVersion: extensions/v1beta1 kind: Ingress metadata: annotations: name: dew-ingress namespace: dew-uat spec: rules: - host: api-uat.idealworld.group http: paths: - backend: serviceName: kernel servicePort: 8080 path: / - host: uat.dew.idealworld.group http: paths: - backend: serviceName: frontend servicePort: 80 path: / ``` -------------------------------- ### Install Nginx Ingress Controller using Helm Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/devops/install.adoc Instructions for installing the Nginx Ingress Controller via Helm, exposing ports 80 and 443, and enabling metrics. It configures the controller as a DaemonSet with host network enabled. Includes a command for uninstallation. ```bash # 使用如下方式将80 443暴露出来 helm install dew-nginx stable/nginx-ingress --namespace nginx-ingress \ --set controller.kind=DaemonSet \ --set controller.hostNetwork=true \ --set defaultBackend.enabled=false \ --set controller.metrics.enabled=true # 删除 helm uninstall dew-nginx --namespace nginx-ingress ``` -------------------------------- ### Install K3s Agent Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/devops/install.adoc Installs a K3s agent node, configuring it to use Docker and disabling Traefik and ServiceLB, then joining it to the K3s server using the provided URL and token. ```bash curl -sfL https://docs.rancher.cn/k3s/k3s-install.sh | \ INSTALL_K3S_MIRROR=cn INSTALL_K3S_EXEC="--docker --no-deploy traefik --no-deploy servicelb" \ K3S_URL=https://:6443 K3S_TOKEN= sh - ``` -------------------------------- ### Install Kibana with NFS Persistent Volume Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/devops/install.adoc Instructions for deploying Kibana using Helm, including creating an NFS directory, and defining Kubernetes Persistent Volume (PV) and Persistent Volume Claim (PVC) for storage. Also provides notes on using StorageClass instead of explicit PV/PVC creation. ```bash # 使用NFS存储,创建PV & PVC # 在NFS节点中创建NFS目录 mkdir -p /data/nfs/kibana # 在Kubernetes Master节点中创建PV & PVC cat < register(@RequestBody User user) { // 实际注册处理 user.setId($.field.createUUID()); MOCK_USER_CONTAINER.put(user.getId(), user); return Resp.success(null); } /** * 模拟用户登录. */ @PostMapping(value = "auth/login") public Resp login(@RequestBody LoginDTO loginDTO) { // 实际登录处理 User user = MOCK_USER_CONTAINER.values().stream().filter(u -> u.getIdCard().equals(loginDTO.getIdCard())).findFirst().get(); String token = $.field.createUUID(); Dew.auth.setOptInfo(new OptInfoExt() .setIdCard(user.getIdCard()) .setAccountCode($.field.createShortUUID()) .setToken(token) .setName(user.getName()) .setMobile(user.getPhone()) .setRoleInfo(new HashSet<>() { { add(new OptInfo.RoleInfo() .setCode(userDTO.getRole()) .setName("..") ); } })); return Resp.success(token); } /** * 模拟业务操作. */ @GetMapping(value = "business/someopt") public Resp someOpt() { // 获取登录用户信息 Optional optInfoExtOpt = Dew.auth.getOptInfo(); if (!optInfoExtOpt.isPresent()) { return Resp.unAuthorized("用户认证错误"); } // 登录用户的信息 optInfoExtOpt.get(); return Resp.success(null); } /** * 模拟用户注销. */ @DeleteMapping(value = "auth/logout") public Resp logout() { ``` -------------------------------- ### Check Helm Version Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/devops/install.adoc Command to display the installed Helm client and server versions. ```bash helm version ``` -------------------------------- ### Dew Configuration File Reference Source: https://github.com/gudaoxuri/dew/blob/master/devops/it/src/it/helloworld-frontend/readme.adoc This snippet indicates how the project's .dew configuration file is referenced. This file typically contains YAML definitions crucial for DevOps settings within the Dew microservice system, enabling automated deployments. ```yml include::.dew[] ``` -------------------------------- ### Using HBaseTemplate in Spring Boot Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/framework/user-manual.adoc Illustrates how to inject and use `HBaseTemplate` in a Spring Boot application to perform operations like retrieving data from HBase. The example shows a `get` operation with a custom row mapper. ```Java @Autowired private HBaseTemplate hbaseTemplate; hbaseTemplate.get("table_hbase", "0002093140000000", "0", "reg_platform", (result, row) -> Bytes.toString(result.value())); ``` -------------------------------- ### Helm Installation Notes and Configuration for Prometheus Operator Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/devops/install.adoc This section provides essential notes and configuration parameters for installing the Prometheus Operator using Helm. It covers creating necessary secrets for etcd monitoring, adjusting image repositories, enabling etcd monitoring, configuring Grafana via `grafana.ini`, and using StorageClasses for persistent storage for Grafana, Prometheus, and Alertmanager. ```text # 若需要对etcd进行监控,则需要先创建secret kubectl -n devops create secret generic dew-prometheus-operator-etcd \ --from-file=/etc/kubernetes/pki/etcd/ca.crt \ --from-file=/etc/kubernetes/pki/etcd/peer.crt \ --from-file=/etc/kubernetes/pki/etcd/peer.key # 安装 # · 不使用代理要加上 --set kube-state-metrics.image.repository=registry.cn-hangzhou.aliyuncs.com/google_containers/kube-state-metrics # · 若要启用对etcd监控,需设置kubeEtcd相关参数。 # · grafana.'grafana\.ini'为Grafana的配置参数,请安装时自行修改。 # · 使用StorageClass # - 若 Grafana 使用StorageClass,需要进行以下配置,无需创建PV,PVC # --set grafana.persistence.storageClassName="yourscname" # --set grafana.persistence.size=10Gi # 删去此行配置: --set grafana.persistence.existingClaim=grafana \ # - Prometheus使用StorageClass,需要添加以下配置,无需创建PV,PVC # --set prometheus.prometheusSpec.storageSpec.volumeClaimTemplate.spec.storageClassName="yourscname" \ # - Alertmanager和Prometheus使用StorageClass,需要添加以下配置,无需创建PVC,但因为自动生成的PVC名称过长, # 而无法动态创建PV,所以需要自行修改chart相关配置或指定已创建好的PV名称: # --set alertmanager.alertmanagerSpec.storage.volumeClaimTemplate.spec.volumeName=prometheus-alertmanager \ ``` -------------------------------- ### Add Helm Charts Repository Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/devops/install.adoc Removes the default stable repository and adds the Azure China mirror for Helm charts, then updates the repository index. ```bash helm repo remove stable helm repo add stable http://mirror.azure.cn/kubernetes/charts/ helm repo update ``` -------------------------------- ### Verify All Pods Status Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/devops/install.adoc Checks the status of all pods across all namespaces to confirm successful deployment. ```bash kubectl get pods --all-namespaces ``` -------------------------------- ### Java Example: Using ClusterLock for Distributed Locking Source: https://github.com/gudaoxuri/dew/blob/master/docs/index.html Demonstrates how to create a `ClusterLock` instance, acquire a lock using `tryLock` with manual unlocking, and a simplified approach using `tryLockWithFun` which handles automatic unlocking. ```Java // 创建指定名为test_lock的分布锁实例 ClusterLock lock = Dew.cluster.lock.instance("test_lock"); // tryLock 示例,等待0ms,忘了手工unLock或出异常时1s后自动解锁 if (lock.tryLock(0, 1000)) { try { // 已加锁,执行业务方法 } finally { // 手工解锁 lock.unLock(); } } // 上面的示例可用 tryLockWithFun 简化 lock.tryLockWithFun(0, 1000, () -> { // 已加锁,执行业务方法,tryLockWithFun会将业务方法包裹在try-finally中,无需手工解锁 }); ``` -------------------------------- ### Java Example for Dew Cluster Distributed Cache Operations Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/framework/user-manual.adoc This Java code snippet demonstrates common operations using Dew Cluster's distributed cache. It includes examples for flushing the database, deleting keys, checking key existence, setting values with expiration, and retrieving values, showcasing basic interactions with the `Dew.cluster.cache` API. ```Java // 清空DB Dew.cluster.cache.flushdb(); // 删除key Dew.cluster.cache.del("n_test"); // 判断是否存在 Dew.cluster.cache.exists("n_test"); // 设置值 Dew.cluster.cache.set("n_test", "{\"name\":\"jzy\"}", 1); // 获取值并转成Json $.json.toJson(Dew.cluster.cache.get("n_test")); ``` -------------------------------- ### Join Node to Kubernetes Cluster and Verify Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/devops/install.adoc Instructions for joining a worker node to the Kubernetes cluster using the `kubeadm join` command, followed by verifying node status on the master. ```bash # 执行上一步输出的 kubeadm join ... # 完成后在master上执行情况如下(以1.18版本为例) kubeadm get no ``` -------------------------------- ### GitLab High Availability Configuration Reference Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/devops/install.adoc Reference to the official GitLab documentation for setting up High Availability (HA) configurations. ```APIDOC # @see https://about.gitlab.com/solutions/high-availability/ ``` -------------------------------- ### Example Dew Custom Exception Mapping Configuration (XML) Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/framework/user-manual.adoc Provides an example of configuring `java.io.IOException` to return a 200 HTTP status code and a custom business code 'NBE00123' using Dew's error mapping. ```xml dew: basic: error-mapping: "java.io.IOException": http-code: 200 business-code: "NBE00123" ``` -------------------------------- ### Dew Notification Configuration Example Source: https://github.com/gudaoxuri/dew/blob/master/docs/index.html Provides concrete examples of configuring different notification types: a DingTalk notification for errors, an email notification, and a custom HTTP notification. ```YAML # 示例 dew: notifies: __DEW_ERROR__: type: DD defaultReceivers: xxxx args: url: https://oapi.dingtalk.com/robot/send?access_token=8ff65c48001c1981df7d3269 strategy: minIntervalSec: 5 sendMail: type: MAIL defaultReceivers: x@y.z custom: type: HTTP defaultReceivers: x@y.z args: url: https://... ``` -------------------------------- ### Dew Exception Mapping Configuration Example Source: https://github.com/gudaoxuri/dew/blob/master/docs/index.html Provides an example of configuring `java.io.IOException` to return a specific HTTP status code (200) and a business code ('NBE00123') when encountered. ```YAML dew: basic: error-mapping: "java.io.IOException": http-code: 200 business-code: "NBE00123" ``` -------------------------------- ### Example of Dew Unified Exception Handling in Java Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/framework/user-manual.adoc Illustrates how to use `Dew.E.e` to encapsulate and throw an exception in business logic. It also shows the resulting JSON response received by the client, including a custom business code and message. ```java // 业务代码捕获了一个异常 Exception someError = new IOException("xxx不存在") // 使用统一异常处理封装 throw Dew.E.e("NBE00123",someError,200) // 请求方得到的结果为 http状态=200,响应体: { "code": "NBE00123", "message": "xxx不存在", "body": null } ``` -------------------------------- ### Dew Cache Multi-instance Access Examples Source: https://github.com/gudaoxuri/dew/blob/master/docs/index.html Illustrates how to access specific Redis cache instances configured with the `multi` property using `Dew.cluster.caches.instance("")` and also shows usage of the default cache connection. ```Java // Use the connection with key 'auth' Dew.cluster.caches.instance("auth").set("token", "xxxxx"); // Use the default connection Dew.cluster.cache.set("name", "xxxxx") ``` -------------------------------- ### Initialize Kubernetes Master with Proxy Network Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/devops/install.adoc Initializes the Kubernetes master node with a specified pod network CIDR, using a proxy method. ```bash kubeadm init \ --pod-network-cidr=10.244.0.0/16 ``` -------------------------------- ### Configure Metrics Server for Kubernetes (Fixes) Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/devops/install.adoc Clones the metrics-server repository, applies 1.8+ deployment files, and patches the deployment to fix TLS and address type issues, and optionally changes the image for non-proxy environments. ```bash git clone https://github.com/kubernetes-incubator/metrics-server.git # Kuberneters V1.8以上执行 kubeadm create -f metrics-server/deploy/1.8+/ # Fix issue: https://github.com/kubernetes-incubator/metrics-server/issues/131 kubeadm patch deploy metrics-server -n kube-system -p " spec: template: spec: containers: - name: metrics-server command: - /metrics-server - --kubelet-insecure-tls - --kubelet-preferred-address-types=InternalIP" # 不使用代理,需要修改镜像 kubeadm -n kube-system set image deploy metrics-server metrics-server=rancher/metrics-server-amd64:v0.3.1 ``` -------------------------------- ### Configure Kubeconfig for Cluster Access Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/devops/install.adoc Sets up the kubeconfig file for accessing the Kubernetes cluster, copying the admin configuration and setting appropriate permissions. ```bash mkdir -p $HOME/.kube cp -i /etc/kubernetes/admin.conf $HOME/.kube/config chown $(id -u):$(id -g) $HOME/.kube/config export KUBECONFIG=/etc/kubernetes/admin.conf ``` -------------------------------- ### GitLab CI/CD Trigger Condition Source: https://github.com/gudaoxuri/dew/blob/master/devops/it/src/it/helloworld-frontend/readme.adoc This snippet describes the trigger condition for automated deployments via GitLab CI/CD. The pipeline will execute when code is merged or pushed to the 'test' branch. ```shell # Merge或Push代码到test分支 ``` -------------------------------- ### Basic Dew Cache Usage Examples Source: https://github.com/gudaoxuri/dew/blob/master/docs/index.html Demonstrates common operations using the Dew cache, including clearing the database, deleting a key, checking key existence, setting a string value with expiration, and retrieving a value, converting it to JSON. ```Java // Clear DB Dew.cluster.cache.flushdb(); // Delete key Dew.cluster.cache.del("n_test"); // Check existence Dew.cluster.cache.exists("n_test"); // Set value Dew.cluster.cache.set("n_test", "{\"name\":\"jzy\"}", 1); // Get value and convert to Json $.json.toJson(Dew.cluster.cache.get("n_test")); ``` -------------------------------- ### Manual DevOps Deployment Execution Source: https://github.com/gudaoxuri/dew/blob/master/devops/it/src/it/helloworld-frontend/readme.adoc This section provides commands for manually preparing the environment and deploying the application to the test environment using Maven. It includes setting necessary environment variables for Kubernetes, Docker host, and Docker registry credentials, followed by the Maven deploy command. ```shell # 参数设置 (win/unix) SET/export dew_devops_kube_config=... SET/export dew_devops_docker_host=... SET/export dew_devops_docker_registry_url=... SET/export dew_devops_docker_registry_username=... SET/export dew_devops_docker_registry_password=... # 执行如下maven命令(发布到test环境) mvn -P devops deploy -Ddew_devops_profile=test ``` -------------------------------- ### TodoKernelApplication Main Entry Point for Kernel Service Source: https://github.com/gudaoxuri/dew/blob/master/docs/index.html This Java class is the main entry point for the kernel service, extending `TodoParentApplication`. It uses `SpringApplicationBuilder` to run the Spring Boot application, initializing the kernel service. ```Java // 继承自TodoParentApplication public class TodoKernelApplication extends TodoParentApplication { // 启动类 public static void main(String[] args) { new SpringApplicationBuilder(TodoKernelApplication.class).run(args); } } ``` -------------------------------- ### Check K3s Cluster Status (kubectl) Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/devops/install.adoc Verifies the status of K3s nodes and pods using the newly installed `kubectl` command. ```bash kubectl get nodes -o wide kubeadm get pod -n kube-system ``` -------------------------------- ### Main Spring Boot Application for TODO Kernel Service Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/framework/quick-start.adoc Extends `TodoParentApplication` and serves as the main entry point for the TODO kernel service. It initializes and runs the Spring Boot application using `SpringApplicationBuilder`, demonstrating how to launch a specific service module. ```java // 继承自TodoParentApplication public class TodoKernelApplication extends TodoParentApplication { // 启动类 public static void main(String[] args) { new SpringApplicationBuilder(TodoKernelApplication.class).run(args); } } ``` -------------------------------- ### Adding Spring Boot HBase Starter Dependency Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/framework/user-manual.adoc Specifies the Maven dependency required to integrate HBase client capabilities with Spring Boot applications, enabling configuration management and Kerberos authentication. ```XML group.idealworld.dew hbase-starter ``` -------------------------------- ### Importing Dew Framework Dependencies via Maven Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/framework/user-manual.adoc This snippet demonstrates how to include Dew framework modules in a Maven project. It shows the parent dependency and how to add specific modules, along with developer and SCM information. The `parent-starter` simplifies version management by providing default versions for all Dew modules. ```xml group.idealworld.dew parent-starter ${dew.version} ... group.idealworld.dew 对应模块名,见下文 ... ... ... ... ... ... ``` -------------------------------- ### Install and Configure Prometheus Operator Helm Chart Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/devops/install.adoc This Helm command installs the `stable/prometheus-operator` chart, configuring Prometheus, Alertmanager, and Grafana. It sets up ingress hosts for all three components, enables persistent storage with 100Gi requests and specific label selectors, enables etcd service monitoring with TLS verification skipped, configures Grafana's admin password, default dashboards, ingress, sidecar for dashboards/datasources, and SMTP settings. All components are configured with a node selector for 'group=devops'. ```Shell helm install stable/prometheus-operator --name dew-prometheus-operator --namespace devops --version=5.0.10 --set kubelet.serviceMonitor.https=true --set prometheus.ingress.enabled=true --set prometheus.ingress.hosts={prometheus.dew.idealworld.group} --set alertmanager.ingress.enabled=true --set alertmanager.ingress.hosts={alertmanager.dew.idealworld.group} --set prometheusOperator.securityContext.runAsNonRoot=false --set prometheus.prometheusSpec.storageSpec.volumeClaimTemplate.spec.resources.requests.storage=100Gi --set alertmanager.alertmanagerSpec.storage.volumeClaimTemplate.spec.resources.requests.storage=100Gi --set prometheus.prometheusSpec.storageSpec.volumeClaimTemplate.spec.selector.matchLabels."component"="prometheus" --set alertmanager.alertmanagerSpec.storage.volumeClaimTemplate.spec.selector.matchLabels."component"="alertmanager" --set prometheus.prometheusSpec.secrets[0]=dew-prometheus-operator-etcd --set kubeEtcd.serviceMonitor.scheme=https --set kubeEtcd.serviceMonitor.insecureSkipVerify=true --set kubeEtcd.serviceMonitor.caFile="/etc/prometheus/secrets/dew-prometheus-operator-etcd/ca.crt" --set kubeEtcd.serviceMonitor.certFile="/etc/prometheus/secrets/dew-prometheus-operator-etcd/peer.crt" --set kubeEtcd.serviceMonitor.keyFile="/etc/prometheus/secrets/dew-prometheus-operator-etcd/peer.key" --set grafana.enabled=true --set grafana.adminPassword=Dew123456 --set grafana.defaultDashboardsEnabled=true --set grafana.ingress.enabled=true --set grafana.ingress.hosts={grafana.dew.idealworld.group} --set grafana.sidecar.dashboards.enabled=true --set grafana.sidecar.dashboards.searchNamespace="devops" --set grafana.sidecar.dashboards.label=grafana_dashboard --set grafana.sidecar.datasources.enabled=true --set grafana.sidecar.datasources.searchNamespace="devops" --set grafana.sidecar.datasources.label=grafana_datasource --set grafana.'grafana\\.ini'.smtp.enabled="true" --set grafana.'grafana\\.ini'.smtp.host="smtp.163.com:25" --set grafana.'grafana\\.ini'.smtp.user=XXXXX@163.com --set grafana.'grafana\\.ini'.smtp.password=XXXXX --set grafana.'grafana\\.ini'.smtp.from_address="XXXXX@163.com" --set grafana.'grafana\\.ini'.smtp.skip_verify=true --set grafana.'grafana\\.ini'.server.root_url="http://grafana.dew.idealworld.group" --set grafana.persistence.enabled=true --set grafana.persistence.existingClaim=grafana --set prometheusOperator.nodeSelector.group=devops --set alertmanager.alertmanagerSpec.nodeSelector.group=devops --set prometheus.prometheusSpec.nodeSelector.group=devops --set kube-state-metrics.nodeSelector.group=devops --set grafana.nodeSelector.group=devops ``` -------------------------------- ### Common Library Maven Dependencies for Dew Boot Starter and JPA Source: https://github.com/gudaoxuri/dew/blob/master/docs/index.html This `pom.xml` snippet for the common library module illustrates the inclusion of `boot-starter` for core Dew framework capabilities and `spring-boot-starter-data-jpa` for Spring Data JPA support, enabling database interaction. ```XML group.idealworld.dew boot-starter org.springframework.boot spring-boot-starter-data-jpa ``` -------------------------------- ### Usage Examples of Dew Common Utilities Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/framework/user-manual.adoc This snippet demonstrates how to use various utility functions provided by the `Dew-Common` package. These include converting JSON to Java objects, copying Bean properties, performing asymmetric encryption, making HTTP GET requests, and validating mobile phone numbers. All `Dew Common` functions are accessed through the `$` prefix. ```java // Dew Common 功能均以 $ 开始,如: //Json转成Java对象: $.json.toObject(json,JavaModel.class) //Json字符串转成List对象 $.json.toList(jsonArray, JavaModel.class) //Bean复制 $.bean.copyProperties(ori, dist) //获取Class的注解信息 $.bean.getClassAnnotation(IdxController.class, TestAnnotation.RPC.class) //非对称加密 $.encrypt.Asymmetric.encrypt(d.getBytes("UTF-8"), publicKey, 1024, "RSA") //Http Get $.http.get("https://httpbin.org/get") //验证手机号格式是否合法 $.field.validateMobile("18657120000") //... ``` -------------------------------- ### Root Maven POM Configuration for Dew Parent Starter Source: https://github.com/gudaoxuri/dew/blob/master/docs/index.html This snippet shows the root `pom.xml` file, demonstrating how to inherit from Dew's `parent-starter`. This parent POM provides common dependencies and build configurations for Dew-based projects. ```XML group.idealworld.dew parent-starter ... ``` -------------------------------- ### Maven Dependency for Dew Cluster Module Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/framework/user-manual.adoc To enable Dew's cluster functionalities, such as distributed caching, maps, locks, and messaging, the `boot-starter` module must be included as a Maven dependency. This module provides the foundational components for integrating Dew's cluster capabilities into your application. ```xml group.idealworld.dew boot-starter ``` -------------------------------- ### Java Example for Leader Election Service Source: https://github.com/gudaoxuri/dew/blob/master/docs/index.html This Java example illustrates how to use Dew's ClusterElection service. It demonstrates instantiating a typed leader election service (e.g., 'fun1') and then conditionally executing code blocks only if the current node is identified as the leader for that specific election type, enabling distributed coordination. ```Java // 实例化fun1类型的领导者选举,Redis的实现支持多类型领导者 ClusterElection electionFun1 = Dew.cluster.election.instance("fun1"); // ... if (electionFun1.isLeader()) { // 当前节点是fun1类型的领导者 // ... } ``` -------------------------------- ### Check Kubernetes Cluster Component Status Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/devops/install.adoc Verifies the status of Kubernetes cluster components. ```bash kubectl get cs ``` -------------------------------- ### Retrieve K3s Server Token Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/devops/install.adoc Retrieves the K3s server token, which is necessary for agent nodes to join the cluster. ```bash cat /var/lib/rancher/k3s/server/token ``` -------------------------------- ### Scale Application Replicas Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/devops/user-manual.adoc Outlines the Maven command for scaling an application, including parameters for setting the number of replicas, enabling auto-scaling, and configuring auto-scaling limits. ```bash # 基础命令 mvn -P devops dew:scale # ====================== 附加参数 ====================== # dew_devops_scale_replicas # 伸缩Pod数量 # dew_devops_scale_auto # 是否启用自动伸缩 # dew_devops_scale_auto_minReplicas # 自动伸缩Pod数下限 ``` -------------------------------- ### Configure Docker for K3s Compatibility Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/devops/install.adoc Modifies Docker daemon configuration to use `cgroupfs` as the cgroup driver, which is required for K3s. ```bash # Docker 修改 # K3s使用的是cgroupfs # 去掉 /etc/docker/daemon.json 中的 "exec-opts": ["native.cgroupdriver=systemd"] systemctl daemon-reload systemctl restart docker ``` -------------------------------- ### Create Kubernetes Namespaces Source: https://github.com/gudaoxuri/dew/blob/master/docs/src/main/asciidoc/_chapter/devops/install.adoc Creates new namespaces (e.g., 'nginx-ingress', 'devops') within the Kubernetes cluster for logical separation of resources. ```bash cat <