<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>九千万代码的梦想</title>
    <description>这个是卡尔的个人博客站，仅用来发表卡尔的个人博客，有技术有非技术的博客，欢迎大家有空来逛逛。</description>
    <link>http://kael-aiur.com/</link>
    <atom:link href="http://kael-aiur.com/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Thu, 20 Nov 2025 14:47:05 +0000</pubDate>
    <lastBuildDate>Thu, 20 Nov 2025 14:47:05 +0000</lastBuildDate>
    <generator>Jekyll v3.10.0</generator>
    
      <item>
        <title>服务器部署mihomo</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#前言&quot; id=&quot;markdown-toc-前言&quot;&gt;前言&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#目标&quot; id=&quot;markdown-toc-目标&quot;&gt;目标&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#部署&quot; id=&quot;markdown-toc-部署&quot;&gt;部署&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#clashclash-meta和mihomo简介&quot; id=&quot;markdown-toc-clashclash-meta和mihomo简介&quot;&gt;clash、clash meta和mihomo简介&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#环境准备&quot; id=&quot;markdown-toc-环境准备&quot;&gt;环境准备&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#docker-compose部署&quot; id=&quot;markdown-toc-docker-compose部署&quot;&gt;docker compose部署&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#配置文件准备&quot; id=&quot;markdown-toc-配置文件准备&quot;&gt;配置文件准备&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#更新订阅脚本&quot; id=&quot;markdown-toc-更新订阅脚本&quot;&gt;更新订阅脚本&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#设置定时任务&quot; id=&quot;markdown-toc-设置定时任务&quot;&gt;设置定时任务&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#启动服务&quot; id=&quot;markdown-toc-启动服务&quot;&gt;启动服务&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#使用&quot; id=&quot;markdown-toc-使用&quot;&gt;使用&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#web-ui管理&quot; id=&quot;markdown-toc-web-ui管理&quot;&gt;Web UI管理&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#chrome插件&quot; id=&quot;markdown-toc-chrome插件&quot;&gt;Chrome插件&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#电脑系统代理&quot; id=&quot;markdown-toc-电脑系统代理&quot;&gt;电脑系统代理&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#手机设置&quot; id=&quot;markdown-toc-手机设置&quot;&gt;手机设置&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;前言&quot;&gt;前言&lt;/h2&gt;

&lt;p&gt;我主要是因为需要搭建一个代理服务的环境，用来配置代理服务进行科学上网。现在有很多clash的客户端，但是他们都需要在设备上安装软件，而我期望的方式是直接设置代理服务即可，无需在每台设备上安装clash的客户端，所以要部署在内网服务器作为代理网关。这样所有设备只需要配置代理地址，就能使用统一的代理服务。&lt;/p&gt;

&lt;h2 id=&quot;目标&quot;&gt;目标&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;使用订阅连接自动更新路由配置，需要定期更新，并且支持简单快捷的切换订阅连接地址&lt;/li&gt;
  &lt;li&gt;带ui控制台，方便管理&lt;/li&gt;
  &lt;li&gt;部署在内网服务器，无需公开订阅连接，客户端只需要设置代理地址，配置账号密码即可使用代理&lt;/li&gt;
  &lt;li&gt;在linux服务器上使用命令行部署，通过docker容器化运行&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;部署&quot;&gt;部署&lt;/h2&gt;

&lt;h3 id=&quot;clashclash-meta和mihomo简介&quot;&gt;clash、clash meta和mihomo简介&lt;/h3&gt;

&lt;p&gt;Clash 是一款用 Go 编写的基于规则的代理工具，最初由 Dreamacro 开发。它支持多种代理协议，包括 Shadowsocks、VMess、Trojan 等。&lt;/p&gt;

&lt;p&gt;Clash Meta（现更名为 Mihomo）是基于 Clash 开源项目的二次开发版本。它继承了 Clash 的核心功能并增加了一些独特的特性，包括部分原 Clash Premium 核心的功能。相比于 Clash，Clash Meta/Mihomo 引入了更多协议支持（如 VLESS XTLS、Trojan XTLS、Hysteria 等）和更丰富的规则控制。&lt;/p&gt;

&lt;p&gt;在 2023 年经历了 Clash for Windows 删库事件之后，原 Clash 项目停止更新，于是开发者将 Clash Meta 改名为 Mihomo，继续进行维护和更新。&lt;/p&gt;

&lt;h3 id=&quot;环境准备&quot;&gt;环境准备&lt;/h3&gt;

&lt;p&gt;首先我们需要准备部署环境和创建工作目录：&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# 创建工作目录&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;mkdir&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; ~/workspace/mihomo/config
&lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; ~/workspace/mihomo
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;docker-compose部署&quot;&gt;docker compose部署&lt;/h3&gt;

&lt;p&gt;创建 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker-compose.yml&lt;/code&gt; 文件：&lt;/p&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;na&quot;&gt;services&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# 主体服务&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;mihomo&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;container_name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;mihomo&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;metacubex/mihomo&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;restart&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;always&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;ports&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;7890:7890&quot;&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;7891:7891&quot;&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;9090:9090&quot;&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;volumes&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;./config:/root/.config/mihomo&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# ui站点&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;metacubexd&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;container_name&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;metacubexd&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;image&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;ghcr.io/metacubex/metacubexd&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;restart&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;always&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;ports&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;9097:80&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;配置文件准备&quot;&gt;配置文件准备&lt;/h3&gt;

&lt;p&gt;创建基础配置文件 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;config/config.yaml&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;现在的配置托管地址通常都是包含完整的clash配置文件，因此我们只需要从订阅地址直接下载：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$&amp;gt; curl -s -H &quot;User-Agent: clash&quot; &quot;https://clash.example.top/subscribe&quot; -o ./config/config.yaml
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;注意：有些服务的订阅地址，会根据User-Agent的请求头来决定返回的内容，比如我用的订阅服务，默认情况下只返回节点列表的base64结果，而加上User-Agent: clash请求头则可以返回完整的clash配置。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;更新订阅脚本&quot;&gt;更新订阅脚本&lt;/h3&gt;

&lt;p&gt;创建订阅更新脚本 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;update.sh&lt;/code&gt;：&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;#! /bin/bash&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;export &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;converter_url&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;https://clash.example.top/subscribe&quot;&lt;/span&gt;
curl &lt;span class=&quot;nt&quot;&gt;-H&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;User-Agent: clash&quot;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$converter_url&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; ~/workspace/mihomo/config/config.yaml
&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;secret: Pass@word123&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; ~/workspace/mihomo/config/config.yaml
&lt;span class=&quot;nb&quot;&gt;sed&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-i&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;s/127.0.0.1:9090/0.0.0.0:9090/&apos;&lt;/span&gt; ~/workspace/mihomo/config/config.yaml
&lt;span class=&quot;nb&quot;&gt;sed&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-i&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;s/port: 7890/port: 0/&apos;&lt;/span&gt; ~/workspace/mihomo/config/config.yaml
&lt;span class=&quot;nb&quot;&gt;sed&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-i&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;s/socks-port: 7891/socks-port: 0/&apos;&lt;/span&gt; ~/workspace/mihomo/config/config.yaml
&lt;span class=&quot;nb&quot;&gt;sed&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-i&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;s/mixed-port: 0/mixed-port: 7890/&apos;&lt;/span&gt; ~/workspace/mihomo/config/config.yaml
&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;authentication: &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; ~/workspace/mihomo/config/config.yaml
&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;- &quot;admin:pass@word&quot;&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; ~/workspace/mihomo/config/config.yaml

curl &lt;span class=&quot;nt&quot;&gt;-ik&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-s&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-X&lt;/span&gt; PUT &lt;span class=&quot;s2&quot;&gt;&quot;http://127.0.0.1:9090/configs&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-H&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Content-Type: application/json&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-H&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Authorization: Bearer Pass@word123&quot;&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-d&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;{&quot;path&quot;: &quot;/root/.config/mihomo/config.yaml&quot;}&apos;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;这里的订阅更新脚本做了几件事：&lt;/p&gt;
  &lt;ol&gt;
    &lt;li&gt;从订阅链接下载新的订阅配置&lt;/li&gt;
    &lt;li&gt;在配置中加入secret配置，指定管理密钥&lt;/li&gt;
    &lt;li&gt;将外部控制绑定地址从127.0.0.1:9090改为0.0.0.0:9090&lt;/li&gt;
    &lt;li&gt;将代理端口从7890改为0,socks-port端口从7891改为0，mixed-port端口改为7890，这几个端口分别是http代理端口，socks5代理端口，以及混合代理端口&lt;/li&gt;
    &lt;li&gt;设置代理认证账号密码，客户端需要使用账号密码才能使用代理，不会随便被人使用&lt;/li&gt;
    &lt;li&gt;触发mihomo的核心服务更新配置&lt;/li&gt;
  &lt;/ol&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;设置定时任务&quot;&gt;设置定时任务&lt;/h3&gt;

&lt;p&gt;为了让配置自动更新，我们需要添加一个cron任务。&lt;/p&gt;

&lt;p&gt;执行cron命令：&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$&amp;gt; crontab -e
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;进入cron任务编辑，输入如下cron表达式：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0 2 * * * ~/workspace/mihomo/update.sh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这个表达式表示每天凌晨2点定期执行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/workspace/mihomo/update.sh&lt;/code&gt;脚本，这样就可以通过脚本定期更新配置文件和刷新核心服务配置了。&lt;/p&gt;

&lt;h3 id=&quot;启动服务&quot;&gt;启动服务&lt;/h3&gt;

&lt;p&gt;现在只需要在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/workspace/mihomo&lt;/code&gt;目录下，执行如下命令即可启动mihomo：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;docker compose up -d
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;使用&quot;&gt;使用&lt;/h2&gt;

&lt;h3 id=&quot;web-ui管理&quot;&gt;Web UI管理&lt;/h3&gt;

&lt;p&gt;首先通过web ui查看服务的情况。&lt;/p&gt;

&lt;p&gt;在浏览器输入地址：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;http://127.0.0.1:9097
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;即可看到如下界面：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/static/img/blog/mihomo/mihomo_1.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;输入后端地址：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;http://127.0.0.1:9090&lt;/code&gt;，密钥&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pass@word123&lt;/code&gt;即可链接核心服务：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/static/img/blog/mihomo/mihomo_2.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;chrome插件&quot;&gt;Chrome插件&lt;/h3&gt;

&lt;p&gt;chrome的插件Zero Omega可以非常方便的配置和切换代理。&lt;/p&gt;

&lt;p&gt;安装地址&lt;a href=&quot;https://chromewebstore.google.com/detail/proxy-switchyomega-3-zero/pfnededegaaopdmhkdmcofjmoldfiped?hl=en-US&amp;amp;utm_source=ext_sidebar&quot;&gt;Zero Omega&lt;/a&gt;。&lt;/p&gt;

&lt;p&gt;安装后进入选项页：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/static/img/blog/mihomo/mihomo_3.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;添加一个profile：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/static/img/blog/mihomo/mihomo_4.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;选择Proxy Profile：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/static/img/blog/mihomo/mihomo_5.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;输入代理地址：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/static/img/blog/mihomo/mihomo_6.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;点击应用即可：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/static/img/blog/mihomo/mihomo_7.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;如果你的服务端跟我一样设置了密码，那在输入代理的时候，还需要点击代理地址右边的小锁头图标，输入账号密码才能使用。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;设置完成后，只需要在chrome的插件图标中点击Zero Omega，选择mihomo的代理即可让浏览器使用代理上网了。&lt;/p&gt;

&lt;h3 id=&quot;电脑系统代理&quot;&gt;电脑系统代理&lt;/h3&gt;

&lt;p&gt;如果希望在整个电脑级别使用代理，可以将mihomo设置为系统代理，不同操作系统设置系统代理的方式不一样，这里以mac为例。&lt;/p&gt;

&lt;p&gt;打开&lt;strong&gt;设置&lt;/strong&gt; -&amp;gt; &lt;strong&gt;网络&lt;/strong&gt; -&amp;gt; &lt;strong&gt;详情&lt;/strong&gt;（当前网络下）：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/static/img/blog/mihomo/mihomo_8.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;选择代理菜单：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/static/img/blog/mihomo/mihomo_9.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;保存即可。&lt;/p&gt;

&lt;p&gt;其他操作系统原理是一样的，自行搜索如何设置即可。&lt;/p&gt;

&lt;h3 id=&quot;手机设置&quot;&gt;手机设置&lt;/h3&gt;

