<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] # **1. 創建聚合工程 springcloud-parent-test** > 創建父工程用于版本管理 ~~~ <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.tuna</groupId> <artifactId>springcloud-parent-test</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>provider</module> </modules> <properties> <spring-boot.version>2.3.4.RELEASE</spring-boot.version> <spring-cloud.version>Hoxton.SR8</spring-cloud.version> <spring-cloud-alibaba.version>2.2.3.RELEASE</spring-cloud-alibaba.version> </properties> <!-- 依賴聲明 --> <dependencyManagement> <dependencies> <!-- SpringCloud 微服務 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- SpringCloud Alibaba 微服務 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${spring-cloud-alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- SpringBoot 依賴配置 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project> ~~~ # **2. 服務提供者整合服務發現** * springboot也可以直接結合nacos ## **2.1 創建工程provider** ![](https://img.kancloud.cn/d7/d2/d7d2d2aa049c6c93740dea6d6d1a0ce3_379x254.png) ## **2.2 pom** > 1. 注意`spring-cloud-context`依賴,引入bootstrap.xml才會生效 > 2. 引入服務發現依賴 ~~~ <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>springcloud-parent-test</artifactId> <groupId>com.tuna</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>provider</artifactId> <dependencies> <!--springboot web啟動器--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--需要引入該jar才能使bootstrap配置文件生效--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-context</artifactId> </dependency> <!--服務發現--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> </dependencies> </project> ~~~ ## 2.3 啟動類 ~~~ @SpringBootApplication public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); } } ~~~ ## 2.4 controller ~~~ @RestController @RequestMapping("/test") public class TestController { @GetMapping("") public Object say(){ return "hello cloud - provider"; } } ~~~ ## 2.5 bootstrap.yml ~~~ ## Spring spring: application: # 應用名稱 name: provider profiles: # 環境配置 active: dev ~~~ ## 2.6 application-dev.yml ~~~ server: port: 8000 spring: application: name: provider cloud: nacos: discovery: # 服務注冊地址 server-addr: 192.168.56.10:8848 ~~~ * 測試 ![](https://img.kancloud.cn/af/cc/afcc1c70cd2c40d30ec5a891009b7f9a_554x149.png) * provider注冊到nacos ![](https://img.kancloud.cn/54/78/547845d188d56f845c9e5dfdb7f9aae1_923x277.png) # 3. 消費者整合服務發現 ## 3.1 整合Feign cloud-api模塊,統一fein接口調用,ribbon輪詢和hettrix ![](https://img.kancloud.cn/64/2f/642f48839571c19e8052e1316f71521e_368x186.png) **1. pom** ~~~ <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>springcloud-parent-test</artifactId> <groupId>com.tuna</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-api</artifactId> <dependencies> <!--feign 依賴--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies> </project> ~~~ **2. 定義接口** ~~~ @FeignClient(contextId = "provider-test",value = "provider") //指定調用的微服務名稱 public interface ProviderClientService { @RequestMapping(value = "/test", method = RequestMethod.GET) String say(); } ~~~ ## 3.2 Consumer模塊 ![](https://img.kancloud.cn/8f/4c/8f4ca8051922c49c18b9af7203fbb552_392x237.png) **1. pom需要引入api** ~~~ <dependency> <groupId>com.tuna</groupId> <artifactId>cloud-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> ~~~ **2. 配置文件和provider一樣** **3. controller** 引入foreign ``` @Autowired ProviderClientService providerClientService; ``` ~~~ @RestController @RequestMapping("/consumer") public class ConsumerController { @Autowired ProviderClientService providerClientService; @GetMapping("") public Object say(){ return "consumer::" + providerClientService.say(); } } ~~~ **4. 啟動類開啟服務發現和foreign** ~~~ @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients(basePackages = "com.tuna.cloud.api") public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } } ~~~ 測試:Consumer成功調用provider ![](https://img.kancloud.cn/d7/92/d7929b23fdb7acd99c7582649f9440a2_535x129.png) # 動態修改名稱空間 ![](https://img.kancloud.cn/21/ca/21ca88c74aa72e6a67c15e213033edc1_1185x318.png) 添加到dev名稱空間,修改配置保存 ![](https://img.kancloud.cn/1f/98/1f98c6eea792f739b50dbda251ad6d31_895x417.png) 自動更新 ![](https://img.kancloud.cn/b9/55/b955641a2d8252946a62ed245e04c0b7_867x319.png)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看