日本熟妇hd丰满老熟妇,中文字幕一区二区三区在线不卡 ,亚洲成片在线观看,免费女同在线一区二区

在Spring Cloud應用中接入MSE Nacos作為服務注冊中心

通過配置Spring Cloud應用指向MSE Nacos的服務地址,應用啟動后能夠自動向MSE Nacos注冊中心注冊服務實例信息,并實現服務發現、配置管理等功能。本文介紹如何在Spring Cloud應用中接入MSE Nacos作為服務注冊中心,實現服務調用。

前提條件

重要

如果通過公網的方式訪問MSE Nacos,請確保服務提供者和服務消費者都已具備訪問公網的能力,并且需要您對應用訪問的MSE Nacos進行白名單配置。具體操作,請參見設置白名單

創建服務提供者

在本地創建服務提供者應用工程,然后添加依賴,并開啟服務注冊與發現功能,將注冊中心指定為創建的MSE Nacos。

  1. 創建Maven工程,命名為nacos-service-provider

  2. pom.xml中添加如下依賴。

    以Spring Boot 2.1.4.RELEASE和Spring Cloud Greenwich.SR1為例,依賴如下:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/>
    </parent>
    
    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>                  

    示例中使用的版本為Spring Cloud Greenwich,對應Spring Cloud Alibaba版本為2.1.1.RELEASE。

    • 如果使用Spring Cloud Finchley版本,對應Spring Cloud Alibaba版本為2.0.1.RELEASE。

    • 如果使用Spring Cloud Edgware版本,對應Spring Cloud Alibaba版本為1.5.1.RELEASE。

    重要

    Spring Cloud Edgware版本的生命周期已結束,不推薦使用這個版本開發應用。

  3. 在src\main\java工程路徑下創建名為com.aliware.edas的Package

  4. 在com.aliware.edas中創建服務提供者的啟動類ProviderApplication,并添加如下代碼。

    package com.aliware.edas;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    
    @SpringBootApplication
        @EnableDiscoveryClient
        public class ProviderApplication {
    
            public static void main(String[] args) {
                SpringApplication.run(ProviderApplication.class, args);
            }
        }             
    說明

    其中@EnableDiscoveryClient注解表明此應用需開啟服務注冊與發現功能。

  5. 在com.aliware.edas中創建EchoController。指定URL mapping為/echo/{String},指定HTTP方法為GET,方法參數從URL路徑中獲得,回顯收到的參數。

    package com.aliware.edas;
    
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class EchoController {
       @RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
        public String echo(@PathVariable String string) {
           return string;
          }
    }              
  6. 在src\main\resources工程路徑下創建文件application.properties,在application.properties中添加如下配置,并指定Nacos Server的訪問地址。

    spring.application.name=service-provider
    server.port=18081
    spring.cloud.nacos.discovery.server-addr=mse.XX.nacos.mse.aliyuncs.com:8848               

    在實例列表頁面,可以查看MSE Nacos的外網訪問地址,格式為mse.XX.nacos.mse.aliyuncs.com。

    重要

    如果您使用的服務注冊中心為MSE的Zookeeper或者Eureka,那么您需要將本步驟的注冊中心代碼換成Zookeeper或者Eureka相應的代碼,具體詳情請參見MSE集群托管使用說明

  7. 查看應用是否注冊成功。

    1. 執行nacos-service-providerProviderApplicationmain函數,啟動應用。

    2. 登錄MSE注冊配置中心管理控制臺,并在頂部菜單欄選擇地域。

    3. 在左側導航欄,選擇注冊配置中心 > 實例列表。單擊目標實例名稱。

    4. 在左側導航欄,選擇服務管理 > 服務列表

    5. 在服務列表頁面,查看應用是否注冊成功。

創建服務消費者