&lt;p&gt;如果你使用手机连接家里的wifi，则可以在手机的wifi设置代理即可，具体的设置方法可以执行百度。&lt;/p&gt;
</description>
        <pubDate>Wed, 19 Nov 2025 00:00:00 +0000</pubDate>
        <link>http://kael-aiur.com/%E5%B7%A5%E5%85%B7%E4%BD%BF%E7%94%A8/%E6%9C%8D%E5%8A%A1%E5%99%A8%E9%83%A8%E7%BD%B2mihomo.html</link>
        <guid isPermaLink="true">http://kael-aiur.com/%E5%B7%A5%E5%85%B7%E4%BD%BF%E7%94%A8/%E6%9C%8D%E5%8A%A1%E5%99%A8%E9%83%A8%E7%BD%B2mihomo.html</guid>
        
        <category>clash,mihomo</category>
        
        
        <category>工具使用</category>
        
      </item>
    
      <item>
        <title>ai编程工具汇总</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#前言&quot; id=&quot;markdown-toc-前言&quot;&gt;前言&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#claude-code&quot; id=&quot;markdown-toc-claude-code&quot;&gt;Claude Code&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#安装方法&quot; id=&quot;markdown-toc-安装方法&quot;&gt;安装方法&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#使用方法&quot; id=&quot;markdown-toc-使用方法&quot;&gt;使用方法&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#评价&quot; id=&quot;markdown-toc-评价&quot;&gt;评价&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#claude-code--ccr&quot; id=&quot;markdown-toc-claude-code--ccr&quot;&gt;Claude Code + ccr&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#安装方法-1&quot; id=&quot;markdown-toc-安装方法-1&quot;&gt;安装方法&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#使用方法-1&quot; id=&quot;markdown-toc-使用方法-1&quot;&gt;使用方法&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#评价-1&quot; id=&quot;markdown-toc-评价-1&quot;&gt;评价&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#claude-code--kimi&quot; id=&quot;markdown-toc-claude-code--kimi&quot;&gt;Claude Code + Kimi&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#使用方法-2&quot; id=&quot;markdown-toc-使用方法-2&quot;&gt;使用方法&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#评价-2&quot; id=&quot;markdown-toc-评价-2&quot;&gt;评价&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#cursor&quot; id=&quot;markdown-toc-cursor&quot;&gt;Cursor&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#使用方法-3&quot; id=&quot;markdown-toc-使用方法-3&quot;&gt;使用方法&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#评价-3&quot; id=&quot;markdown-toc-评价-3&quot;&gt;评价&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#gemini-cli&quot; id=&quot;markdown-toc-gemini-cli&quot;&gt;gemini-cli&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#安装方法-2&quot; id=&quot;markdown-toc-安装方法-2&quot;&gt;安装方法&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#使用方法-4&quot; id=&quot;markdown-toc-使用方法-4&quot;&gt;使用方法&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#评价-4&quot; id=&quot;markdown-toc-评价-4&quot;&gt;评价&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#qwen-code&quot; id=&quot;markdown-toc-qwen-code&quot;&gt;qwen code&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#安装方法-3&quot; id=&quot;markdown-toc-安装方法-3&quot;&gt;安装方法&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#使用方法-5&quot; id=&quot;markdown-toc-使用方法-5&quot;&gt;使用方法&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#评价-5&quot; id=&quot;markdown-toc-评价-5&quot;&gt;评价&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#codex&quot; id=&quot;markdown-toc-codex&quot;&gt;Codex&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#安装方法-4&quot; id=&quot;markdown-toc-安装方法-4&quot;&gt;安装方法&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#使用方法-6&quot; id=&quot;markdown-toc-使用方法-6&quot;&gt;使用方法&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#评价-6&quot; id=&quot;markdown-toc-评价-6&quot;&gt;评价&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#trea&quot; id=&quot;markdown-toc-trea&quot;&gt;trea&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#使用方法-7&quot; id=&quot;markdown-toc-使用方法-7&quot;&gt;使用方法&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#评价-7&quot; id=&quot;markdown-toc-评价-7&quot;&gt;评价&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#总结&quot; id=&quot;markdown-toc-总结&quot;&gt;总结&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;前言&quot;&gt;前言&lt;/h2&gt;

&lt;p&gt;AI编程助手已经成为开发者不可获取的提效工具，本文主要给大家介绍对比市面上各种常用的编程助手。&lt;/p&gt;

&lt;h2 id=&quot;claude-code&quot;&gt;Claude Code&lt;/h2&gt;

&lt;p&gt;Claude Code 是由 Anthropic 推出的 AI 编程助手，基于 Claude 大语言模型。它支持多种编程语言，能够帮助开发者自动补全代码、生成函数、解释代码逻辑、修复 bug 以及编写单元测试。Claude Code 强调安全性和可靠性，适合在敏感或高要求的项目中使用。其界面简洁，支持与主流 IDE 集成，提升开发效率，适合个人开发者和团队协作。&lt;/p&gt;

&lt;p&gt;claude code本身是开源的，项目地址：&lt;a href=&quot;https://github.com/anthropics/claude-code&quot;&gt;anthropics/claude-code&lt;/a&gt;。&lt;/p&gt;

&lt;p&gt;官网文档地址：&lt;a href=&quot;https://docs.claude.com/en/docs/claude-code/overview&quot;&gt;Claude Docs&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;安装方法&quot;&gt;安装方法&lt;/h3&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$&amp;gt;&lt;/span&gt; npm &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-g&lt;/span&gt; @anthropic-ai/claude-code
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;claude code依赖nodejs，因为运行前需要安装nodejs，具体安装方法可以参考官网：&lt;a href=&quot;https://nodejs.org/zh-cn/download&quot;&gt;NodeJs&lt;/a&gt;。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;使用方法&quot;&gt;使用方法&lt;/h3&gt;

&lt;p&gt;Claude Code是命令行工具，直接通过命令行启动：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$&amp;gt;&lt;/span&gt; claude
╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│                                                                                                                                                                                                                 │
│ Do you trust the files &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;this folder?                                                                                                                                                                          │
│                                                                                                                                                                                                                 │
│ /home/kael                                                                                                                                                                                                      │
│                                                                                                                                                                                                                 │
│ Claude Code may &lt;span class=&quot;nb&quot;&gt;read&lt;/span&gt;, write, or execute files contained &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;this directory. This can pose security risks, so only use files from trusted sources.                                                                │
│                                                                                                                                                                                                                 │
│ Learn more &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt; https://docs.claude.com/s/claude-code-security &lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;                                                                                                                                                   │
│                                                                                                                                                                                                                 │
│ ❯ 1. Yes, proceed                                                                                                                                                                                               │
│   2. No, &lt;span class=&quot;nb&quot;&gt;exit&lt;/span&gt;                                                                                                                                                                                                   │
│                                                                                                                                                                                                                 │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
   Enter to confirm · Esc to &lt;span class=&quot;nb&quot;&gt;exit&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;其中内置了很多命令,可以输入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/&lt;/code&gt;获得提示：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;╭───────────────────────────────────────────────────╮
│ ✻ Welcome to Claude Code!                         │
│                                                   │
│   /help &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;help&lt;/span&gt;, /status &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;your current setup  │
│                                                   │
│   cwd: /home/kael                                 │
│                                                   │
│   ─────────────────────────────────────────────── │
│                                                   │
│   Overrides &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;via &lt;span class=&quot;nb&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;:                            │
│                                                   │
│   • API Key: kae…                                 │
│   • API Base URL: https://claude.server.com       │
╰───────────────────────────────────────────────────╯

 Tips &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;getting started:

  Run /init to create a CLAUDE.md file with instructions &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;Claude
  Use Claude to &lt;span class=&quot;nb&quot;&gt;help &lt;/span&gt;with file analysis, editing, bash commands and git
  Be as specific as you would with another engineer &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;the best results

 Note: You have launched claude &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;your home directory. For the best experience, launch it &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;a project directory instead.

───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; / 
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
  /add-dir                Add a new working directory
  /agents                 Manage agent configurations
  /bashes                 List and manage background tasks
  /clear &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;reset, new&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;     Clear conversation &lt;span class=&quot;nb&quot;&gt;history &lt;/span&gt;and free up context
  /compact                Clear conversation &lt;span class=&quot;nb&quot;&gt;history &lt;/span&gt;but keep a summary &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;context. Optional: /compact &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;instructions &lt;span class=&quot;k&quot;&gt;for &lt;/span&gt;summarization]
  /config &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;theme&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;         Open config panel
  /context                Visualize current context usage as a colored grid
  /cost                   Show the total cost and duration of the current session
  /doctor                 Diagnose and verify your Claude Code installation and settings
  /exit &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;quit&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;            Exit the REPL
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;输入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@&lt;/code&gt;符号可以选择文件：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
&amp;gt; @ 
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
  .SmartTomcat/
  .SwitchHosts/
  .TranslationPlugin/
  .android/
  .arthas/
  .bash_history
  .bash_logout
  .bash_profile
  .bashrc
  .bin/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;评价&quot;&gt;评价&lt;/h3&gt;

&lt;p&gt;优点：&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Claude Code是目前业内评价最好的AI编程助手之一，配合Claude相关模型效果非常优秀&lt;/li&gt;
  &lt;li&gt;支持安装idea和vs code插件实现联动，使用体验优秀&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;缺点：&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;价格偏贵，对token使用的优化较差，相同的任务使用的token数更多&lt;/li&gt;
  &lt;li&gt;Anthropic公司视中国为敌对国，封禁中国用户，付费困难&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;claude-code--ccr&quot;&gt;Claude Code + ccr&lt;/h2&gt;

&lt;p&gt;claude code router（简称ccr）是国人开发的一个开源适配服务，其主要作用是降低Claude Code的使用门槛。&lt;/p&gt;

&lt;p&gt;项目地址： &lt;a href=&quot;https://github.com/musistudio/claude-code-router&quot;&gt;claude-code-router&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;ccr的 原理是适配Anthropic的api接口协议，转换为openai的接口协议，然后将claude code的baseurl替换为ccr的地址来实现代替官方的后端。并且支持替换其他模型，从而减少模型费用。&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;使用ccr之前需要先安装claude code&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;安装方法-1&quot;&gt;安装方法&lt;/h3&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$&amp;gt;&lt;/span&gt; npm &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-g&lt;/span&gt; @musistudio/claude-code-router
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;使用方法-1&quot;&gt;使用方法&lt;/h3&gt;

&lt;p&gt;安装之后，可以在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.claude-code-router/&lt;/code&gt;目录下创建一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;config.json&lt;/code&gt;文件对ccr进行配置，也可以使用ccr提供的ui进行配置。&lt;/p&gt;

&lt;p&gt;配置完成后使用ccr启动claude code：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$&amp;gt; ccr code
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这里会设置环境变量并启动claude code，使得你的claude服务端变成ccr的服务入口：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;╭───────────────────────────────────────────────────╮
│ ✻ Welcome to Claude Code!                         │
│                                                   │
│   /help for help, /status for your current setup  │
│                                                   │
│   cwd: /home/kael                                 │
│                                                   │
│   ─────────────────────────────────────────────── │
│                                                   │
│   Overrides (via env):                            │
│                                                   │
│   • API Key: kae…                                 │
│   • API Base URL: http://127.0.0.1:3456           │
╰───────────────────────────────────────────────────╯

 Tips for getting started:

  Run /init to create a CLAUDE.md file with instructions for Claude
  Use Claude to help with file analysis, editing, bash commands and git
  Be as specific as you would with another engineer for the best results

 Note: You have launched claude in your home directory. For the best experience, launch it in a project directory instead.

───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
&amp;gt; 
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;直接访问&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;http://127.0.0.1:3456/ui&lt;/code&gt;即可打开配置页面：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/static/img/blog/aicode/2025-09-29-ccrui.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;评价-1&quot;&gt;评价&lt;/h3&gt;

&lt;p&gt;使用ccr本质上还是使用claude code。&lt;/p&gt;

&lt;p&gt;优点：&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;可以通过&lt;a href=&quot;https://openrouter.ai&quot;&gt;OpenRouter&lt;/a&gt;等渠道进行付费，实现Claude Code + Claude的最强组合，使用门槛低&lt;/li&gt;
  &lt;li&gt;可以自主替换低成本模型，实现效果和成本的平衡&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;缺点：&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;需要多启动一个本地进程，且只能使用API方式，价格按照Token使用量计算，重度使用价格比订阅更贵&lt;/li&gt;
  &lt;li&gt;需要学习和理解ccr的转换器配置等功能，学习成本高&lt;/li&gt;
  &lt;li&gt;ccr目前功能还不够丰富完善，只能个人使用&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;claude-code--kimi&quot;&gt;Claude Code + Kimi&lt;/h2&gt;

&lt;p&gt;Kimi 是由月之暗面（Moonshot AI）推出的大型语言模型，专注于中文和多语言场景，具备强大的代码理解与生成能力。Kimi 支持多种主流编程语言，能够自动补全代码、生成函数、解释代码逻辑、修复 bug 以及编写单元测试。其模型在长文本处理和复杂任务推理方面表现优异，适合开发者在实际项目中进行代码辅助和智能问答。Kimi 提供 API 接口和网页端，易于集成到各类开发工具和工作流中，目前Kimi也兼容了Anthropic的API接口，可以代替Claude Code的后端。&lt;/p&gt;

&lt;h3 id=&quot;使用方法-2&quot;&gt;使用方法&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;首先安装Claude Code。&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$&amp;gt;&lt;/span&gt; npm &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-g&lt;/span&gt; @anthropic-ai/claude-code
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ol&gt;
  &lt;li&gt;然后生成一个kimi的apikey：
访问&lt;a href=&quot;https://platform.moonshot.cn/console/api-keys&quot;&gt;用户中心&lt;/a&gt;
创建一个新的apikey即可。
    &lt;blockquote&gt;
      &lt;p&gt;apikey只能在创建的时候复制，之后无法再看到了，要自己保存好&lt;/p&gt;
    &lt;/blockquote&gt;
  &lt;/li&gt;
  &lt;li&gt;在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.claude/settings.json&lt;/code&gt;中配置：&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-json highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;env&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;ANTHROPIC_BASE_URL&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;https://api.moonshot.cn/anthropic&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;ANTHROPIC_API_KEY&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;{kimi api key}&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;完成配置后即可启动：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$&amp;gt; claude
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;参考&lt;a href=&quot;https://platform.moonshot.cn/docs/guide/agent-support#%E4%BD%BF%E7%94%A8%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9&quot;&gt;在 software agents 中使用 kimi k2 模型&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;评价-2&quot;&gt;评价&lt;/h3&gt;

&lt;p&gt;Claude Code + Kimi的使用方式进一步降低了门槛，据网上的评价，可以达到使用Claude模型的70%左右的效果，但是价格会便宜许多。&lt;/p&gt;

&lt;p&gt;价格对比&lt;/p&gt;

