{"id":10006348,"date":"2022-08-30T22:00:55","date_gmt":"2022-08-30T14:00:55","guid":{"rendered":"https:\/\/ityet.com\/?p=10006348"},"modified":"2022-08-30T22:00:55","modified_gmt":"2022-08-30T14:00:55","slug":"%e4%b8%80%e6%96%87%e5%bf%ab%e9%80%9f%e4%b8%8a%e6%89%8b-nacos-%e6%b3%a8%e5%86%8c%e4%b8%ad%e5%bf%83%e9%85%8d%e7%bd%ae%e4%b8%ad%e5%bf%83%ef%bc%81","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006348","title":{"rendered":"\u4e00\u6587\u5feb\u901f\u4e0a\u624b Nacos \u6ce8\u518c\u4e2d\u5fc3+\u914d\u7f6e\u4e2d\u5fc3\uff01"},"content":{"rendered":"<p>Spring Cloud Alibaba \u662f\u963f\u91cc\u5df4\u5df4\u63d0\u4f9b\u7684\u4e00\u7ad9\u5f0f\u5fae\u670d\u52a1\u5f00\u53d1\u89e3\u51b3\u65b9\u6848\uff0c\u76ee\u524d\u5df2\u88ab Spring Cloud \u5b98\u65b9\u6536\u5f55\u3002\u800c Nacos \u4f5c\u4e3a Spring Cloud Alibaba \u7684\u6838\u5fc3\u7ec4\u4ef6\u4e4b\u4e00\uff0c\u63d0\u4f9b\u4e86\u4e24\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u529f\u80fd\uff1a\u6ce8\u518c\u4e2d\u5fc3\u548c\u914d\u7f6e\u4e2d\u5fc3\uff0c\u6211\u4eec\u4eca\u5929\u6765\u4e86\u89e3\u548c\u5b9e\u73b0\u4e00\u4e0b\u4e8c\u8005\u3002<\/p>\n<h2>1.Nacos \u7b80\u4ecb<\/h2>\n<p>Nacos \u81f4\u529b\u4e8e\u5e2e\u52a9\u5f00\u53d1\u8005\u53d1\u73b0\u3001\u914d\u7f6e\u548c\u7ba1\u7406\u5fae\u670d\u52a1\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u7ec4\u7b80\u5355\u6613\u7528\u7684\u7279\u6027\u96c6\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u5feb\u901f\u5b9e\u73b0\u52a8\u6001\u670d\u52a1\u53d1\u73b0\u3001\u670d\u52a1\u914d\u7f6e\u3001\u670d\u52a1\u5143\u6570\u636e\u53ca\u6d41\u91cf\u7ba1\u7406\u3002<\/p>\n<h3>Nacos \u7279\u6027\u4ecb\u7ecd<\/h3>\n<p>Nacos \u5177\u6709\u4ee5\u4e0b\u7279\u6027\uff1a<\/p>\n<ul>\n<li>\u670d\u52a1\u53d1\u73b0\u548c\u670d\u52a1\u5065\u5eb7\u76d1\u6d4b\uff1a\u652f\u6301\u57fa\u4e8eDNS\u548c\u57fa\u4e8eRPC\u7684\u670d\u52a1\u53d1\u73b0\uff0c\u652f\u6301\u5bf9\u670d\u52a1\u7684\u5b9e\u65f6\u7684\u5065\u5eb7\u68c0\u67e5\uff0c\u963b\u6b62\u5411\u4e0d\u5065\u5eb7\u7684\u4e3b\u673a\u6216\u670d\u52a1\u5b9e\u4f8b\u53d1\u9001\u8bf7\u6c42\u3002<\/li>\n<li>\u52a8\u6001\u914d\u7f6e\u670d\u52a1\uff1a\u52a8\u6001\u914d\u7f6e\u670d\u52a1\u53ef\u4ee5\u8ba9\u60a8\u4ee5\u4e2d\u5fc3\u5316\u3001\u5916\u90e8\u5316\u548c\u52a8\u6001\u5316\u7684\u65b9\u5f0f\u7ba1\u7406\u6240\u6709\u73af\u5883\u7684\u5e94\u7528\u914d\u7f6e\u548c\u670d\u52a1\u914d\u7f6e\u3002<\/li>\n<li>\u52a8\u6001 DNS \u670d\u52a1\uff1a\u52a8\u6001 DNS \u670d\u52a1\u652f\u6301\u6743\u91cd\u8def\u7531\uff0c\u8ba9\u60a8\u66f4\u5bb9\u6613\u5730\u5b9e\u73b0\u4e2d\u95f4\u5c42\u8d1f\u8f7d\u5747\u8861\u3001\u66f4\u7075\u6d3b\u7684\u8def\u7531\u7b56\u7565\u3001\u6d41\u91cf\u63a7\u5236\u4ee5\u53ca\u6570\u636e\u4e2d\u5fc3\u5185\u7f51\u7684\u7b80\u5355 DNS \u89e3\u6790\u670d\u52a1\u3002<\/li>\n<li>\u670d\u52a1\u53ca\u5176\u5143\u6570\u636e\u7ba1\u7406\uff1a\u652f\u6301\u4ece\u5fae\u670d\u52a1\u5e73\u53f0\u5efa\u8bbe\u7684\u89c6\u89d2\u7ba1\u7406\u6570\u636e\u4e2d\u5fc3\u7684\u6240\u6709\u670d\u52a1\u53ca\u5143\u6570\u636e\u3002<\/li>\n<\/ul>\n<h2>2.\u6ce8\u518c\u4e2d\u5fc3\u5b9e\u73b0<\/h2>\n<p>\u6ce8\u518c\u4e2d\u5fc3\u6709\u4e24\u4e2a\u91cd\u8981\u7684\u529f\u80fd\uff1a\u670d\u52a1\u6ce8\u518c\u548c\u670d\u52a1\u53d1\u73b0\uff0c\u5b83\u89e3\u51b3\u4e86\u5fae\u670d\u52a1\u96c6\u7fa4\u4e2d\uff0c\u8c03\u7528\u8005\u548c\u670d\u52a1\u63d0\u4f9b\u8005\u8fde\u63a5\u7ba1\u7406\u548c\u8bf7\u6c42\u8f6c\u53d1\u7684\u529f\u80fd\uff0c\u8ba9\u7a0b\u5e8f\u7684\u5f00\u53d1\u8005\u65e0\u9700\u8fc7\u591a\u7684\u5173\u6ce8\u670d\u52a1\u63d0\u4f9b\u8005\u7684\u7a33\u5b9a\u6027\u548c\u5065\u5eb7\u7a0b\u5ea6\u4ee5\u53ca\u8c03\u7528\u5730\u5740\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u90fd\u53ef\u4ee5\u4f9d\u9760 Nacos \u8fdb\u884c\u76d1\u6d4b\u3001\u7ba1\u7406\u548c\u81ea\u52a8\u8f6c\u53d1\u3002<\/p>\n<p>\u6ce8\u518c\u4e2d\u5fc3\u4e2d\u6709\u4e24\u4e2a\u89d2\u8272\uff1a\u4e00\u4e2a\u662f\u670d\u52a1\u63d0\u4f9b\u8005 Provider\uff0c\u53e6\u4e00\u4e2a\u662f\u670d\u52a1\u8c03\u7528\u8005 Consumer\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u5206\u522b\u6765\u521b\u5efa\u4e8c\u8005\u3002<\/p>\n<h3>2.1 \u521b\u5efa\u670d\u52a1\u63d0\u4f9b\u8005<\/h3>\n<h4>2.1.1 \u65b0\u5efa\u9879\u76ee\u5e76\u6dfb\u52a0\u4f9d\u8d56<\/h4>\n<p>\u65b0\u5efa\u4e00\u4e2a Spring Boot \u9879\u76ee\uff0c\u4f7f\u7528\u963f\u91cc\u4e91\u5730\u5740 <a href=\"https:\/\/link.segmentfault.com\/?enc=mU90NiJPMV5%2FFfYooHpGCQ%3D%3D.3SBWrJPVMvZmhFjDftW%2BBRWhcm7QcFwX5SpZ70qfS8w%3D\" rel=\"nofollow\">http:\/\/start.aliyun.com<\/a> \u6765\u521b\u5efa Spring Cloud Alibaba Nacos \u9879\u76ee\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<br \/>  <img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042405756\" alt=\"\" title=\"\" loading=\"lazy\"><br \/>\u70b9\u51fb Next \u4e0b\u4e00\u6b65\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<br \/>  <img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042405757\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><br \/>\u589e\u52a0 Nacos Service Discovery \u6846\u67b6\u652f\u6301\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<br \/>  <img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042405758\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><br \/>\u5bf9\u5e94\u7684\u4f9d\u8d56\u6846\u67b6\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"xml\">&lt;dependency&gt;\n  &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n  &lt;artifactId&gt;spring-boot-starter-web&lt;\/artifactId&gt;\n&lt;\/dependency&gt;\n&lt;dependency&gt;\n  &lt;groupId&gt;com.alibaba.cloud&lt;\/groupId&gt;\n  &lt;artifactId&gt;spring-cloud-starter-alibaba-nacos-discovery&lt;\/artifactId&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<blockquote><p>PS\uff1a\u6dfb\u52a0 Spring Web\uff08Spring MVC\uff09\u6846\u67b6\u4e3b\u8981\u662f\u4e3a\u4e86\u65b9\u4fbf\u540e\u9762\u6d4b\u8bd5\u3002<\/p><\/blockquote>\n<h4>2.1.2 \u914d\u7f6e Nacos \u8fde\u63a5\u4fe1\u606f<\/h4>\n<p>\u5728\u914d\u7f6e\u6587\u4ef6 application.properties \u4e2d\u8981\u586b\u5199 Nacos \u7684\u76f8\u5173\u8fde\u63a5\u4fe1\u606f\uff0c\u5177\u4f53\u4fe1\u606f\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"xml\"># \u5e94\u7528\u540d\u79f0\uff08\u4e5f\u662f Nacos \u4e2d\u7684\u670d\u52a1\u540d\uff09\nspring.application.name=spring-cloud-nacos-producer\n# \u5e94\u7528\u670d\u52a1 WEB \u8bbf\u95ee\u7aef\u53e3\nserver.port=8082\n# Nacos\u8ba4\u8bc1\u4fe1\u606f\nspring.cloud.nacos.discovery.username=nacos\nspring.cloud.nacos.discovery.password=nacos\n# Nacos \u670d\u52a1\u53d1\u73b0\u4e0e\u6ce8\u518c\u914d\u7f6e\uff0c\u5176\u4e2d\u5b50\u5c5e\u6027 server-addr \u6307\u5b9a Nacos \u670d\u52a1\u5668\u4e3b\u673a\u548c\u7aef\u53e3\nspring.cloud.nacos.discovery.server-addr=127.0.0.1:8848\n# \u6ce8\u518c\u5230 nacos \u7684\u6307\u5b9a namespace\uff0c\u9ed8\u8ba4\u4e3a public\nspring.cloud.nacos.discovery.namespace=public<\/code><\/pre>\n<h4>2.1.3 \u6dfb\u52a0\u670d\u52a1\u63d0\u4f9b\u65b9\u6cd5<\/h4>\n<p>\u65b0\u5efa\u4e00\u4e2a\u63a7\u5236\u5668 Controller\uff0c\u6dfb\u52a0\u4e00\u4e2a sayhi \u65b9\u6cd5\uff0c\u6b64\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u7528 HTTP \u534f\u8bae\u8fdb\u884c\u8bbf\u95ee\uff0c\u5b83\u662f\u4e3a\u540e\u9762\u7684\u670d\u52a1\u6d88\u8d39\u8005\u63d0\u4f9b\u7684\u8c03\u7528\u65b9\u6cd5\uff0c\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">import org.springframework.boot.SpringApplication;\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\nimport org.springframework.cloud.client.discovery.EnableDiscoveryClient;\nimport org.springframework.web.bind.annotation.PathVariable;\nimport org.springframework.web.bind.annotation.RequestMapping;\nimport org.springframework.web.bind.annotation.RestController;\n\n@SpringBootApplication\n@RestController\npublic class SpringCloudNacosProducerApplication {\n    public static void main(String[] args) {\n        SpringApplication.run(SpringCloudNacosProducerApplication.class, args);\n    }\n    @RequestMapping(&quot;\/sayhi\/{name}&quot;)\n    public String sayHi(@PathVariable String name) {\n        return &quot;Hi Nacos Discovery &quot; + name;\n    }\n}<\/code><\/pre>\n<p>\u7f16\u5199\u5b8c\u4ee3\u7801\u4e4b\u540e\uff0c\u8fd0\u884c\u9879\u76ee\u5c31\u53ef\u4ee5\u5728 Nacos \u7684\u670d\u52a1\u5217\u8868\u4e2d\u770b\u5230\u5b83\u4e86\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042405759\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><br \/>\u7ecf\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u7684\u670d\u52a1\u63d0\u4f9b\u8005\u5c31\u521b\u5efa\u597d\u4e86\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u670d\u52a1\u6d88\u8d39\u8005 Consumer\u3002<\/p>\n<h3>2.2 \u521b\u5efa\u670d\u52a1\u6d88\u8d39\u8005<\/h3>\n<h4>2.2.1 \u65b0\u5efa\u9879\u76ee\u5e76\u6dfb\u52a0\u4f9d\u8d56<\/h4>\n<p>\u6b64\u6b65\u9aa4\u548c\u4e0a\u9762\u7684\u670d\u52a1\u63d0\u4f9b\u8005\u7c7b\u4f3c\uff0c\u4e5f\u662f\u6dfb\u52a0 nacos discovery \u548c web \u4f9d\u8d56\uff0c\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042405760\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042405761\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/p>\n<h4>2.2.2 \u914d\u7f6e Nacos \u8fde\u63a5\u4fe1\u606f<\/h4>\n<pre><code class=\"xml\"># \u5e94\u7528\u540d\u79f0\nspring.application.name=springcloud-nacos-consumer\n# \u5e94\u7528\u670d\u52a1 WEB \u8bbf\u95ee\u7aef\u53e3\nserver.port=8082\n# Nacos\u8ba4\u8bc1\u4fe1\u606f\nspring.cloud.nacos.discovery.username=nacos\nspring.cloud.nacos.discovery.password=nacos\n# Nacos \u670d\u52a1\u53d1\u73b0\u4e0e\u6ce8\u518c\u914d\u7f6e\uff0c\u5176\u4e2d\u5b50\u5c5e\u6027 server-addr \u6307\u5b9a Nacos \u670d\u52a1\u5668\u4e3b\u673a\u548c\u7aef\u53e3\nspring.cloud.nacos.discovery.server-addr=127.0.0.1:8848\n# \u6ce8\u518c\u5230 nacos \u7684\u6307\u5b9a namespace\uff0c\u9ed8\u8ba4\u4e3a public\nspring.cloud.nacos.discovery.namespace=public<\/code><\/pre>\n<h4>2.2.3 \u6dfb\u52a0\u670d\u52a1\u8c03\u7528\u4ee3\u7801<\/h4>\n<p>\u670d\u52a1\u6d88\u8d39\u8005\u7684\u5b9e\u73b0\u6709\u4e24\u4e2a\u5173\u952e\u70b9\uff0c\u7b2c\u4e00\uff0c\u5148 new \u4e00\u4e2a RestTemplate \u5bf9\u8c61\uff0c\u6b64\u5bf9\u8c61\u662f Spring \u6846\u67b6\u63d0\u4f9b\u7528\u4e8e\u8fdb\u884c HTTP \u8bf7\u6c42\u7684\u7c7b\uff0c\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">import org.springframework.boot.SpringApplication;\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\nimport org.springframework.cloud.client.discovery.EnableDiscoveryClient;\nimport org.springframework.cloud.client.loadbalancer.LoadBalanced;\nimport org.springframework.context.annotation.Bean;\nimport org.springframework.web.client.RestTemplate;\n\n@SpringBootApplication\n@EnableDiscoveryClient\npublic class SpringcloudNacosConsumerApplication {\n    public static void main(String[] args) {\n        SpringApplication.run(SpringcloudNacosConsumerApplication.class, args);\n    }\n    @LoadBalanced\n    @Bean\n    public RestTemplate restTemplate() {\n        \/\/ \u7528\u4e8e\u8fdb\u884c HTTP \u8bf7\u6c42\u7684\u5bf9\u8c61\n        return new RestTemplate();\n    }\n}<\/code><\/pre>\n<p>\u7b2c\u4e8c\uff0c\u65b0\u5efa\u4e00\u4e2a\u63a7\u5236\u5668\uff0c\u6ce8\u5165 RestTemplate \u5bf9\u8c61\uff0c\u5e76\u8c03\u7528 Nacos \u4e2d\u7684\u670d\u52a1\u63d0\u4f9b\u8005\u63a5\u53e3\uff0c\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">import org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.web.bind.annotation.RequestMapping;\nimport org.springframework.web.bind.annotation.RestController;\nimport org.springframework.web.client.RestTemplate;\n\n@RestController\npublic class TestController {\n    @Autowired\n    private RestTemplate restTemplate;\n\n    @RequestMapping(&quot;\/hi&quot;)\n    public String hi(String name) {\n        \/\/ \u8c03\u7528\u751f\u4ea7\u8005 sayhi \u65b9\u6cd5\uff0c\u5e76\u8fd4\u56de\u7ed3\u679c\n        return restTemplate.getForObject(&quot;http:\/\/spring-cloud-nacos-producer\/sayhi\/&quot; + name,\n                String.class);\n    }\n}<\/code><\/pre>\n<blockquote><p>\u5176\u4e2d spring-cloud-nacos-producer \u662f\u670d\u52a1\u63d0\u4f9b\u8005\u7684 ID\uff0c\u800c sayhi \u662f\u670d\u52a1\u63d0\u4f9b\u8005\u63d0\u4f9b\u7684\u63a5\u53e3\u5730\u5740\u3002<\/p><\/blockquote>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u914d\u7f6e\uff0c\u6211\u4eec\u7684\u670d\u52a1\u6d88\u8d39\u8005\u4e5f\u521b\u5efa\u597d\u4e86\uff0c\u542f\u52a8\u9879\u76ee\uff0c\u6267\u884c\u7ed3\u679c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042405762\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><br \/>\u4ece\u4e0a\u8ff0\u7ed3\u679c\u53ef\u4ee5\u770b\u51fa\uff0c\u670d\u52a1\u6d88\u8d39\u8005\u901a\u8fc7 Nacos \u5df2\u7ecf\u6210\u529f\u8c03\u7528\u5230\u670d\u52a1\u63d0\u4f9b\u8005\u4e86\uff0c\u8fd9\u5c31\u662f Nacos \u4e2d\u6ce8\u518c\u4e2d\u5fc3\u7684\u57fa\u672c\u4f7f\u7528\u3002<\/p>\n<h2>3.\u914d\u7f6e\u4e2d\u5fc3\u5b9e\u73b0<\/h2>\n<p>\u914d\u7f6e\u4e2d\u5fc3\u7684\u4f5c\u7528\u662f\u5c06\u672c\u5730\u914d\u7f6e\u6587\u4ef6\u4e91\u7aef\u8bdd\uff0c\u6240\u8c13\u7684\u4e91\u7aef\u4e5f\u5c31\u662f Nacos \u7684\u670d\u52a1\u5668\u7aef\uff0c\u8fd9\u6837\u65e2\u80fd\u4fdd\u8bc1\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u654f\u611f\u6570\u636e\u4e0d\u4f1a\u66b4\u9732\uff0c\u540c\u65f6\u53c8\u63d0\u4f9b\u4e86\u5b9e\u65f6\u7684\u4fee\u6539\u3001\u67e5\u770b\u3001\u56de\u6eda\u548c\u52a8\u6001\u5237\u65b0\u914d\u7f6e\u6587\u4ef6\u7684\u529f\u80fd\uff0c\u975e\u5e38\u5b9e\u7528\u3002<\/p>\n<p>\u4f46\u662f\u9700\u8981\u6ce8\u610f\u7684\u662f<strong>\u914d\u7f6e\u4e2d\u5fc3\u548c\u6ce8\u518c\u4e2d\u5fc3\u7684\u4f9d\u8d56\u5305\u662f\u4e0d\u540c\u7684<\/strong>\uff0c\u6ce8\u518c\u4e2d\u5fc3\u7684\u4f9d\u8d56\u5305\u662f nacos discovery\uff0c\u800c\u914d\u7f6e\u4e2d\u5fc3\u7684\u4f9d\u8d56\u5305\u662f nacos config\uff0c\u5b83\u7684\u5177\u4f53\u5982\u4e0b\u3002<\/p>\n<h3>3.1 \u65b0\u5efa\u9879\u76ee\u5e76\u6dfb\u52a0\u4f9d\u8d56<\/h3>\n<p>\u521b\u5efa\u4e00\u4e2a Spring Boot \u9879\u76ee\uff0c\u6dfb\u52a0 nacos config \u548c web \u4f9d\u8d56\u5305\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042391396\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/p>\n<blockquote><p>PS\uff1a\u6dfb\u52a0 web \u4f9d\u8d56\u4e3b\u8981\u662f\u4e3a\u4e86\u65b9\u4fbf\u540e\u9762\u6d4b\u8bd5\u3002<\/p><\/blockquote>\n<p>\u5bf9\u5e94\u7684\u914d\u7f6e\u4f9d\u8d56\u4fe1\u606f\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"xml\">&lt;dependency&gt;\n  &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n  &lt;artifactId&gt;spring-boot-starter-web&lt;\/artifactId&gt;\n&lt;\/dependency&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;com.alibaba.cloud&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-cloud-starter-alibaba-nacos-config&lt;\/artifactId&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<h3>3.2 \u914d\u7f6e Nacos Config \u4fe1\u606f<\/h3>\n<p>\u5728\u5e94\u7528\u7684 \/src\/main\/resources\/ \u76ee\u5f55\u4e0b\uff0c\u521b\u5efa\u5f15\u5bfc\u914d\u7f6e\u6587\u4ef6 bootstrap.yml\uff08\u6216 bootstrap.properties\uff09\uff0c\u6dfb\u52a0\u4ee5\u4e0b Nacos Config \u914d\u7f6e\uff1a<\/p>\n<pre><code class=\"yaml\">spring:\n  application:\n    name: nacosconfig # \u9879\u76ee\u540d\u79f0\u548c nacos DataId \u76f8\u5339\u914d\n  cloud:\n    nacos:\n      config:\n        server-addr: 127.0.0.1:8848 # nacos \u5730\u5740\u548c\u7aef\u53e3\n        file-extension: yaml # \u83b7\u53d6\u914d\u7f6e\u6587\u4ef6\u7684\u683c\u5f0f\uff1ayaml\n        username: nacos # nacos \u8ba4\u8bc1\u7528\u6237\u540d\n        password: nacos # nacos \u8ba4\u8bc1\u5bc6\u7801\nserver:\n  port: 9001 # \u9879\u76ee\u542f\u52a8\u7aef\u53e3<\/code><\/pre>\n<h3>3.3 \u7f16\u5199\u4ee3\u7801\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6<\/h3>\n<p>\u65b0\u5efa\u63a7\u5236\u5668\uff0c\u4f7f\u7528 @Value \u6ce8\u89e3\u8bfb\u53d6\u914d\u7f6e\u4fe1\u606f\uff0c\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">import org.springframework.beans.factory.annotation.Value;\nimport org.springframework.web.bind.annotation.GetMapping;\nimport org.springframework.web.bind.annotation.RestController;\n\n@RestController\npublic class TestController {\n    \/\/ \u4ece nacos \u4e2d\u8bfb\u53d6\u914d\u7f6e\u9879 config.info\n    @Value(&quot;${config.info}&quot;)\n    private String configInfo;\n\n    @GetMapping(&quot;\/getconfig&quot;)\n    public String getConfigInfo(){\n        return configInfo;\n    }\n}<\/code><\/pre>\n<h3>3.4 Nacos \u63a7\u5236\u53f0\u6dfb\u52a0\u914d\u7f6e\u4fe1\u606f<\/h3>\n<p>\u5728 Nacos \u63a7\u5236\u53f0\u521b\u5efa\u5e76\u8bbe\u7f6e\u914d\u7f6e\u6587\u4ef6\uff0c\u6267\u884c\u6b65\u9aa4\u5982\u4e0b\u6240\u793a\u3002<br \/>\u9996\u5148\uff0c\u5728\u914d\u7f6e\u5217\u8868\u4e2d\u70b9\u51fb\u201c\u6dfb\u52a0\u201d\u6309\u94ae\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042391397\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><br \/>\u8fdb\u5165\u914d\u7f6e\u9875\u9762\uff0c\u65b0\u5efa YAML \u6216 Properties \u914d\u7f6e\u6587\u4ef6\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042391398\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><br \/>\u4ee5\u4e0a\u5173\u952e\u53c2\u6570\u7684\u542b\u4e49\u548c\u89c4\u5219\u8bf4\u660e\u5982\u4e0b\u3002<\/p>\n<h4>3.4.1 Data ID<\/h4>\n<p>Data ID \u7684\u62fc\u63a5\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<blockquote><p>${prefix} &#8211; ${spring.profiles.active} . ${file-extension}<\/p><\/blockquote>\n<p>\u5176\u4e2d<\/p>\n<ul>\n<li>prefix \u9ed8\u8ba4\u4e3a spring.application.name \u7684\u503c\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u914d\u7f6e\u9879 spring.cloud.nacos.config.prefix \u6765\u914d\u7f6e\u3002<\/li>\n<li>spring.profiles.active \u5373\u4e3a\u5f53\u524d\u73af\u5883\u5bf9\u5e94\u7684 profile\uff0c<strong>\u5f53 active profile \u4e3a\u7a7a\u65f6\uff0c\u5bf9\u5e94\u7684\u8fde\u63a5\u7b26 &#8211; \u4e5f\u5c06\u4e0d\u5b58\u5728\uff0cdataId \u7684\u62fc\u63a5\u683c\u5f0f\u53d8\u6210 ${prefix}.${file-extension}<\/strong><\/li>\n<li>file-extension \u4e3a\u914d\u7f6e\u5185\u5bb9\u7684\u6570\u636e\u683c\u5f0f\uff0c\u53ef\u4ee5\u901a\u8fc7\u914d\u7f6e\u9879 spring.cloud.nacos.config.file-extension \u6765\u914d\u7f6e\u3002\u76ee\u524d\u53ea\u652f\u6301 properties \u7c7b\u578b\u3002<\/li>\n<\/ul>\n<h4>3.4.2 Group<\/h4>\n<p>Group \u5206\u7ec4\u9009\u9879\uff0c\u4e3b\u8981\u662f\u7528\u6765\u9694\u79bb\u4e0d\u540c\u7684\u914d\u7f6e\u9879\u76ee\u7684\uff0c\u5b83\u7684\u9ed8\u8ba4\u503c\u4e3a DEFAULT_GROUP\uff0c\u53ef\u4ee5\u901a\u8fc7 spring.cloud.nacos.config.group \u914d\u7f6e\u3002<\/p>\n<p>\u914d\u7f6e\u597d\u76f8\u5e94\u7684\u5185\u5bb9\u4e4b\u540e\uff0c\u70b9\u51fb\u5e95\u90e8\u7684\u201c\u53d1\u5e03\u201d\u6309\u94ae\u5373\u53ef\uff0c\u6dfb\u52a0\u6210\u529f\u4e4b\u540e\u4f1a\u81ea\u52a8\u8fd4\u56de\u914d\u7f6e\u5217\u8868\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042391399\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><br \/>\u7ecf\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0cNacos \u914d\u7f6e\u4e2d\u5fc3\u7684\u529f\u80fd\u5c31\u5b9e\u73b0\u5b8c\u4e86\uff0c\u63a5\u4e0b\u6765\u542f\u52a8\u9879\u76ee\uff0c\u7a0b\u5e8f\u7684\u6267\u884c\u7ed3\u679c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042391400\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/p>\n<h3>3.5 \u52a8\u6001\u5237\u65b0\u529f\u80fd<\/h3>\n<p><strong>\u52a8\u6001\u5237\u65b0\u529f\u80fd\u662f\u6307\uff0c\u5728 Nacos \u914d\u7f6e\u4e2d\u5fc3\u4fee\u6539\u4e86\u914d\u7f6e\u6587\u4ef6\uff0c\u5728\u4e0d\u91cd\u542f\u9879\u76ee\u7684\u524d\u63d0\u4e0b\uff0c\u53ef\u4ee5\u5b9e\u65f6\u8bfb\u53d6\u5230\u6700\u65b0\u7684\u914d\u7f6e\u5185\u7f6e\u3002<\/strong><\/p>\n<p>Nacos \u9ed8\u8ba4\u4f1a\u4e3a\u6240\u6709\u83b7\u53d6\u6570\u636e\u6210\u529f\u7684 Nacos \u7684\u914d\u7f6e\u9879\u6dfb\u52a0\u4e86\u76d1\u542c\u529f\u80fd\uff0c\u5728\u76d1\u542c\u5230\u670d\u52a1\u7aef\u914d\u7f6e\u53d1\u751f\u53d8\u5316\u65f6\u4f1a\u5b9e\u65f6\u89e6\u53d1 org.springframework.cloud.context.refresh.ContextRefresher \u7684 refresh \u65b9\u6cd5\u3002<\/p>\n<p>\u4f46<strong>\u5982\u679c\u9700\u8981\u5bf9 Bean \u8fdb\u884c\u52a8\u6001\u5237\u65b0<\/strong>\uff0c\u9700\u8981\u53c2\u7167 Spring \u548c Spring Cloud \u89c4\u8303\uff0c<strong>\u63a8\u8350\u7ed9\u7c7b\u6dfb\u52a0 @RefreshScope \u6216 @ConfigurationProperties \u6ce8\u89e3<\/strong>\uff0c\u5c31\u53ef\u4ee5\u5b9e\u73b0\u914d\u7f6e\u4e2d\u5fc3\u7684\u52a8\u6001\u5237\u65b0\u529f\u80fd\u4e86\u3002<\/p>\n<h2>4.\u9879\u76ee\u6e90\u7801<\/h2>\n<p><a href=\"https:\/\/link.segmentfault.com\/?enc=8KQrx5xpwSJPiEEG84FHTg%3D%3D.z2lSVrZpEJxlRtvo2emkndQ%2FsaXF8kcgkxv%2FGfaBs28sHhLBHy0v%2FmUeU%2FG%2BkTX2oypXgEikRvRKXX7uyNS%2Beg%3D%3D\" rel=\"nofollow\">https:\/\/gitee.com\/mydb\/spring-cloud-alibaba-example<\/a><\/p>\n<h2>\u5c0f\u7ed3<\/h2>\n<p>Nacos \u4f5c\u4e3a Spring Cloud Alibaba \u7684\u6838\u5fc3\u7ec4\u4ef6\u4e4b\u4e00\uff0c\u63d0\u4f9b\u4e86\u4e24\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u529f\u80fd\uff1a\u6ce8\u518c\u4e2d\u5fc3\u548c\u914d\u7f6e\u4e2d\u5fc3\u3002\u6ce8\u518c\u4e2d\u5fc3\u6709\u4e24\u4e2a\u91cd\u8981\u7684\u529f\u80fd\uff1a\u670d\u52a1\u6ce8\u518c\u548c\u670d\u52a1\u53d1\u73b0\uff0c\u5b83\u89e3\u51b3\u4e86\u5fae\u670d\u52a1\u96c6\u7fa4\u4e2d\uff0c\u8c03\u7528\u8005\u548c\u670d\u52a1\u63d0\u4f9b\u8005\u8fde\u63a5\u7ba1\u7406\u548c\u8bf7\u6c42\u8f6c\u53d1\u7684\u529f\u80fd\uff0c\u4fdd\u8bc1\u4e86\u670d\u52a1\u8c03\u7528\u8005\u80fd\u591f\u7a33\u5b9a\u7684\u8c03\u7528\u5230\u5065\u5eb7\u7684\u670d\u52a1\u3002\u800c\u6ce8\u518c\u4e2d\u5fc3\u7684\u672c\u8d28\u662f\u5c06\u9879\u76ee\u4e2d\u7684\u672c\u5730\u914d\u7f6e\u6587\u4ef6\u4e91\u7aef\u5316\uff0c\u89e3\u51b3\u4e86\u914d\u7f6e\u6587\u4ef6\u7684\u5b89\u5168\u6027\u4e0e\u7edf\u4e00\u6027\u7684\u95ee\u9898\uff0c\u5e76\u4e14\u63d0\u4f9b\u4e86\u914d\u7f6e\u6587\u4ef6\u5386\u53f2\u7248\u672c\u56de\u6eda\u548c\u914d\u7f6e\u6587\u4ef6\u52a8\u6001\u5237\u65b0\u7684\u529f\u80fd\u3002<\/p>\n<h4>\u53c2\u8003\u6587\u6863<\/h4>\n<p>Nacos \u5b98\u65b9\u6587\u6863\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=YPIuubEdQP5McgYmrbPuzw%3D%3D.jDYqWslZCcWyqNGkssfBeo2k2YYTFZnVAtodW5v%2F1qA5Ns8e%2BVoQKL7iETewvrjA\" rel=\"nofollow\">https:\/\/nacos.io\/zh-cn\/docs\/w&#8230;<\/a><\/p>\n<blockquote>\n<p>\u662f\u975e\u5ba1\u4e4b\u4e8e\u5df1\uff0c\u6bc1\u8a89\u542c\u4e4b\u4e8e\u4eba\uff0c\u5f97\u5931\u5b89\u4e4b\u4e8e\u6570\u3002<\/p>\n<p>\u516c\u4f17\u53f7\uff1aJava\u4e2d\u6587\u793e\u7fa4<\/p>\n<p>Java\u9762\u8bd5\u5408\u96c6\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=Wm3qL4WyQDE5kGQWNmmnmw%3D%3D.JILhGpajNt4M8Z8VTtk%2FL75hUunlFbWVCOD2NhYj0YFF082aiJeBRNMsrYURsAyy\" rel=\"nofollow\">https:\/\/gitee.com\/mydb\/interview<\/a><\/p>\n<\/blockquote>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042405754\">https:\/\/segmentfault.com\/a\/1190000042405754<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spring Cloud Alibaba \u662f\u963f\u91cc\u5df4\u5df4\u63d0\u4f9b\u7684\u4e00\u7ad9\u5f0f\u5fae\u670d\u52a1\u5f00\u53d1\u89e3\u51b3\u65b9\u6848\uff0c\u76ee\u524d\u5df2\u88ab Spring C [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006348","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006348","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006348"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006348\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006348"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006348"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}