本步驟除介紹服務注冊的功能,還將介紹Nacos服務發現與RestTemplate和FeignClient兩個客戶端如何配合使用。

  1. 創建Maven工程,命名為nacos-service-consumer

  2. pom.xml中添加如下依賴。

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/>
    </parent>
    
    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>        
  3. 在src\main\java工程路徑下創建名為com.aliware.edas的Package

  4. 在com.aliware.edas中配置RestTemplate和FeignClient。

    1. 在com.aliware.edas中創建一個接口類EchoService,添加@FeignClient注解,并配置對應的HTTP URL地址及HTTP方法。

      package com.aliware.edas;
      
      import org.springframework.cloud.openfeign.FeignClient;
      import org.springframework.web.bind.annotation.PathVariable;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RequestMethod;
      
      @FeignClient(name = "service-provider")
      public interface EchoService {
          @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
          String echo(@PathVariable("str") String str);
      }                   
    2. 在com.aliware.edas中創建啟動類ConsumerApplication并添加相關配置。

      • 使用@EnableDiscoveryClient注解啟用服務注冊與發現。

      • 使用@EnableFeignClients注解激活FeignClient。

      • 使用@LoadBalanced注解將RestTemplate與服務發現集成。

      package com.aliware.edas;
      
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
      import org.springframework.cloud.client.loadbalancer.LoadBalanced;
      import org.springframework.cloud.openfeign.EnableFeignClients;
      import org.springframework.context.annotation.Bean;
      import org.springframework.web.client.RestTemplate;
      
      @SpringBootApplication
      @EnableDiscoveryClient
      @EnableFeignClients
      public class ConsumerApplication {
      
          @LoadBalanced
          @Bean
          public RestTemplate restTemplate() {
              return new RestTemplate();
          }
      
          public static void main(String[] args) {
              SpringApplication.run(ConsumerApplication.class, args);
          }
      }
  5. 在com.aliware.edas中創建類TestController,以演示和驗證服務發現功能。

    package com.aliware.edas;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    
    @RestController
    public class TestController {
    
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private EchoService echoService;
    
    @RequestMapping(value = "/echo-rest/{str}", method = RequestMethod.GET)
    public String rest(@PathVariable String str) {
          return restTemplate.getForObject("http://service-provider/echo/" + str,
                        String.class);
            }
    
          @RequestMapping(value = "/echo-feign/{str}", method = RequestMethod.GET)
          public String feign(@PathVariable String str) {
                return echoService.echo(str);
            }
    
        }           
  6. 在src\main\resources工程路徑下創建文件application.properties,在application.properties中添加如下配置,指定Nacos Server的地址。

    spring.application.name=service-consumer
    server.port=18082
    spring.cloud.nacos.discovery.server-addr=mse.XX.nacos.mse.aliyuncs.com:8848

    實例列表頁面,可以查看MSE Nacos的外網訪問地址,格式為mse.XX.nacos.mse.aliyuncs.com

    重要

    如果您使用的服務注冊中心為MSE的Zookeeper或者Eureka,那么您需要將本步驟的注冊中心代碼換成Zookeeper或者Eureka相應的代碼,具體詳情請參見MSE使用說明

  7. 查看應用是否注冊成功。

    1. 執行nacos-service-consumerConsumerApplicationmain函數,啟動應用。

    2. 登錄MSE注冊配置中心管理控制臺,并在頂部菜單欄選擇地域。

    3. 在左側導航欄,選擇注冊配置中心 > 實例列表。單擊目標實例名稱。

    4. 在左側導航欄,選擇服務管理 > 服務列表

    5. 在服務列表頁面,查看應用是否注冊成功。

本地測試

在本地測試消費者對提供者的服務調用結果。

  • Linux/Unix/Mac系統:使用如下命令進行服務調用。

    curl http://127.0.0.1:18082/echo-rest/rest-rest
    curl http://127.0.0.1:18082/echo-feign/feign-rest
  • Windows系統:在瀏覽器中輸入http://127.0.0.1:18082/echo-rest/rest-rest和http://127.0.0.1:18082/echo-feign/feign-rest。

    如圖所示表明服務調用成功。

    Spring Cloud微服務應用是使用MSE調用成功

常見問題

本地開發的Spring Cloud微服務應用,其服務注冊中心為MSE上創建的Nacos,應用運行后,在MSE服務管理頁面看不到服務信息,如何處理?

您需要對您的應用訪問進行白名單配置。具體操作,請參見設置白名單

說明

MSE默認設置為127.0.0.1/32,表示禁止所有地址的訪問。

MSE支持哪些Spring Cloud版本?

  • Spring Cloud Greenwich對應的Spring Cloud Alibaba版本為2.1.1.RELEASE。

  • Spring Cloud Finchley對應的Spring Cloud Alibaba版本為 2.0.1.RELEASE。

  • Spring Cloud Edgware對應的 Spring Cloud Alibaba版本為1.5.1.RELEASE。

重要

Spring Cloud Edgware版本的生命周期已結束,不推薦使用此版本開發應用。