&lt;table&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;模型&lt;/td&gt;
      &lt;td&gt;每百万输入token&lt;/td&gt;
      &lt;td&gt;每百万输出token&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Kimi K2&lt;/td&gt;
      &lt;td&gt;命中缓存： 1元，未命中：4元&lt;/td&gt;
      &lt;td&gt;16元&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;kimi-k2-turbo-preview&lt;/td&gt;
      &lt;td&gt;命中缓存： 4元，未命中：16元&lt;/td&gt;
      &lt;td&gt;64元&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Opus 4.1&lt;/td&gt;
      &lt;td&gt;命中缓存：1.5$，未命中 ：15$&lt;/td&gt;
      &lt;td&gt;命中缓存：18.75$，未命中 ：75$&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Sonnet 4&lt;/td&gt;
      &lt;td&gt;命中缓存：&lt;br /&gt;0.3$(token &amp;lt;= 200k)&lt;br /&gt;0.6$(token &amp;gt; 200k)&lt;br /&gt;未命中 ：&lt;br /&gt;3$(token &amp;lt;= 200k)&lt;br /&gt;6$(token &amp;gt; 200k)&lt;/td&gt;
      &lt;td&gt;命中缓存：&lt;br /&gt;3.75$(token &amp;lt;= 200k)&lt;br /&gt;7.5$(token &amp;gt; 200k)&lt;br /&gt;未命中 ：&lt;br /&gt;15$(token &amp;lt;= 200k)&lt;br /&gt;22.5$(token &amp;gt; 200k)&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;官网价格说明：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://claude.com/pricing#api&quot;&gt;Claude&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://platform.moonshot.cn/docs/pricing/chat#%E4%BA%A7%E5%93%81%E5%AE%9A%E4%BB%B7&quot;&gt;Kimi&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;2025年9月16日-10月15日官方kimi-k2-turbo-preview 模型限时折扣 50%，10月16日恢复原价&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;优点：&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;充值使用方便，kimi k2主要卖点也是编程能力，价格便宜&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;缺点：&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;效果不如Claude，并且只能使用Kimi模型&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;cursor&quot;&gt;Cursor&lt;/h2&gt;

&lt;p&gt;Cursor 是一款专为开发者设计的 AI 编程助手和智能代码编辑器。它集成了主流大语言模型（如 GPT-4、Claude、Kimi 等），支持自动补全、代码生成、智能重构、代码解释和单元测试生成等功能。Cursor 提供类 IDE 的界面，兼容 VS Code 插件生态，支持多种编程语言和项目类型。其强大的 AI 能力能够显著提升开发效率，适合个人开发者和团队协作。Cursor 支持本地和云端运行，用户可根据需求选择不同模型和服务，灵活性高，体验优秀。&lt;/p&gt;

&lt;h3 id=&quot;使用方法-3&quot;&gt;使用方法&lt;/h3&gt;

&lt;p&gt;直接通过官网下载cursor：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://cursor.com/cn/download&quot;&gt;Cursor&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;下载后安装即可。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/static/img/blog/aicode/2025-09-29-cursor.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Cursor是基于vs code开发的，按照vs code的习惯使用即可。&lt;/p&gt;

&lt;h3 id=&quot;评价-3&quot;&gt;评价&lt;/h3&gt;

&lt;p&gt;优点：&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;使用门槛低，充值方便&lt;/li&gt;
  &lt;li&gt;可以使用cursor等优秀模型，效果非常好&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;缺点：&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;价格偏贵，免费体验期结束后，20$每月限量使用，不限量使用要200$每月&lt;/li&gt;
  &lt;li&gt;基于vs code使用，对java的项目支持体验不如idea&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;gemini-cli&quot;&gt;gemini-cli&lt;/h2&gt;

&lt;p&gt;Gemini CLI 是一款开源 AI 代理，可将 Gemini 的强大功能直接引入您的终端。它提供对 Gemini 的轻量级访问，为您提供从终端提示到我们模型的最直接路径。&lt;/p&gt;

&lt;p&gt;项目地址：&lt;a href=&quot;https://github.com/google-gemini/gemini-cli&quot;&gt;gemini-cli&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;安装方法-2&quot;&gt;安装方法&lt;/h3&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$&amp;gt;&lt;/span&gt; npm &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-g&lt;/span&gt; @google/gemini-cli
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;使用方法-4&quot;&gt;使用方法&lt;/h3&gt;

&lt;p&gt;启动：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$&amp;gt;&lt;/span&gt; gemini
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;启动后会可以选择使用google账号登录（Login with Google）或者使用Gemini API Key（Use Gemini API Key）的方式验证。&lt;/p&gt;

&lt;p&gt;选择Login with Google的方式会打开浏览器，登录谷歌账号即可。
选择Use Gemini API Key的方式，则需要自己先生成一个Gemini API key，并配置到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.env&lt;/code&gt;文件中。&lt;/p&gt;

&lt;p&gt;生成Gemini API key的方法：&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;打开&lt;a href=&quot;https://aistudio.google.com/app/api-keys&quot;&gt;aistudio&lt;/a&gt;，点击右上角的Create API Key生成一个key&lt;/li&gt;
  &lt;li&gt;将API key配置到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.env&lt;/code&gt;文件中：&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;GEMINI_API_KEY&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;AIzaSysxxxxxxxxxxx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;谷歌gemini是现在了亚太地区使用的，因此ip出口在亚太地区的话，使用gemini-cli会出现报错：&lt;/p&gt;
  &lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[API Error: User location is not supported for the API use. (Status: FAILED_PRECONDITION)]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;img src=&quot;/static//img/blog/aicode/2025-09-29-gemini-cli.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;评价-4&quot;&gt;评价&lt;/h3&gt;

&lt;p&gt;优点：&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;使用谷歌账号登录每天1000个免费请求，无需计算token数，个人用户使用0成本&lt;/li&gt;
  &lt;li&gt;使用apikey可以使用Gemini 2.5 Pro模型，每天100个免费请求&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;缺点：&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;对ip出口有严格限制，需要全程使用代理确保ip出口在谷歌支持的地区,有一定的使用门槛&lt;/li&gt;
  &lt;li&gt;不支持替换后端服务，只能使用gemini模型&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;qwen-code&quot;&gt;qwen code&lt;/h2&gt;

&lt;p&gt;Qwen Code 是一款功能强大的命令行 AI 工作流工具，改编自 Gemini CLI （ 详情 ），并专门针对 Qwen3-Coder 模型进行了优化。它通过高级代码理解、自动化任务和智能辅助功能增强您的开发工作流程。&lt;/p&gt;

&lt;p&gt;项目地址：&lt;a href=&quot;https://github.com/QwenLM/qwen-code&quot;&gt;qwen-code&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;安装方法-3&quot;&gt;安装方法&lt;/h3&gt;

&lt;p&gt;使用npm安装：&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$&amp;gt; npm install -g @qwen-code/qwen-code@latest
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;使用方法-5&quot;&gt;使用方法&lt;/h3&gt;

&lt;p&gt;命令行启动千问：&lt;/p&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$&amp;gt;&lt;/span&gt; qwen
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;总体使用方法跟gemini-cli相似。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/static//img/blog/aicode/2025-09-29-qwen.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;评价-5&quot;&gt;评价&lt;/h3&gt;

&lt;p&gt;优点：&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;个人使用每天2000个请求，无需计算token数，个人用户使用0成本&lt;/li&gt;
  &lt;li&gt;支持替换后端服务，可以使用其他openai接口兼容的模型&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;缺点：&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;当前效果无法跟cursor和claude code相比&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;codex&quot;&gt;Codex&lt;/h2&gt;

&lt;p&gt;Codex 是由 OpenAI 推出的强大代码生成与理解模型，基于 GPT-3 架构，专为编程任务优化。Codex 能够理解自然语言描述，自动生成多种编程语言的代码，支持自动补全、代码解释、单元测试生成等功能。它已集成到 GitHub Copilot、OpenAI Playground 等工具中，广泛应用于软件开发、数据分析和自动化脚本编写。Codex 支持多种主流 IDE，能够显著提升开发效率，适合个人开发者和团队协作。&lt;/p&gt;

&lt;p&gt;项目地址：&lt;a href=&quot;https://github.com/openai/codex&quot;&gt;codex&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;安装方法-4&quot;&gt;安装方法&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$&amp;gt; npm i -g @openai/codex
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;使用方法-6&quot;&gt;使用方法&lt;/h3&gt;

&lt;p&gt;启动：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$&amp;gt; codex
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;╭───────────────────────────────────────────────────────────╮
│ &amp;gt;_ OpenAI Codex (v0.42.0)                                 │
│                                                           │
│ model:     deepseek/deepseek-chat-v3.1   /model to change │
│ directory: ~/workspace/code/project                       │
╰───────────────────────────────────────────────────────────╯

  To get started, describe a task or try one of these commands:

  /init - create an AGENTS.md file with instructions for Codex
  /status - show current session configuration
  /approvals - choose what Codex can do without approval
  /model - choose what model and reasoning effort to use

▌ hello

&amp;gt; Hello! I&apos;m Codex CLI, a coding assistant. I&apos;m here to help you with any coding tasks in the /home/kael/workspace/code/project workspace.

  What would you like to work on today?
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;codex同样内置了诸多命令，使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/&lt;/code&gt;获得命令提示。&lt;/p&gt;

&lt;p&gt;codex启动后同样会询问授权方式，可以使用oauth2登录，也可以使用apikey。&lt;/p&gt;

&lt;p&gt;使用apikey的方式，可以在&lt;a href=&quot;https://platform.openai.com/settings/organization/api-keys&quot;&gt;api-key&lt;/a&gt;这里创建一个新的apikey，然后配置到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.codex/auth.json&lt;/code&gt;中：&lt;/p&gt;

&lt;div class=&quot;language-json highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;&quot;OPENAI_API_KEY&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;asdasdad&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;如果要替换使用其他openai接口兼容的服务端，需要修改~/.codex/config.toml的配置 (文件不存在可以自行创建)，在最开始添加如下配置：&lt;/p&gt;

&lt;div class=&quot;language-json highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;err&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;gpt-5&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;model_provider&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;mail&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;model_reasoning_effort&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;medium&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;model_providers.mail&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Mail&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;base_url&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;http://compatible.server/protocol/openai/v1&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;env_key&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;CODEX_API_KEY&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;这里env_key的值CODEX_API_KEY是一个环境变量的key，apikey的真正值要在环境变量设置CODEX_API_KEY=${apikey}&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;评价-6&quot;&gt;评价&lt;/h3&gt;

&lt;p&gt;优点：&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;支持替换openai接口兼容的模型服务&lt;/li&gt;
  &lt;li&gt;支持多个ide的插件（不支持idea插件）&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;缺点：&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;没有免费额度&lt;/li&gt;
  &lt;li&gt;默认直接使用openai的模型，由于禁止中国使用，充值门槛也较高，通常建议使用openrouter或者国内其他openai兼容接口模型&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;trea&quot;&gt;trea&lt;/h2&gt;

&lt;p&gt;Trea 是字节跳动推出的 AI 编程助手，专注于提升开发者的编程效率。Trea 支持多种主流编程语言，具备代码自动补全、智能生成、代码解释、Bug 修复和单元测试生成等功能。它集成了先进的大语言模型，能够理解复杂的开发需求，适用于个人开发者和团队协作。Trea 提供命令行工具和 IDE 插件，易于集成到现有开发流程中，助力开发者高效完成各类编程任务。&lt;/p&gt;

&lt;h3 id=&quot;使用方法-7&quot;&gt;使用方法&lt;/h3&gt;

&lt;p&gt;trae分国内版和海外版。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.trae.cn/download&quot;&gt;国内版官网&lt;/a&gt;: 免费，只能使用国内大模型&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.trae.ai/download&quot;&gt;海外版官网&lt;/a&gt;： 分免费版和Pro版，可以使用Claude等大模型，免费版有部分限制，付费版10$每月&lt;/p&gt;

&lt;p&gt;国内版只能使用国内的大模型，海外版可以使用claude等模型。&lt;/p&gt;

&lt;p&gt;下载后打开，登录即可：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/static/img/blog/aicode/2025-09-29-trae.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;评价-7&quot;&gt;评价&lt;/h3&gt;

&lt;p&gt;优点：&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;免费使用，海外版支持海外大模型，但不支持中国地区&lt;/li&gt;
  &lt;li&gt;海外付费版付费方便，可以使用支付宝微信支付&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;缺点：&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;目前总体评价一般，效果一般&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;总结&quot;&gt;总结&lt;/h2&gt;

&lt;p&gt;上面的说明ai编程工具对比推荐：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;免费版用户&lt;/strong&gt;： qwen code
原因：使用无门槛，效果能达到claude code 60%左右，完全免费使用。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;付费版用户&lt;/strong&gt;：Claude Code + Kimi K2
原因：付费使用简单，门槛低，客户端支持常用idea插件，费用整体不高，效果能达到claude code的70%&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Mon, 29 Sep 2025 00:00:00 +0000</pubDate>
        <link>http://kael-aiur.com/ai/ai%E7%BC%96%E7%A8%8B%E5%B7%A5%E5%85%B7%E6%B1%87%E6%80%BB.html</link>
        <guid isPermaLink="true">http://kael-aiur.com/ai/ai%E7%BC%96%E7%A8%8B%E5%B7%A5%E5%85%B7%E6%B1%87%E6%80%BB.html</guid>
        
        <category>ai,code,claude</category>
        
        <category>code,gemini-cli,codex,qwen</category>
        
        <category>coder,cursor，trae</category>
        
        
        <category>AI</category>
        
      </item>
    
      <item>
        <title>降级组件对比</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#前言&quot; id=&quot;markdown-toc-前言&quot;&gt;前言&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#hystrix&quot; id=&quot;markdown-toc-hystrix&quot;&gt;Hystrix&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#reselience4j&quot; id=&quot;markdown-toc-reselience4j&quot;&gt;Reselience4j&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#sentinel&quot; id=&quot;markdown-toc-sentinel&quot;&gt;Sentinel&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#组件对比&quot; id=&quot;markdown-toc-组件对比&quot;&gt;组件对比&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#推荐方案&quot; id=&quot;markdown-toc-推荐方案&quot;&gt;推荐方案&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;前言&quot;&gt;前言&lt;/h2&gt;

&lt;p&gt;目前业内常见的微服务系统容错降级方案主要有几个：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/Netflix/Hystrix&quot;&gt;Hystrix&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/resilience4j/resilience4j&quot;&gt;Resilience4j&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://sentinelguard.io/zh-cn/docs/introduction.html&quot;&gt;Sentinel&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;这里对几个组件简单介绍一下。&lt;/p&gt;

&lt;h3 id=&quot;hystrix&quot;&gt;Hystrix&lt;/h3&gt;

&lt;p&gt;Hystrix [hɪst’rɪks] 是Netflix开源的延迟和容错组件，主要是用于隔离远程系统，服务和第三方组件的一些接口故障，包括无法连接，超时，报错等，实现在复杂的分布式系统中的容错和弹性。&lt;/p&gt;

&lt;p&gt;Hystrix基于Java开发，目前最新的版本是1.5.18，支持如下功能：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;请求限流&lt;/li&gt;
  &lt;li&gt;服务降级&lt;/li&gt;
  &lt;li&gt;服务隔离（舱壁模式）&lt;/li&gt;
  &lt;li&gt;服务熔断&lt;/li&gt;
  &lt;li&gt;调用缓存&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Netflix已经宣布不再开发新功能，仅维护现有功能稳定，并且推荐使用新的高可用框架Resilience4j。&lt;/p&gt;

&lt;h3 id=&quot;reselience4j&quot;&gt;Reselience4j&lt;/h3&gt;

&lt;p&gt;Resilience4j [rɪˈzɪliəns] 是一个函数式编程设计的容错库，是Hystrix停更之后推荐的高可用容错组件，Resilience4j本身是受Hystrix启发而设计开发的，相比于Hystrix更加轻量，少了很多外部依赖，只依赖Vavr。&lt;/p&gt;

&lt;p&gt;Resilience4j提供了一些基于装饰器模式的高阶函数，用于增强函数接口和lambda表达式的方法，用于增强断路器，限流器，重试器和舱壁隔离等。Resilience4j的设计思想是按需取用，因为不同功能都区分了模块，主要提供了如下几个核心功能模块：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;resilience4j-circuitbreaker：断路模块，支持熔断功能&lt;/li&gt;
  &lt;li&gt;resilience4j-ratelimiter：速率模块，支持限流功能&lt;/li&gt;
  &lt;li&gt;resilience4j-bulkhead：舱壁模块，支持服务隔离&lt;/li&gt;
  &lt;li&gt;resilience4j-retry：重试模块，支持自动重试&lt;/li&gt;
  &lt;li&gt;resilience4j-timelimiter：超时模块，支持超时处理&lt;/li&gt;
  &lt;li&gt;resilience4j-cache：缓存模块，支持调用缓冲&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Resilience4j目前还在持续发展，社区也有一定的活跃程度，但是目前国内使用比较少。&lt;/p&gt;

&lt;h3 id=&quot;sentinel&quot;&gt;Sentinel&lt;/h3&gt;

&lt;p&gt;Sentinel 是面向分布式、多语言异构化服务架构的流量治理组件，主要以流量为切入点，从流量路由、流量控制、流量整形、熔断降级、系统自适应过载保护、热点流量防护等多个维度来帮助开发者保障微服务的稳定性。&lt;/p&gt;

&lt;p&gt;Sentinel是一个轻量的组件，不依赖任何框架和库，能够运行于Java 8及以上的版本，同时对常见的开源组件有较好的支持，主要的功能包括：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;流量控制&lt;/li&gt;
  &lt;li&gt;熔断降级&lt;/li&gt;
  &lt;li&gt;系统负载保护&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;sentinel目前社区活跃，在国内广泛使用，是一个非常受欢迎的容错组件。&lt;/p&gt;

&lt;h2 id=&quot;组件对比&quot;&gt;组件对比&lt;/h2&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;对比项&lt;/th&gt;
      &lt;th&gt;Hystrix&lt;/th&gt;
      &lt;th&gt;Resilience4j&lt;/th&gt;
      &lt;th&gt;Sentinel&lt;/th&gt;
      &lt;th&gt;对比&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;流量控制&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;1.基于信号量&lt;br /&gt;2.基于线程数（官方推荐）&lt;/td&gt;
      &lt;td&gt;支持信号量限流&lt;/td&gt;
      &lt;td&gt;1. 基于QPS和并发线程数的限制&lt;br /&gt;2. 基于调用关系的流量控制&lt;/td&gt;
      &lt;td&gt;1. Hystrix对限流的支持功能较弱，比较有限&lt;br /&gt;2. Resilience4j的限流只支持信号量，但限流功能较强&lt;br /&gt;3. Sentinel的限流功能最灵活强大&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;熔断策略&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;基于数量的异常比例&lt;/td&gt;
      &lt;td&gt;1. 基于请求数量的滑动窗口&lt;br /&gt;2. 基于时间的滑动窗口&lt;/td&gt;
      &lt;td&gt;基于滑动时间窗口&lt;/td&gt;
      &lt;td&gt;1. Hystrix熔断策略单一&lt;br /&gt;2. Resilience4j的熔断策略最丰富，但是基于请求数量的滑动窗口策略较少使用&lt;br /&gt;3. Sentinel基于滑动时间窗口的熔断策略功能最灵活&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;服务隔离策略&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;基于线程池隔离&lt;/td&gt;
      &lt;td&gt;1. 基于信号量隔离&lt;br /&gt;2. 基于有界队列和固定的线程池隔离&lt;/td&gt;
      &lt;td&gt;基于信号量隔离&lt;/td&gt;
      &lt;td&gt;1. Hystrix基于线程池的隔离策略较重，并且需要预先设置好各个服务的线程池数量&lt;br /&gt;2. Resilience4j的隔离策略较灵活丰富&lt;br /&gt;3. Sentinel中规中矩，仅支持信号量隔离&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;黑白名单&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;不支持&lt;/td&gt;
      &lt;td&gt;不支持&lt;/td&gt;
      &lt;td&gt;基于来源配置黑白名单&lt;/td&gt;
      &lt;td&gt;黑白名单功能比较少用，只有在极特殊的场景进行动态阻断流量使用&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;指标监控&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;支持实时控制台，需要接入springboot框架&lt;/td&gt;
      &lt;td&gt;支持通过prometheus接入grafana监控&lt;/td&gt;
      &lt;td&gt;支持开箱即用的实时控制台，并且可以通过控制台动态修改降级规则&lt;/td&gt;
      &lt;td&gt;1. Sentinel的实时控制台最强大，但是缺少报警功能&lt;br /&gt;2. resilience4j可以通过接入grafana实现报警功能&lt;br /&gt;3. Hystrix的控制台较弱，只能看数据，并且有框架依赖&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;系统自适应保护&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;不支持&lt;/td&gt;
      &lt;td&gt;不支持&lt;/td&gt;
      &lt;td&gt;支持&lt;/td&gt;
      &lt;td&gt;Sentinel可以针对系统负载情况进行自适应保护，防止系统出现集群故障&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;动态规则&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;基于&lt;a href=&quot;https://github.com/Netflix/archaius&quot;&gt;Archaius&lt;/a&gt;的动态配置&lt;/td&gt;
      &lt;td&gt;不支持&lt;/td&gt;
      &lt;td&gt;通过控制台动态配置&lt;/td&gt;
      &lt;td&gt;1. Hystrix基于文件的动态配置，需要通过svn/git等工具进行配置文件管理&lt;br /&gt;2. Sentinel可以通过控制台或者接口进行修改&lt;br /&gt;3. Resilience4j需要定制开发实现动态配置&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;规则持久化&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;基于配置文件存储&lt;/td&gt;
      &lt;td&gt;不支持（写在代码中）&lt;/td&gt;
      &lt;td&gt;Zookeeper&lt;br /&gt;Apollo&lt;br /&gt;Nacos&lt;/td&gt;
      &lt;td&gt;1. Sentinel的配置持久化支持较好，并且可以自主扩展其他持久化存储&lt;br /&gt;2. Resilience4j需要定制开发实现配置持久化&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;主流框架适配&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;spring boot&lt;br /&gt;spring cloud&lt;/td&gt;
      &lt;td&gt;1. spring boot&lt;br /&gt;2. spring cloud&lt;br /&gt;3. 其他（&lt;a href=&quot;https://resilience4j.readme.io/docs/getting-started-1&quot;&gt;参考文档&lt;/a&gt;）&lt;/td&gt;
      &lt;td&gt;1. spring boot&lt;br /&gt;2. spring cloud&lt;br /&gt;3. 其他（&lt;a href=&quot;https://sentinelguard.io/zh-cn/docs/open-source-framework-integrations.html&quot;&gt;参考文档&lt;/a&gt;）&lt;/td&gt;
      &lt;td&gt;1. 实际上几个组件对spring boot/spring cloud体系的集成都是不错的&lt;br /&gt;2. sentinel有更多开源框架的适配&lt;br /&gt;3. sentinel适配spring boot 2.0.x版本的组件已经不再维护，建议升级了&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;基于插件机制&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;基于插件机制&lt;/td&gt;
      &lt;td&gt;通过springboot框架提供的插件机制&lt;/td&gt;
      &lt;td&gt;支持功能槽扩展&lt;br /&gt;内置SPI机制扩展&lt;/td&gt;
      &lt;td&gt;1. Hystrix官网提供了插件机制支持扩展和二次开发&lt;br /&gt;2. Resilience4j官方文档没有提及扩展和二次开发的能力&lt;br /&gt;3. Sentinel的扩展能力最强，设计时已经预留基于责任链模式的扩展槽，并且内置提供了SPI机制用于扩展开发&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;strong&gt;最新稳定版本&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;1.5.8&lt;/td&gt;
      &lt;td&gt;1.7.0&lt;/td&gt;
      &lt;td&gt;1.8.1&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;推荐方案&quot;&gt;推荐方案&lt;/h2&gt;

&lt;p&gt;从上面的对比中可以看出，Hystrix由于已经不在发展了，首先抛弃。对比Resilience4j，Sentinel具有社区更活跃，功能更完整，应用更广泛等明显优势，建议使用Sentinel作为容错降级的首选组件。
但是Sentinel目前的功能还不能完全满足需求，主要缺少如下内容：&lt;/p&gt;

&lt;p&gt;控制台存在如下问题：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;仅支持单机部署，不作为开箱即用的生产环境控制台&lt;/li&gt;
  &lt;li&gt;控制台只能查看最近5分钟的指标数据，缺乏持久化的能力&lt;/li&gt;
  &lt;li&gt;不支持权限划分，所有人登录控制台都能修改所有应用的规则&lt;/li&gt;
  &lt;li&gt;没有开箱即用对接Grafana等报表和监控平台，缺乏有效的报警手段&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;以上的功能，实际上可以通过SPI机制时间扩展开发。&lt;/p&gt;

&lt;p&gt;附：Sentinel版本和springboot版本对应关系：&lt;a href=&quot;https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E&quot;&gt;版本说明&lt;/a&gt;&lt;/p&gt;
</description>
        <pubDate>Sun, 05 Nov 2023 00:00:00 +0000</pubDate>
        <link>http://kael-aiur.com/%E5%AE%B9%E9%94%99%E5%92%8C%E9%99%8D%E7%BA%A7/%E9%99%8D%E7%BA%A7%E7%BB%84%E4%BB%B6%E5%AF%B9%E6%AF%94.html</link>
        <guid isPermaLink="true">http://kael-aiur.com/%E5%AE%B9%E9%94%99%E5%92%8C%E9%99%8D%E7%BA%A7/%E9%99%8D%E7%BA%A7%E7%BB%84%E4%BB%B6%E5%AF%B9%E6%AF%94.html</guid>
        
        
        <category>容错和降级</category>
        
      </item>
    
      <item>
        <title>Sentinel规则说明</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#简介&quot; id=&quot;markdown-toc-简介&quot;&gt;简介&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#流控规则&quot; id=&quot;markdown-toc-流控规则&quot;&gt;流控规则&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#降级规则&quot; id=&quot;markdown-toc-降级规则&quot;&gt;降级规则&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;简介&quot;&gt;简介&lt;/h2&gt;

&lt;p&gt;Sentinel是阿里巴巴开源的一个容错降级组件，其核心的设计思想是定义资源，然后为资源定义规则实现流控和容错降级，本文主要讨论Sentinel的规则配置。&lt;/p&gt;

&lt;h2 id=&quot;流控规则&quot;&gt;流控规则&lt;/h2&gt;

&lt;p&gt;流控规则配置的核心类是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;com.alibaba.csp.sentinel.slots.block.flow.FlowRule&lt;/code&gt;，主要的配置也是按照这个类的属性配置。&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;属性&lt;/th&gt;
      &lt;th&gt;必须&lt;/th&gt;
      &lt;th&gt;默认值&lt;/th&gt;
      &lt;th&gt;说明&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;resource&lt;/td&gt;
      &lt;td&gt;是&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
      &lt;td&gt;资源名称，表面这个规则对哪个资源生效&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;limitApp&lt;/td&gt;
      &lt;td&gt;是&lt;/td&gt;
      &lt;td&gt;default&lt;/td&gt;
      &lt;td&gt;流控针对的调用来源，default表示不区分来源&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;grade&lt;/td&gt;
      &lt;td&gt;是&lt;/td&gt;
      &lt;td&gt;1&lt;/td&gt;
      &lt;td&gt;限流阈值类型，QPS 或线程数模式，1表示QPS模式，0表示线程模式&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;count&lt;/td&gt;
      &lt;td&gt;是&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;一旦配置了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;resource&lt;/code&gt;，这个流量就必须配置，否则按默认值处理的话，会拦截所有流量&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;strategy&lt;/td&gt;
      &lt;td&gt;是&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;调用关系限流策略：0: 直接、1: 关联、2: 链路&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;controlBehavior&lt;/td&gt;
      &lt;td&gt;是&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;流控效果（0:直接拒绝、1: 慢启动模式、2：排队等待、3: 慢启动+限速模式），不支持按调用关系限流&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;ul&gt;
  &lt;li&gt;直接拒绝：放流量通过，但是直接进入降级逻辑&lt;/li&gt;
  &lt;li&gt;慢启动模式：逐步增加通过流量，给服务端热身时间，达到count的阈值之后，多余的流量进入降级逻辑&lt;/li&gt;
  &lt;li&gt;排队等待：控制流量进入，只能以固定速率进入正常逻辑，不会进入降级逻辑&lt;/li&gt;
  &lt;li&gt;慢启动+限速模式：逐步增加通过流量，给服务端热身时间，达到count的阈值之后，其他请求继续等待，不会进入降级逻n辑&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;降级规则&quot;&gt;降级规则&lt;/h2&gt;

&lt;p&gt;降级规则配置的核心类是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule&lt;/code&gt;。&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;属性&lt;/th&gt;
      &lt;th&gt;必须&lt;/th&gt;
      &lt;th&gt;默认值&lt;/th&gt;
      &lt;th&gt;说明&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;resource&lt;/td&gt;
      &lt;td&gt;是&lt;/td&gt;
      &lt;td&gt;-&lt;/td&gt;
      &lt;td&gt;资源名称，表面这个规则对哪个资源生效&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;grade&lt;/td&gt;
      &lt;td&gt;是&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;熔断策略，0: 慢调用比例，1：异常比例，2: 异常数&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;count&lt;/td&gt;
      &lt;td&gt;是&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;慢调用比例下表示慢调用阈值，即超过这个值的调用计为慢调用，单位s，异常比例/异常数模式下为对应的阈值&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;timeWindow&lt;/td&gt;
      &lt;td&gt;是&lt;/td&gt;
      &lt;td&gt;0&lt;/td&gt;
      &lt;td&gt;熔断时长，单位s&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;minRequestAmount&lt;/td&gt;
      &lt;td&gt;是&lt;/td&gt;
      &lt;td&gt;5&lt;/td&gt;
      &lt;td&gt;熔断触发的最小请求数，即请求数小于该值则不会进行熔断&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;statIntervalMs&lt;/td&gt;
      &lt;td&gt;是&lt;/td&gt;
      &lt;td&gt;1000&lt;/td&gt;
      &lt;td&gt;统计时长，单位ms&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;slowRatioThreshold&lt;/td&gt;
      &lt;td&gt;是&lt;/td&gt;
      &lt;td&gt;1.0&lt;/td&gt;
      &lt;td&gt;慢调用比例阈值，仅慢调用比例模式有效（1.8.0引入）&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
</description>
        <pubDate>Wed, 02 Nov 2022 00:00:00 +0000</pubDate>
        <link>http://kael-aiur.com/%E5%AE%B9%E9%94%99%E5%92%8C%E9%99%8D%E7%BA%A7/Sentinel%E8%A7%84%E5%88%99%E8%AF%B4%E6%98%8E.html</link>
        <guid isPermaLink="true">http://kael-aiur.com/%E5%AE%B9%E9%94%99%E5%92%8C%E9%99%8D%E7%BA%A7/Sentinel%E8%A7%84%E5%88%99%E8%AF%B4%E6%98%8E.html</guid>
        
        
        <category>容错和降级</category>
        
      </item>
    
      <item>
        <title>Tunnelblick自动输入动态密码和自动连接</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#前言&quot; id=&quot;markdown-toc-前言&quot;&gt;前言&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#前期准备&quot; id=&quot;markdown-toc-前期准备&quot;&gt;前期准备&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#编写脚本&quot; id=&quot;markdown-toc-编写脚本&quot;&gt;编写脚本&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#生成谷歌验证码&quot; id=&quot;markdown-toc-生成谷歌验证码&quot;&gt;生成谷歌验证码&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#替换输入密码&quot; id=&quot;markdown-toc-替换输入密码&quot;&gt;替换输入密码&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#重新加载配置&quot; id=&quot;markdown-toc-重新加载配置&quot;&gt;重新加载配置&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#设置启动连接可选&quot; id=&quot;markdown-toc-设置启动连接可选&quot;&gt;设置启动连接【可选】&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;前言&quot;&gt;前言&lt;/h2&gt;

&lt;p&gt;很多公司为了保证安全，一般内网服务都不允许直接公网访问，员工使用内网服务通常都需要使用VPN接入公司内网，目前企业内网的VPN方便常见的有微软自带的VPN功能，或者开元的OpenVPN协议，或者其他的easy connect等等。&lt;/p&gt;

&lt;p&gt;本文主要讨论基于OpenVPN方案的工具。&lt;/p&gt;

&lt;p&gt;基于OpenVPN方案的客户端比较常见的有&lt;a href=&quot;https://openvpn.net/&quot;&gt;OpenVPN Connect&lt;/a&gt;和&lt;a href=&quot;https://tunnelblick.net/&quot;&gt;Tunnelblick&lt;/a&gt;，这两个都是非常优秀的客户端软件，（Ubuntu系统自带了OpenVPN连接，可以不使用这两个软件）。&lt;/p&gt;

&lt;p&gt;出于工作需要，每次打开电脑都要连接VPN，如果使用固定密码的话，可以通过软件的记住密码功能实现不用每次都输入密码，但是有些公司的服务器为了安全，不会使用固定密码，而是使用类似谷歌验证的方式，使用动态密码，这个时候就非常麻烦了，每次连接必须先获取谷歌动态密码，然后再输入到密码框中，一天连接一次可能还好，连接多次的时候，确实是非常烦人的操作。&lt;/p&gt;

&lt;p&gt;目前经过笔者研究，OpenVPN Connect还不支持使用脚本动态输入密码，但是Tunnelblick从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3.8.3beta02&lt;/code&gt;以后的版本就支持通过脚本动态替换密码的能力了，官网文档&lt;a href=&quot;https://tunnelblick.net/cUsingScripts.html&quot;&gt;Using Scripts&lt;/a&gt;。&lt;/p&gt;

&lt;p&gt;本文主要介绍在Mac OS系统下如何基于Tunnelblick的脚本功能实现动态输入密码自动连接VPN。&lt;/p&gt;

&lt;h2 id=&quot;前期准备&quot;&gt;前期准备&lt;/h2&gt;

&lt;p&gt;在开始实现动态输入密码的脚本前，我们要先通过正常的安装和配置，确保们的VPN已经可以正常使用，参考官网安装并导入配置即可。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://tunnelblick.net/cInstall.html&quot;&gt;安装文档&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://tunnelblick.net/cConfigT.html&quot;&gt;导入VPN配置&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;安装配置完成后，可以看到如下vpn详情中有如下配置：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/static/img/blog/tunnelblick/tunnelblick_1.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;现在我们先配置自动登录：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/static/img/blog/tunnelblick/tunnelblick_2.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;这里的密码随便输入，后面我们会用脚本自动替换，在这里随便输入密码只是为了自动触发登录的操作。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;ok，到这里我们的前期准备已经完成。&lt;/p&gt;

&lt;h2 id=&quot;编写脚本&quot;&gt;编写脚本&lt;/h2&gt;

&lt;p&gt;根据官网的文档：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;password-replace.user.sh is executed to get a string to replace a password before it is passed to OpenVPN.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;我们可以使用脚本&lt;strong&gt;password-replace.user.sh&lt;/strong&gt;在登录前对输入的密码进行替换，替换成我们自动计算的动态密码即可，需要实现如下两部：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;生成谷歌验证码&lt;/li&gt;
  &lt;li&gt;使用password-replace.user.sh脚本将谷歌验证码返回给tunnelblick替换前面记住的密码&lt;/li&gt;
  &lt;li&gt;重新加载配置，使脚本生效&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;生成谷歌验证码&quot;&gt;生成谷歌验证码&lt;/h3&gt;

&lt;p&gt;首选我们需要一个自动生成谷歌验证码的脚本，目前最简单的方式就是使用python脚本，这里给出一个实际可用的例子：&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;hmac&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;base64&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hashlib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;platform&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_hotp_token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;secret&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;intervals_no&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;key&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;base64&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b32decode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;secret&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&amp;gt;Q&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;intervals_no&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hmac&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hashlib&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sha1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;digest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;o&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;ord&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;chr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;19&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;15&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unpack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&amp;gt;I&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x7fffffff&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000000&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_totp_token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;secret&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_hotp_token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;secret&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;intervals_no&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bias&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;//&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_google_code&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;secret&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;$SECRET&quot;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 谷歌验证密钥，用于生成谷歌验证码，需要注意的是，验证码的长度需是16的整数倍，比如16，32，64等，如果长度不足，则通过=号补齐长度
&lt;/span&gt;    &lt;span class=&quot;n&quot;&gt;googlecode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_totp_token&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;secret&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&apos;%06d&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;googlecode&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_google_code&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;完成上述脚本之后，可以执行一下验证是否成功：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$&amp;gt; python3 tunnelblick.py
303158%
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;说明脚本已经ok。&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;注意最后的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;%&lt;/code&gt;，这个是自动添加的，可以不理会，tunnelblick在使用密码的时候会自动将这个结尾去掉，官网原文：&lt;/p&gt;

  &lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;If the output from the &quot;password…&quot; scripts ends in an ASCII LF character (0x0A), it will be removed before the string is used to replace or modify the password.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;替换输入密码&quot;&gt;替换输入密码&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;password-replace.user.sh&lt;/strong&gt;脚本要放在对应的VPN设置目录下，默认是:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/Library/Application Support/Tunnelblick/Users/{user}/{config_name}.tblk/Contents/Resources
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这个目录必须使用root权限创建和修改脚本，参考官网&lt;a href=&quot;https://tunnelblick.net/cFileLocations.html&quot;&gt;File Locations&lt;/a&gt;。&lt;/p&gt;

&lt;p&gt;脚本内容如下：&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;#! /bin/bash&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;PASSWORD&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;python3 tunnelblick.py&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$PASSWORD&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;这里第一行的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;#! /bin/bash&lt;/code&gt;不能少，否则tunnelblick不会执行这个脚本。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;echo&lt;/code&gt;输出的结果tunnelblick会作为返回值，替换原来记住的密码，可以根据需要处理即可。&lt;/p&gt;

&lt;blockquote&gt;
  &lt;ul&gt;
    &lt;li&gt;password-replace.user.sh脚本的权限必须是0755，否则tunnelblick会识别为不安全脚本无法连接&lt;/li&gt;
    &lt;li&gt;google_auth.py的目录建议不要放在自己的目录下，否则权限也必须是0755&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;重新加载配置&quot;&gt;重新加载配置&lt;/h3&gt;

&lt;p&gt;增加这个脚本后，相当于修改了当前vpn连接的配置，Tunnelblick会扫描并发现配置变更了，此时连接会出现警告：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/static/img/blog/tunnelblick/tunnelblick_3.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;这里有三个选项：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Secure the Configuration&lt;/strong&gt;: 回滚配置，使用最后一次正确的配置&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Revert to the Last Secured Copy&lt;/strong&gt;: 应用配置，保存本次变更为最后一次正确配置&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Cancel&lt;/strong&gt;: 取消，暂时不处理，也不连接VPN&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;这里我们选择&lt;strong&gt;Revert to the Last Secured Copy&lt;/strong&gt;保存配置即可。&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;有可能不会每次都弹出这个提醒，如果没有弹出这个提醒,并且连接一直失败的话，请重启tunnelblick，并检查2.1.2中的脚本权限是否正确&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;现在已经可以自动填写动态密码并连接VPN了。&lt;/p&gt;

&lt;h2 id=&quot;设置启动连接可选&quot;&gt;设置启动连接【可选】&lt;/h2&gt;

&lt;p&gt;最后，我们可以在配置中选择启动tunnelblick即自动连接VPN，这样就可以达到重启后也自动连接的目的，最大限度减少我们关注VPN是否连接的问题：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/static/img/blog/tunnelblick/tunnelblick_4.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
</description>
        <pubDate>Fri, 28 Oct 2022 00:00:00 +0000</pubDate>
        <link>http://kael-aiur.com/%E9%BB%91%E7%A7%91%E6%8A%80/Tunnelblick%E8%87%AA%E5%8A%A8%E8%BE%93%E5%85%A5%E5%8A%A8%E6%80%81%E5%AF%86%E7%A0%81%E5%92%8C%E8%87%AA%E5%8A%A8%E8%BF%9E%E6%8E%A5VPN.html</link>
        <guid isPermaLink="true">http://kael-aiur.com/%E9%BB%91%E7%A7%91%E6%8A%80/Tunnelblick%E8%87%AA%E5%8A%A8%E8%BE%93%E5%85%A5%E5%8A%A8%E6%80%81%E5%AF%86%E7%A0%81%E5%92%8C%E8%87%AA%E5%8A%A8%E8%BF%9E%E6%8E%A5VPN.html</guid>
        
        <category>VPN,动态密码</category>
        
        
        <category>黑科技</category>
        
      </item>
    
      <item>
        <title>狼蛛F87说明书</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#狼蛛f87说明书照片&quot; id=&quot;markdown-toc-狼蛛f87说明书照片&quot;&gt;狼蛛F87说明书照片&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;狼蛛f87说明书照片&quot;&gt;狼蛛F87说明书照片&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/static/img/blog/keyboard/aual_f87.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
</description>
        <pubDate>Thu, 01 Sep 2022 00:00:00 +0000</pubDate>
        <link>http://kael-aiur.com/%E6%9C%BA%E6%A2%B0%E9%94%AE%E7%9B%98/%E7%8B%BC%E8%9B%9BF87%E9%94%AE%E7%9B%98%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E%E4%B9%A6.html</link>
        <guid isPermaLink="true">http://kael-aiur.com/%E6%9C%BA%E6%A2%B0%E9%94%AE%E7%9B%98/%E7%8B%BC%E8%9B%9BF87%E9%94%AE%E7%9B%98%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E%E4%B9%A6.html</guid>
        
        <category>狼蛛,F87,机械键盘</category>
        
        
        <category>机械键盘</category>
        
      </item>
    
      <item>
        <title>杂牌三模87热插拔键盘</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#杂牌三模热插拔87键说明书电子版照片&quot; id=&quot;markdown-toc-杂牌三模热插拔87键说明书电子版照片&quot;&gt;杂牌三模热插拔87键说明书电子版照片&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;杂牌三模热插拔87键说明书电子版照片&quot;&gt;杂牌三模热插拔87键说明书电子版照片&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;/static/img/blog/keyboard/3mod_87.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;
</description>
        <pubDate>Thu, 01 Sep 2022 00:00:00 +0000</pubDate>
        <link>http://kael-aiur.com/%E6%9C%BA%E6%A2%B0%E9%94%AE%E7%9B%98/%E4%B8%89%E6%A8%A187%E7%83%AD%E6%8F%92%E6%8B%94%E9%94%AE%E7%9B%98%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E%E4%B9%A6.html</link>
        <guid isPermaLink="true">http://kael-aiur.com/%E6%9C%BA%E6%A2%B0%E9%94%AE%E7%9B%98/%E4%B8%89%E6%A8%A187%E7%83%AD%E6%8F%92%E6%8B%94%E9%94%AE%E7%9B%98%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E%E4%B9%A6.html</guid>
        
        <category>87,热插拔,杂牌</category>
        
        
        <category>机械键盘</category>
        
      </item>
    
      <item>
        <title>k8s部署实战</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#前言&quot; id=&quot;markdown-toc-前言&quot;&gt;前言&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#minikube部署&quot; id=&quot;markdown-toc-minikube部署&quot;&gt;Minikube部署&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#kubeadm部署&quot; id=&quot;markdown-toc-kubeadm部署&quot;&gt;Kubeadm部署&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#二进制包部署&quot; id=&quot;markdown-toc-二进制包部署&quot;&gt;二进制包部署&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#参考资料&quot; id=&quot;markdown-toc-参考资料&quot;&gt;参考资料&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;前言&quot;&gt;前言&lt;/h2&gt;

&lt;p&gt;k8s全称kubernetes，这个名字大家应该都不陌生，k8s是为容器服务而生的一个可移植容器的编排管理工具，越来越多的公司正在拥抱k8s，并且当前k8s已经主导了云业务流程，推动了微服务架构等热门技术的普及和落地，正在如火如荼的发展。&lt;/p&gt;

&lt;p&gt;本文主要介绍k8s的部署实施方法。&lt;/p&gt;

&lt;p&gt;k8s一共有三种方式部署目前一共三种部署方式：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://minikube.sigs.k8s.io/docs/start/&quot;&gt;Minikube&lt;/a&gt;：官方部署工具，可以在本地快速运行一个单点的k8s，可以用于日常开发和学习使用。&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://kubernetes.io/docs/reference/setup-tools/kubeadm/kubeadm/&quot;&gt;Kubeadm&lt;/a&gt;：官方部署工具，用于快速部署和节点管理等，使用要求较多，可以用于生产环境部署。&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/kubernetes/kubernetes/releases&quot;&gt;二进制包&lt;/a&gt;：即手工部署，需要自己下载二进制包，并手动配置和启动k8s服务。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;minikube部署&quot;&gt;Minikube部署&lt;/h3&gt;

&lt;p&gt;对于开发和学习使用，Minikube是最好的选择，因为部署非常简单，可以节省大量时间，不用陷入各种配置细节。&lt;/p&gt;

&lt;p&gt;使用Minikube需要先安装Minikube。&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://minikube.sigs.k8s.io/docs/start/&quot;&gt;官方文档&lt;/a&gt;提供了多种安装方式，在本文我们使用64位的CentOS 7，使用如下方式安装：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;安装完成后，使用如下命令启动：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;minikube start
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;正确执行结果如下：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;😄  minikube v1.24.0 on Centos 7.6.1810 (kvm/amd64)
✨  Using the docker driver based on existing profile
👍  Starting control plane node minikube in cluster minikube
🚜  Pulling base image ...
🔄  Restarting existing docker container for &quot;minikube&quot; ...
🐳  Preparing Kubernetes v1.22.3 on Docker 20.10.8 ...
🔎  Verifying Kubernetes components...
    ▪ Using image gcr.io/k8s-minikube/storage-provisioner:v5
🌟  Enabled addons: storage-provisioner, default-storageclass
🏄  Done! kubectl is now configured to use &quot;minikube&quot; cluster and &quot;default&quot; namespace by default
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;minikube支持基于docker，kvm2，virtualBox，裸金属，podman等多种环境部署，因此start之前，必须已经安装如上环境之一，并且需要安装ssh，否则可能安装失败。&lt;/p&gt;

&lt;p&gt;笔者这里是安装了docker环境，因此启动过程使用了docker作为k8s的驱动，在启动日志的第二行即可看到：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;✨  Using the docker driver based on existing profile
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;启动完成之后，可以看到一个正在运行的docker容器：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$&amp;gt; docker ps
CONTAINER ID   IMAGE                                 COMMAND                  CREATED       STATUS       PORTS                                                                                                                                  NAMES
7f2230cc94fb   gcr.io/k8s-minikube/kicbase:v0.0.28   &quot;/usr/local/bin/entr…&quot;   4 hours ago   Up 2 hours   127.0.0.1:49162-&amp;gt;22/tcp, 127.0.0.1:49161-&amp;gt;2376/tcp, 127.0.0.1:49160-&amp;gt;5000/tcp, 127.0.0.1:49159-&amp;gt;8443/tcp, 127.0.0.1:49158-&amp;gt;32443/tcp   minikube
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;如果你的机器上已经安装&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kubectl&lt;/code&gt;, minikube还会自动将&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kubectl&lt;/code&gt;配置为使用minikube机群，可以看到启动日志的最后一行：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;🏄  Done! kubectl is now configured to use &quot;minikube&quot; cluster and &quot;default&quot; namespace by default
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;如果没有安装&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kubectl&lt;/code&gt;的话，可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;minikube kubectl --&lt;/code&gt;代替kubectl，minikube会自动下载合适的kubectl版本进行操作，但是每次使用kubectl命令都要输入这个命令也比较麻烦，可以为这个命令设置别名：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;alias kubectl=&quot;minikube kubectl --&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;到这里本地一个本地开发的k8s集群就部署好了，非常简单。&lt;/p&gt;

&lt;h3 id=&quot;kubeadm部署&quot;&gt;Kubeadm部署&lt;/h3&gt;

&lt;h3 id=&quot;二进制包部署&quot;&gt;二进制包部署&lt;/h3&gt;

&lt;h2 id=&quot;参考资料&quot;&gt;参考资料&lt;/h2&gt;

&lt;p&gt;[1]: &lt;a href=&quot;https://www.cnblogs.com/wanlige/p/14954621.html&quot;&gt;使用kubeadm快速部署一套K8S集群以及众多神坑记录&lt;/a&gt;&lt;/p&gt;

</description>
        <pubDate>Wed, 12 Jan 2022 00:00:00 +0000</pubDate>
        <link>http://kael-aiur.com/k8s/k8s%E9%83%A8%E7%BD%B2%E5%AE%9E%E6%88%98.html</link>
        <guid isPermaLink="true">http://kael-aiur.com/k8s/k8s%E9%83%A8%E7%BD%B2%E5%AE%9E%E6%88%98.html</guid>
        
        
        <category>k8s</category>
        
      </item>
    
      <item>
        <title>istio部署与评估</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#前言&quot; id=&quot;markdown-toc-前言&quot;&gt;前言&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#准备工作&quot; id=&quot;markdown-toc-准备工作&quot;&gt;准备工作&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#开始安装&quot; id=&quot;markdown-toc-开始安装&quot;&gt;开始安装&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#示例应用&quot; id=&quot;markdown-toc-示例应用&quot;&gt;示例应用&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#查看仪表盘&quot; id=&quot;markdown-toc-查看仪表盘&quot;&gt;查看仪表盘&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#访问kiali仪表板&quot; id=&quot;markdown-toc-访问kiali仪表板&quot;&gt;访问Kiali仪表板&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#查看prometheus仪表盘&quot; id=&quot;markdown-toc-查看prometheus仪表盘&quot;&gt;查看Prometheus仪表盘&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#查看grafana面板&quot; id=&quot;markdown-toc-查看grafana面板&quot;&gt;查看Grafana面板&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;前言&quot;&gt;前言&lt;/h2&gt;

&lt;p&gt;istio是服务网格的一种具体实现方案，在网上已经有非常非常多的相关介绍了，本文主要介绍如何搭建一个开发环境的istio。&lt;/p&gt;

&lt;h2 id=&quot;准备工作&quot;&gt;准备工作&lt;/h2&gt;

&lt;p&gt;istio提供了多种部署方式：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;istioctl： 官方安装工具，需要k8s集群&lt;/li&gt;
  &lt;li&gt;Istio Operator： 官方安装工具，需要k8s集群&lt;/li&gt;
  &lt;li&gt;helm：使用k8s的helm安装&lt;/li&gt;
  &lt;li&gt;多集群安装：跨多个k8s集群安装&lt;/li&gt;
  &lt;li&gt;虚拟机安装：虚拟机直接安装&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;出于开发使用和安装简单，我们使用istioctl的方式安装，需要做如下准备：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;部署一个k8s集群，参考&lt;a href=&quot;/k8s/k8s部署实战.html&quot;&gt;k8s部署实战&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;下载istio部署包&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;curl -L https://istio.io/downloadIstio | sh -
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;下载后进入部署包目录：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cd istio-1.12.1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;部署包内包含了客户端工具&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bin/istioctl&lt;/code&gt;和示例应用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;samples&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;将istioctl客户端加入搜索路径：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;export PATH=$PWD/bin:$PATH
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;开始安装&quot;&gt;开始安装&lt;/h2&gt;

&lt;p&gt;下载的安装包里包含了不同部署场景的配置。&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;面向开发使用的场景我们选用demo，这里包含了一组专门为测试准备的功能集合。&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ istioctl install --set profile=demo -y
✔ Istio core installed
✔ Istiod installed
✔ Egress gateways installed
✔ Ingress gateways installed
✔ Installation complete
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ol&gt;
  &lt;li&gt;给命名空间添加标签，指示istio在部署应用的时候，自动注入Envoy边车代理：&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ kubectl label namespace default istio-injection=enabled
namespace/default labeled
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;这里使用的是kubectl，k8s的工具，实际上是增加了一个触发器，在Pod创建时，会触发istio的sidecar注入流程，为pod注入sidecar（istio默认使用的是envoy），注入sidecar的pod实际上会产生两个容器。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;到这里，我们就完成在k8s集群上部署istio了。&lt;/p&gt;

&lt;p&gt;整个部署过程非常简单，接下来我们看看如何试用istio的功能。&lt;/p&gt;

&lt;h2 id=&quot;示例应用&quot;&gt;示例应用&lt;/h2&gt;

&lt;p&gt;在istio中，提供了一个非常经典的示例应用，Bookinfo，现在我们将这个应用部署到k8s中：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;部署Bookinfo&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ kubectl apply -f samples/bookinfo/platform/kube/bookinfo.yaml
service/details created
serviceaccount/bookinfo-details created
deployment.apps/details-v1 created
service/ratings created
serviceaccount/bookinfo-ratings created
deployment.apps/ratings-v1 created
service/reviews created
serviceaccount/bookinfo-reviews created
deployment.apps/reviews-v1 created
deployment.apps/reviews-v2 created
deployment.apps/reviews-v3 created
service/productpage created
serviceaccount/bookinfo-productpage created
deployment.apps/productpage-v1 created
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;istio内置的示例应用实际上是通过k8s的配置文件实现的，k8s的部署就是配置文件编写，程序镜像已经事先推送到docker的镜像仓库了。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;部署命令执行完成后，并不是部署完成了，而是存储了部署计划，开始启动部署流程，我们可以通过k8s的命令查看部署的情况：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;查看部署情况：&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ kubectl get services
NAME          TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
details       ClusterIP   10.0.0.212      &amp;lt;none&amp;gt;        9080/TCP   29s
kubernetes    ClusterIP   10.0.0.1        &amp;lt;none&amp;gt;        443/TCP    25m
productpage   ClusterIP   10.0.0.57       &amp;lt;none&amp;gt;        9080/TCP   28s
ratings       ClusterIP   10.0.0.33       &amp;lt;none&amp;gt;        9080/TCP   29s
reviews       ClusterIP   10.0.0.28       &amp;lt;none&amp;gt;        9080/TCP   29s

$ kubectl get pods
NAME                              READY   STATUS    RESTARTS   AGE
details-v1-558b8b4b76-2llld       2/2     Running   0          2m41s
productpage-v1-6987489c74-lpkgl   2/2     Running   0          2m40s
ratings-v1-7dc98c7588-vzftc       2/2     Running   0          2m41s
reviews-v1-7f99cc4496-gdxfn       2/2     Running   0          2m41s
reviews-v2-7d79d5bd5d-8zzqd       2/2     Running   0          2m41s
reviews-v3-7dbcdcbc56-m8dph       2/2     Running   0          2m41s
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;重复运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kubectl get pods&lt;/code&gt;等待所有pod的READY状态值为2/2，STATUS的值为Running，才表示部署完成。&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;确认服务已经正常运行：&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl exec &quot;$(kubectl get pod -l app=ratings -o jsonpath=&apos;{.items[0].metadata.name}&apos;)&quot; -c ratings -- curl -s productpage:9080/productpage | grep -o &quot;&amp;lt;title&amp;gt;.*&amp;lt;/title&amp;gt;&quot;
&amp;lt;title&amp;gt;Simple Bookstore App&amp;lt;/title&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;看到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;title&amp;gt;Simple Bookstore App&amp;lt;/title&amp;gt;&lt;/code&gt;的结果即说明我们已经可以访问应用了。&lt;/p&gt;

&lt;p&gt;虽然部署已经完成，但是此时我们还是无法访问这个应用，因为k8s内部的资源无法被外部直接访问，我们还需要开放外部访问。&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;开放外部访问&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;要开放示例应用的外部访问，我们需要一个Istio入站网关（Ingress Gateway），它会在网格边缘把一个路径映射到路由。&lt;/p&gt;

&lt;p&gt;先把应用关联到Istio网关：&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ kubectl apply -f samples/bookinfo/networking/bookinfo-gateway.yaml
gateway.networking.istio.io/bookinfo-gateway created
virtualservice.networking.istio.io/bookinfo created
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后验证配置文件：&lt;/p&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ istioctl analyze
✔ No validation issues found when analyzing namespace: default.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;关联到istio网关是指，在istio网关上增加一个代理的配置，将istio网关的特定url转发到示例应用的服务中来。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;现在示例应用已经挂到istio的网关上了，我们需要知道istio网关的访问地址。&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;不同的k8s集群部署方式获取istio网关访问地址的方式不同，本文使用的是&lt;a href=&quot;https://minikube.sigs.k8s.io/docs/start/&quot;&gt;minikube&lt;/a&gt;部署的k8s集群，接下来的介绍我们基于minikube的方式介绍，如果是采用其他方式部署的k8s机器，请参考&lt;a href=&quot;https://istio.io/latest/zh/docs/setup/getting-started/&quot;&gt;官网文档&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
  &lt;li&gt;支持外部访问&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;我们使用minikube部署的k8s集群，实现的原理是在本机的docker上运行了一个镜像：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ docker ps
CONTAINER ID   IMAGE                                 COMMAND                  CREATED        STATUS        PORTS                                                                                                                                  NAMES
ce4e90343fc9   gcr.io/k8s-minikube/kicbase:v0.0.28   &quot;/usr/local/bin/entr…&quot;   46 hours ago   Up 46 hours   127.0.0.1:49172-&amp;gt;22/tcp, 127.0.0.1:49171-&amp;gt;2376/tcp, 127.0.0.1:49170-&amp;gt;5000/tcp, 127.0.0.1:49169-&amp;gt;8443/tcp, 127.0.0.1:49168-&amp;gt;32443/tcp   minikube
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这个docker镜像才是k8s集群的节点（不是本机），因此在本机的docker里我们看不到k8s里运行的容器，这个k8s里的容器，也有docker，真正的k8s内部署的应用，实际上是在容器内的docker运行的，因此想看到k8s上运行的容器，我们得进入到docker容器内运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;docker ps&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;因此，实际上我们现在在本机，访问k8s的容器，已经可以访问到示例应用了，但是要怎么访问呢？&lt;/p&gt;

&lt;p&gt;我们先看看istio网关监听的端口：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ echo $(kubectl -n istio-system get service istio-ingressgateway -o jsonpath=&apos;{.spec.ports[?(@.name==&quot;http2&quot;)].nodePort}&apos;)
32608
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后看看k8s容器的ip地址：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ minikube ip
192.168.49.2
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;那么访问地址如下：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;echo http://$(minikube ip):$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath=&apos;{.spec.ports[?(@.name==&quot;http2&quot;)].nodePort}&apos;)/productpage
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;可以直接使用如下命令访问：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ curl http://$(minikube ip):$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath=&apos;{.spec.ports[?(@.name==&quot;http2&quot;)].nodePort}&apos;)/productpage | grep -o &quot;&amp;lt;title&amp;gt;.*&amp;lt;/title&amp;gt;&quot;
&amp;lt;title&amp;gt;Simple Bookstore App&amp;lt;/title&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;到这一步，如果你是在本机直接部署访问的，到这里即可访问了，但是在开发阶段，我们也有可能不是在本机部署，是在虚拟机部署之后，在开发机上访问，那么这种情况是访问不了的，因为k8s容器监听的端口没有绑定到宿主机上，&lt;/p&gt;

&lt;p&gt;因此要实现其他机器访问示例应用，只能通过宿主机端口转发的方式，端口转发的方法有很多，这里我们使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ncat&lt;/code&gt;来实现：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# 安装ncat
$ yum install nmap-ncat -y 
# 运行端口转发
$ ncat --sh-exec &quot;ncat $(minikube ip) $(kubectl -n istio-system get service istio-ingressgateway -o jsonpath=&apos;{.spec.ports[?(@.name==&quot;http2&quot;)].nodePort}&apos;)&quot; -l 8888  --keep-open 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;现在我们使用宿主机的ip:8888这个地址就可以访问服务了：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/static/img/blog/istio/istio_demo_1.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;查看仪表盘&quot;&gt;查看仪表盘&lt;/h2&gt;

&lt;p&gt;到目前为止，我们完成了istio和bookinfo示例应用的部署，但是还没有感受到istio带来的效果和威力，接下来我们开始部署真正展现istio威力的工具。&lt;/p&gt;

&lt;p&gt;使用如下命令部署istio集成的遥测应用（包括：Kiali，Prometheus，Grafana，Jaeger）：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl apply -f samples/addons
kubectl rollout status deployment/kiali -n istio-system
Waiting for deployment &quot;kiali&quot; rollout to finish: 0 of 1 updated replicas are available...
deployment &quot;kiali&quot; successfully rolled out
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;如果安装插件时出错，再运行一次命令即可，有一些和时间相关的问题，再运行就能解决。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;访问kiali仪表板&quot;&gt;访问Kiali仪表板&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;istioctl dashboard kiali
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;上面的命令运行后，可以直接在本地访问Kiali仪表板，如果我们想在其他机器上访问，则需要指定–address参数来指定Kiali绑定的host，并且可以用–port来制定端口。&lt;/p&gt;

&lt;p&gt;这里为了方便，我们可以使用如下命令截取本机ip：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ ip addr | grep &quot;eth0&quot; | awk &apos;/^[0-9]+: / {}; /inet.*global/ {print gensub(/(.*)\/(.*)/, &quot;\\1&quot;, &quot;g&quot;, $2)}&apos;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;这里eth0是网卡名称，如果你希望获取其他网卡的ip，请自行替换&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;最终我们可以用如下命令运行：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ istioctl dashboard kiali --address $(ip addr | grep &quot;eth0&quot; | awk &apos;/^[0-9]+: / {}; /inet.*global/ {print gensub(/(.*)\/(.*)/, &quot;\\1&quot;, &quot;g&quot;, $2)}&apos;) --port 8003
http://10.224.192.140:8003/kiali
Failed to open browser; open http://10.224.192.140:8003/kiali in your browser.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;此时我们通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;http://10.224.192.140:8003/kiali&lt;/code&gt;就可以访问到Kiali仪表盘了：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/static/img/blog/istio/istio_kiali_1.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/static/img/blog/istio/istio_kiali_2.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;查看prometheus仪表盘&quot;&gt;查看Prometheus仪表盘&lt;/h3&gt;

&lt;p&gt;先验证前面是否已安装Prometheus的插件：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ kubectl -n istio-system get svc prometheus
NAME         CLUSTER-IP     EXTERNAL-IP   PORT(S)    AGE
prometheus   10.59.241.54   &amp;lt;none&amp;gt;        9090/TCP   2m
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这里我们可以看到已经安装了。&lt;/p&gt;

&lt;p&gt;用如下命令可以启动一个代理，在本地访问Prometheus：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ istioctl dashboard prometheus
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;当然，如果需要在其他的机器上访问的话，则需要指定–address参数：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ istioctl dashboard prometheus --address $(ip addr | grep &quot;eth0&quot; | awk &apos;/^[0-9]+: / {}; /inet.*global/ {print gensub(/(.*)\/(.*)/, &quot;\\1&quot;, &quot;g&quot;, $2)}&apos;) --port 8004
http://10.224.192.140:8004
Failed to open browser; open http://10.224.192.140:8004 in your browser.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;现在我们可以通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;http://10.224.192.140:8004&lt;/code&gt;访问Prometheus：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/static/img/blog/istio/istio_promethues_2.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;我们执行一个Prometheus查询，在 web 页面顶部的 “Expression” 对话框中，输入文本：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;istio_requests_total
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;然后点击 Execute 按钮，结果类似如下：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/static/img/blog/istio/istio_promethues_1.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;查看grafana面板&quot;&gt;查看Grafana面板&lt;/h3&gt;

&lt;p&gt;Grafana面板依赖Prometheus，因此要先确认Prometheus已经部署：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ kubectl -n istio-system get svc prometheus
NAME         CLUSTER-IP     EXTERNAL-IP   PORT(S)    AGE
prometheus   10.59.241.54   &amp;lt;none&amp;gt;        9090/TCP   2m
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;再检查Grafana服务是否部署：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ kubectl -n istio-system get svc grafana
NAME      CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
grafana   10.59.247.103   &amp;lt;none&amp;gt;        3000/TCP   2m
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;如果都运行了，可以使用如下命令启动Grafana的外部访问：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;kubectl -n istio-system port-forward $(kubectl -n istio-system get pod -l app=grafana -o jsonpath=&apos;{.items[0].metadata.name}&apos;)  --address=$(ip addr | grep &quot;eth0&quot; | awk &apos;/^[0-9]+: / {}; /inet.*global/ {print gensub(/(.*)\/(.*)/, &quot;\\1&quot;, &quot;g&quot;, $2)}&apos;) 3000:3000 &amp;amp;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;在浏览器中访问&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;http://10.224.192.140:3000&lt;/code&gt;即可打开控制台界面：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/static/img/blog/istio/istio_grafana_1.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;到这里，我们对istio开发版的部署和评估已经基本完成，istio通过sidecar的方式实现服务网格，在sidecar中植入了许多特性，帮助我们实现服务中公共的功能抽离出来，极大减轻了应用的对接负担，但是这种模式本身的部署和管理是非常麻烦的，因此最好在基于k8s的环境上部署和实施。&lt;/p&gt;

</description>
        <pubDate>Wed, 12 Jan 2022 00:00:00 +0000</pubDate>
        <link>http://kael-aiur.com/istio/istio%E9%83%A8%E7%BD%B2%E4%B8%8E%E8%AF%84%E4%BC%B0.html</link>
        <guid isPermaLink="true">http://kael-aiur.com/istio/istio%E9%83%A8%E7%BD%B2%E4%B8%8E%E8%AF%84%E4%BC%B0.html</guid>
        
        
        <category>istio</category>
        
      </item>
    
      <item>
        <title>S/MIME的技术流程</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#前言&quot; id=&quot;markdown-toc-前言&quot;&gt;前言&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#smime协议&quot; id=&quot;markdown-toc-smime协议&quot;&gt;S/MIME协议&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#为什么要使用smime协议&quot; id=&quot;markdown-toc-为什么要使用smime协议&quot;&gt;为什么要使用S/MIME协议&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#smime协议工作流程&quot; id=&quot;markdown-toc-smime协议工作流程&quot;&gt;S/MIME协议工作流程&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#smime证书&quot; id=&quot;markdown-toc-smime证书&quot;&gt;S/MIME证书&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#非对称加密&quot; id=&quot;markdown-toc-非对称加密&quot;&gt;非对称加密&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#签名校验&quot; id=&quot;markdown-toc-签名校验&quot;&gt;签名校验&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#如何确定证书是可信的&quot; id=&quot;markdown-toc-如何确定证书是可信的&quot;&gt;如何确定证书是可信的&lt;/a&gt;        &lt;ul&gt;
          &lt;li&gt;&lt;a href=&quot;#证书链&quot; id=&quot;markdown-toc-证书链&quot;&gt;证书链&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#中间证书&quot; id=&quot;markdown-toc-中间证书&quot;&gt;中间证书&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#证书分发&quot; id=&quot;markdown-toc-证书分发&quot;&gt;证书分发&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;前言&quot;&gt;前言&lt;/h2&gt;

&lt;p&gt;近期在研究S/MIME协议，对于整个协议的大致流程网上有很多说明了，但是却很少有能够详细说明技术细节流程给技术实现提供指导方向的文章，因此在查阅大量文档后，我决定自己整理这样一篇文章来记录一下。&lt;/p&gt;

&lt;h2 id=&quot;smime协议&quot;&gt;S/MIME协议&lt;/h2&gt;

&lt;p&gt;MIME协议是一个互联网标准，全称是多用途互联网邮件扩展（Multipurpose Internet Mail Extensions），它扩展了电子邮件标准，使其能够支撑更多形式的信息（如二进制，声音，图像等），而S/MIME则是在MIME的基础上支持签名和加密，他们的关系类似http和https的关系。&lt;/p&gt;

&lt;h3 id=&quot;为什么要使用smime协议&quot;&gt;为什么要使用S/MIME协议&lt;/h3&gt;

&lt;p&gt;S/MIME主要提供两种安全服务：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;数字签名&lt;/li&gt;
  &lt;li&gt;邮件加密&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;数字签名可以保证邮件内容不被篡改，从而保证邮件的发件人和发件内容不被伪造（这同时也保证可以邮件确实出自发件人，即可以用来作为发件人发件的证据），
邮件加密可以保证邮件内容不会泄露，只有邮件的接收人可以看到邮件的内容，从而保证邮件的安全性。&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;特别说明：S/MIME协议是端对端协议，也就是说，只有发送人和接收人都支持S/MIME协议，才可以使用S/MIME协议收发邮件，如果接收人不支持S/MIME协议，对方会收到一封只有附件的邮件，附件名为：smime.p7m。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&quot;smime协议工作流程&quot;&gt;S/MIME协议工作流程&lt;/h3&gt;

&lt;p&gt;S/MIME分发送方和接受方，发送方需要签名和加密：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://docs.microsoft.com/zh-cn/previous-versions/exchange-server/exchange-server-2000/images/aa995740.3d9893ab-cffa-4783-95d9-fd9d997c1533(exchg.65).gif&quot; alt=&quot;发送方&quot; /&gt;&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;捕获邮件。&lt;/li&gt;
  &lt;li&gt;检索用来唯一标识发件人的信息。&lt;/li&gt;
  &lt;li&gt;检索用来唯一标识收件人的信息。&lt;/li&gt;
  &lt;li&gt;使用发件人的唯一信息对邮件执行签名操作，以产生数字签名。&lt;/li&gt;
  &lt;li&gt;将数字签名附加到邮件中。&lt;/li&gt;
  &lt;li&gt;使用收件人的信息对邮件执行加密操作，以产生加密的邮件。&lt;/li&gt;
  &lt;li&gt;用加密后的邮件替换原始邮件。&lt;/li&gt;
  &lt;li&gt;发送邮件。&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
  &lt;p&gt;这里第2，3，7需要特殊说明一下。&lt;br /&gt;
2中，检索唯一标识发件人的信息，要求发件人要在发件客户端安装私钥，用于邮件签名。&lt;br /&gt;
3中，检索用来唯一标识收件人的信息，需要在发件客户端安装收件人证书，用于邮件加密。&lt;br /&gt;
7中，为了让接收方能够验证签名，需要在邮件中附上发送方的证书。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;对于接受方，需要解密和验证签名：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://docs.microsoft.com/zh-cn/previous-versions/exchange-server/exchange-server-2000/images/aa995740.deab38d5-da66-4c23-add3-a78722c41cb1(exchg.65).gif&quot; alt=&quot;接收方&quot; /&gt;&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;接收邮件。&lt;/li&gt;
  &lt;li&gt;检索加密邮件。&lt;/li&gt;
  &lt;li&gt;检索用来唯一标识收件人的信息。&lt;/li&gt;
  &lt;li&gt;使用收件人的唯一信息对加密邮件执行解密操作，以产生未加密的邮件。&lt;/li&gt;
  &lt;li&gt;返回未加密的邮件。&lt;/li&gt;
  &lt;li&gt;将未加密的邮件返回给收件人。&lt;/li&gt;
  &lt;li&gt;从未加密的邮件中检索数字签名。&lt;/li&gt;
  &lt;li&gt;检索用来标识发件人的信息。&lt;/li&gt;
  &lt;li&gt;使用发件人的信息对未加密的邮件执行签名操作，以产生数字签名。&lt;/li&gt;
  &lt;li&gt;将邮件所附带的数字签名与收到邮件后所产生的数字签名进行比较。&lt;/li&gt;
  &lt;li&gt;如果数字签名匹配，则说明邮件有效。&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
  &lt;p&gt;这里4，9需要进行特殊说明。&lt;br /&gt;
4中，需要在接收端客户端安装接收人的私钥，用来给加密邮件进行解密，只有解密后的邮件才能进行签名校验。&lt;br /&gt;
9中，解密邮件后，可以拿到邮件签名和发送方证书，接收方需要对证书进行验证，验证有效后，即可利用证书的公钥对邮件的签名进行校验，校验通过则说明邮件没有被篡改，是可信的。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;smime证书&quot;&gt;S/MIME证书&lt;/h2&gt;

&lt;p&gt;看了前面的收发信流程，你是否有如下问题？&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;如何保证加密结果只有收件人能解密？&lt;/li&gt;
  &lt;li&gt;使用发件人提供的证书校验签名，如何确认证书不是和邮件一起被篡改的？&lt;/li&gt;
  &lt;li&gt;如何确定证书是可信任的？&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;非对称加密&quot;&gt;非对称加密&lt;/h3&gt;

&lt;p&gt;对于第一个问题，S/MIME协议如何保证邮件内容只有收件人可以解密呢？这里实际上采用的是非对称加密算法。&lt;/p&gt;

&lt;p&gt;在S/MIME协议中，每一个人申请证书时，都会收到至少两个证书：公钥证书和私钥证书，公钥证书是可以公开的，私钥证书是需要自己保存并且不能泄露的。&lt;/p&gt;

&lt;p&gt;在给收件人发送邮件时，需要先获取到收件人公钥，公钥可以先找收件人提供，或者通过服务器获取（协议中没有规定如何获取收件人公钥，因此获取方法也不是固定的）。&lt;/p&gt;

&lt;p&gt;拿到公钥加密后，密文内容就只有收件人的私钥可以解密了，因此能看到密文内容的人，就只有持有收件人私钥的人了。&lt;/p&gt;

&lt;h3 id=&quot;签名校验&quot;&gt;签名校验&lt;/h3&gt;

&lt;p&gt;我们知道，签名算法是私钥签名，公钥按照特定算法计算得到签名，然后比较私钥签名结果和公钥签名，相同即可确认签名有效。&lt;/p&gt;

&lt;p&gt;从这个算法可以看出，当邮件内容签名可以用公钥验证时，至少可以保证邮件内容与私钥-公钥是对应的（如果公钥被篡改，原来的私钥校验肯定不通过），此时我们假设公钥是正确的，没有被篡改，说明邮件内容是可信的，没有被篡改。&lt;/p&gt;

&lt;h3 id=&quot;如何确定证书是可信的&quot;&gt;如何确定证书是可信的&lt;/h3&gt;

&lt;p&gt;在签名校验的环节，我们假设了证书是可靠的，没有被篡改，因此得出邮件内容是可靠的，没有被篡改，那么如何用验证证书是可靠的这个假设呢？&lt;/p&gt;

&lt;p&gt;这里就涉及证书校验的知识了。&lt;/p&gt;

&lt;p&gt;严格来说，证书没有可靠和不可靠的说法，只有信任和不信任，什么样的证书是受信任的，什么样的证书是不受信任的，这里就不仅仅涉及技术问题了，也有一些约定的关系。&lt;/p&gt;

&lt;h4 id=&quot;证书链&quot;&gt;证书链&lt;/h4&gt;

&lt;p&gt;事实上，每一个证书都不是单独存在的，它是在证书链上的叶子结点，打开&lt;a href=&quot;https://www.baidu.com&quot;&gt;百度&lt;/a&gt;，我们可以看到百度的证书链如下：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/static/img/blog/protocol/baidu_cer_chain.png&quot; alt=&quot;百度证书链&quot; /&gt;&lt;/p&gt;

&lt;p&gt;这里我们可以看到三个证书：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;baidu.com&lt;/li&gt;
  &lt;li&gt;GlobalSign Organization Validation CA - SHA256 - G2&lt;/li&gt;
  &lt;li&gt;GlobalSign Root CA&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;这个就是百度的证书链，其中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;baidu.com&lt;/code&gt;我们叫最终证书（end-user certificates），&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GlobalSign Organization Validation CA - SHA256 - G2&lt;/code&gt;我们叫中间证书（intermediates certificates），
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GlobalSign Root CA&lt;/code&gt;我们叫根证书（Root certificates），在一个证书链里，通常可以有一个活着多个中间证书（多层级）。&lt;/p&gt;

&lt;p&gt;实际上，证书的信任是通过这个证书链传递的，当我们检查是否信任一个证书时，会逐步往上层找中间证书，一直找到根证书，如果信任根证书，则可以信任最初的最终证书。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/static/img/blog/protocol/cer_chain_verify.png&quot; alt=&quot;证书链校验&quot; /&gt;&lt;/p&gt;

&lt;p&gt;那根证书怎么来的？我们为什么要信任根证书？&lt;/p&gt;

&lt;p&gt;这里实际上是一些约定关系了，全球有几个特定的厂商，即根证书厂商，所有的根证书都出自这几个厂商（暂且称为根证书厂商），相当于全球约定信任这几个厂商，每个厂商使用自己的根证书去签发中间证书，再由中间证书签发最终证书，最终产生各种各种的证书。&lt;/p&gt;

&lt;p&gt;全球所有的根证书厂商的根证书，都是要预先存储在计算机或者浏览器（firefox）中的，因此在校验证书的过程，只要一层一层往上找，最终找到根证书，只要计算机在根证书库中能找到对应的根证书，则说明该根证书可信，进而证明原来的最终证书可信。&lt;/p&gt;

&lt;p&gt;目前，全球受信根证书企业大致有如下几家：&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Digicert&lt;/strong&gt;&lt;/p&gt;

  &lt;p&gt;DigiCert（原Symantec）是全球首屈一指的网络安全品牌，也是互联网上最受认可和信任的CA机构，其邮件安全证书功能强大、稳定性好，可以提供企业版的S/MIME证书，深受金融、银行、医疗药业等企业的喜爱。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Sectigo&lt;/strong&gt;&lt;/p&gt;

  &lt;p&gt;Sectigo（原Comodo）是全球数字证书市场占有率最高的CA，可提供个人版和企业版的邮件证书，最重要的一点是Sectigo是国际品牌中性价比最高的邮件安全证书品牌，其个人版邮件证书只需148元/年。企业版的S/MIME证书适合中小企业的邮件系统使用。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;GlobalSign&lt;/strong&gt;&lt;/p&gt;

  &lt;p&gt;GlobalSign也是信息安全领域的佼佼者，在中国深受各大企业欢迎，尤其是电商行业和互联网企业。目前常用的有个人版、部门版和企业版三种邮件安全证书，可以满足不同用户需求。GlobalSign的S/MIME证书价格跟DigiCert差不多，主要服务中高端用户群体。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4 id=&quot;中间证书&quot;&gt;中间证书&lt;/h4&gt;

&lt;p&gt;现在我们知道，如果想知道最终证书是否可信，只要找到可信的根证书即可，那么如何找到根证书呢？在多层级的证书链中，我们只有最终证书，需要逐层网上寻找中间证书进行验证，一般来说，应用在校验证书时会有两种方式处理缺失的中间证书：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;根据证书信息中提供的上一级中间证书查找并下载中间证书，然后递归寻找直到找到根证书&lt;/li&gt;
  &lt;li&gt;直接提醒中间证书丢失，让用户自己安装&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;对于 自动查找并下载中间证书的方式，应用可以在证书中找到上级签发证书的下载地址：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/static/img/blog/protocol/cer_issure_download.png&quot; alt=&quot;自动下载中间证书&quot; /&gt;。&lt;/p&gt;

&lt;h2 id=&quot;证书分发&quot;&gt;证书分发&lt;/h2&gt;

&lt;p&gt;现在我们知道S/MIME完整的签名加密-解密校验流程了，那么用户如何获取到自己的证书呢？&lt;/p&gt;

&lt;p&gt;通常来说，用户如果需要一个全球信任的证书，就需要去前面提到的几个根证书厂商申请，当然，国内也有很多中间证书厂商可以申请个人证书，大部分情况下都是要收费的（看证书类型，一般DV证书不收费，OV和EV收费）。&lt;/p&gt;

&lt;p&gt;个人申请证书之后，如何分发自己的公钥？&lt;/p&gt;

&lt;p&gt;这是一个比较麻烦的问题，S/MIME并没有给出证书分发的过程，因此为了方便用户使用，我们通常需要一个证书和密钥的管理分发系统，称为电子密钥管理系统（Electronic Key Management System， EKMS）。&lt;/p&gt;

&lt;p&gt;目前业内并没有规范指导证书分发的方式，因此各个邮件服务商都是自己开发的证书交换服务来简化用户分发证书的过程。&lt;/p&gt;

&lt;p&gt;S/MIME常用的方式是先由第一个用户发送一封只有签名，没有加密的邮件给第二个用户，第二个用户就可以通过这封邮件拿到第一个用户的证书了，然后就可以给第一个用户发送加密邮件了，第一个用户收到第二个用户的加密邮件后，用自己的私钥解密就可以拿到第二个用户的证书了。&lt;/p&gt;

</description>
        <pubDate>Mon, 27 Dec 2021 00:00:00 +0000</pubDate>
        <link>http://kael-aiur.com/%E7%BD%91%E7%BB%9C%E5%8D%8F%E8%AE%AE/SMIME%E5%8D%8F%E8%AE%AE%E7%9A%84%E6%8A%80%E6%9C%AF%E6%B5%81%E7%A8%8B.html</link>
        <guid isPermaLink="true">http://kael-aiur.com/%E7%BD%91%E7%BB%9C%E5%8D%8F%E8%AE%AE/SMIME%E5%8D%8F%E8%AE%AE%E7%9A%84%E6%8A%80%E6%9C%AF%E6%B5%81%E7%A8%8B.html</guid>
        
        <category>S/MIME,rfc7662</category>
        
        
        <category>网络协议</category>
        
      </item>
    
  </channel>
</rss>
