【SpringCloud】Spring Cloud Alibaba 之 Nacos註冊中心(二十七)

  Nacos基本概念,請參考:【SpringCloud】Spring Cloud Alibaba 及 Nacos介紹(二十六) html

  本章介紹,使用搭建以Nacos爲註冊中心的SpringCloud項目java

搭建Nacos服務

  一、下載nacos,地址https://github.com/alibaba/nacos/releasesmysql

    本例使用的是nacos 1.2.1版本,下載獲得nacos-server-1.2.1.tar.gz壓縮包git

  二、解壓文件nacos-server-1.2.1.tar.gz,進入目錄,查看目錄內容github

    

  三、使用命令啓動nacos服務web

    命令:sh ./bin/startup.sh -m standalonespring

  四、瀏覽器訪問地址:http://localhost:8848/nacossql

     

  五、輸入默認用戶名/密碼(nacos/nacos)apache

    

搭建服務提供者

  一、新建一個工程(test-springcloud),導入Spring Cloud Alibaba依賴瀏覽器

1 <!-- spring cloud alibaba -->
2 <dependency>
3     <groupId>com.alibaba.cloud</groupId>
4     <artifactId>spring-cloud-alibaba-dependencies</artifactId>
5     <version>${spring-cloud-alibaba.version}</version>
6     <type>pom</type>
7     <scope>import</scope>
8 </dependency>

  完整pom以下:

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <project xmlns="http://maven.apache.org/POM/4.0.0"
  3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5     <modelVersion>4.0.0</modelVersion>
  6 
  7     <groupId>com.test</groupId>
  8     <artifactId>test-springcloud</artifactId>
  9     <version>1.0-SNAPSHOT</version>
 10     <packaging>pom</packaging>
 11     <!-- 統一管理jar包版本 -->
 12     <properties>
 13         <spring-boot.version>2.2.5.RELEASE</spring-boot.version>
 14         <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
 15         <spring-cloud-alibaba.version>2.1.0.RELEASE</spring-cloud-alibaba.version>
 16         <mybatis-spring-boot.version>2.1.2</mybatis-spring-boot.version>
 17         <mysql.version>8.0.12</mysql.version>
 18         <druid.version>1.1.21</druid.version>
 19         <lombok.version>1.18.12</lombok.version>
 20         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 21         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
 22         <java.version>1.8</java.version>
 23     </properties>
 24 
 25 
 26     <!-- 依賴管理:子模塊繼承後,提供做用:鎖定版本 + 子module 不用寫 version -->
 27     <dependencyManagement>
 28         <dependencies>
 29             <!-- spring boot -->
 30             <dependency>
 31                 <groupId>org.springframework.boot</groupId>
 32                 <artifactId>spring-boot-dependencies</artifactId>
 33                 <version>${spring-boot.version}</version>
 34                 <type>pom</type>
 35                 <scope>import</scope>
 36             </dependency>
 37             <!-- spring cloud -->
 38             <dependency>
 39                 <groupId>org.springframework.cloud</groupId>
 40                 <artifactId>spring-cloud-dependencies</artifactId>
 41                 <version>${spring-cloud.version}</version>
 42                 <type>pom</type>
 43                 <scope>import</scope>
 44             </dependency>
 45             <!-- spring cloud alibaba -->
 46             <dependency>
 47                 <groupId>com.alibaba.cloud</groupId>
 48                 <artifactId>spring-cloud-alibaba-dependencies</artifactId>
 49                 <version>${spring-cloud-alibaba.version}</version>
 50                 <type>pom</type>
 51                 <scope>import</scope>
 52             </dependency>
 53 
 54             <!-- mybatis -->
 55             <dependency>
 56                 <groupId>org.mybatis.spring.boot</groupId>
 57                 <artifactId>mybatis-spring-boot-starter</artifactId>
 58                 <version>${mybatis-spring-boot.version}</version>
 59             </dependency>
 60 
 61             <!-- mysql -->
 62             <dependency>
 63                 <groupId>mysql</groupId>
 64                 <artifactId>mysql-connector-java</artifactId>
 65                 <version>${mysql.version}</version>
 66             </dependency>
 67 
 68             <!-- druid -->
 69             <dependency>
 70                 <groupId>com.alibaba</groupId>
 71                 <artifactId>druid-spring-boot-starter</artifactId>
 72                 <version>${druid.version}</version>
 73             </dependency>
 74 
 75             <!-- lombok -->
 76             <dependency>
 77                 <groupId>org.projectlombok</groupId>
 78                 <artifactId>lombok</artifactId>
 79                 <version>${lombok.version}</version>
 80             </dependency>
 81 
 82             <!-- test -->
 83             <dependency>
 84                 <groupId>org.springframework.boot</groupId>
 85                 <artifactId>spring-boot-starter-test</artifactId>
 86                 <version>${spring-boot.version}</version>
 87                 <scope>test</scope>
 88                 <exclusions>
 89                     <exclusion>
 90                         <groupId>org.junit.vintage</groupId>
 91                         <artifactId>junit-vintage-engine</artifactId>
 92                     </exclusion>
 93                 </exclusions>
 94             </dependency>
 95         </dependencies>
 96     </dependencyManagement>
 97 
 98     <!-- SpringBoot打包插件,能夠將代碼打包成一個可執行的jar包 -->
 99     <build>
100         <finalName>${project.artifactId}</finalName>
101         <plugins>
102             <plugin>
103                 <groupId>org.springframework.boot</groupId>
104                 <artifactId>spring-boot-maven-plugin</artifactId>
105                 <version>${spring-boot.version}</version>
106                 <configuration>
107                     <fork>true</fork>
108                     <addResources>true</addResources>
109                 </configuration>
110             </plugin>
111         </plugins>
112     </build>
113 </project>
pom.xml

  二、新建一個模塊服務提供者(springcloud-provider-alibaba-payment9001),引入Nacos依賴

1 <!-- alibaba nacos -->
2 <dependency>
3     <groupId>com.alibaba.cloud</groupId>
4     <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
5 </dependency>

  完整pom以下:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <parent>
 6         <artifactId>test-springcloud</artifactId>
 7         <groupId>com.test</groupId>
 8         <version>1.0-SNAPSHOT</version>
 9     </parent>
10     <modelVersion>4.0.0</modelVersion>
11 
12     <artifactId>springcloud-provider-alibaba-payment9001</artifactId>
13 
14     <dependencies>
15 
16         <!-- alibaba nacos -->
17         <dependency>
18             <groupId>com.alibaba.cloud</groupId>
19             <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
20         </dependency>
21 
22         <!-- spring boot -->
23         <dependency>
24             <groupId>org.springframework.boot</groupId>
25             <artifactId>spring-boot-starter-web</artifactId>
26         </dependency>
27         <dependency>
28             <groupId>org.springframework.boot</groupId>
29             <artifactId>spring-boot-starter-actuator</artifactId>
30         </dependency>
31         <dependency>
32             <groupId>org.springframework.boot</groupId>
33             <artifactId>spring-boot-devtools</artifactId>
34             <scope>runtime</scope>
35             <optional>true</optional>
36         </dependency>
37         <dependency>
38             <groupId>org.projectlombok</groupId>
39             <artifactId>lombok</artifactId>
40             <optional>true</optional>
41         </dependency>
42         <dependency>
43             <groupId>org.springframework.boot</groupId>
44             <artifactId>spring-boot-starter-test</artifactId>
45             <scope>test</scope>
46         </dependency>
47 
48     </dependencies>
49 
50 </project>
pom.xml

  三、新建一個配置文件appliction.yml

 1 # 端口
 2 server:
 3   port: 9001
 4 
 5 spring:
 6   application:
 7     name: nacos-payment-provider
 8   cloud:
 9     nacos:
10       discovery:
11         server-addr: localhost:8848

  四、新建啓動類

1 @EnableDiscoveryClient
2 @SpringBootApplication
3 public class PaymentMain9001 {
4     public static void main(String[] args) {
5         SpringApplication.run(PaymentMain9001.class, args);
6     }
7 }

  五、新建一個Controller,以下:

 1 @RestController
 2 public class PaymentController {
 3 
 4     @Value("${server.port}")
 5     private String serverPort;
 6 
 7     @GetMapping(value = "/payment/nacos/{id}")
 8     public String getPayment(@PathVariable("id") Integer id) {
 9         return "nacos registry, serverPort: " + serverPort + "\t id: " + id;
10     }
11 }

  六、測試

    1)啓動Nacos服務,啓動服務提供者模塊

    2)訪問地址:http://localhost:9001/payment/nacos/11,正常獲取內容

      

    3)訪問Nacos的Web界面,查看服務列表菜單

      

      能夠看到服務列表中,有nacos-payment-provider服務

搭建服務消費者

  一、在父工程(test-springcloud)中,新建服務消費者模塊(springcloud-consumer-alibaba-order7001)

  二、引入nacos依賴,同上

    完整pom以下:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <parent>
 6         <artifactId>test-springcloud</artifactId>
 7         <groupId>com.test</groupId>
 8         <version>1.0-SNAPSHOT</version>
 9     </parent>
10     <modelVersion>4.0.0</modelVersion>
11 
12     <artifactId>springcloud-consumer-alibaba-order7001</artifactId>
13 
14     <dependencies>
15 
16         <!-- alibaba nacos -->
17         <dependency>
18             <groupId>com.alibaba.cloud</groupId>
19             <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
20         </dependency>
21 
22         <!-- spring boot -->
23         <dependency>
24             <groupId>org.springframework.boot</groupId>
25             <artifactId>spring-boot-starter-web</artifactId>
26         </dependency>
27         <dependency>
28             <groupId>org.springframework.boot</groupId>
29             <artifactId>spring-boot-starter-actuator</artifactId>
30         </dependency>
31         <dependency>
32             <groupId>org.springframework.boot</groupId>
33             <artifactId>spring-boot-devtools</artifactId>
34             <scope>runtime</scope>
35             <optional>true</optional>
36         </dependency>
37         <dependency>
38             <groupId>org.projectlombok</groupId>
39             <artifactId>lombok</artifactId>
40             <optional>true</optional>
41         </dependency>
42         <dependency>
43             <groupId>org.springframework.boot</groupId>
44             <artifactId>spring-boot-starter-test</artifactId>
45             <scope>test</scope>
46         </dependency>
47 
48     </dependencies>
49 </project>
View Code

  三、編寫配置文件,以下:

 1 # 端口
 2 server:
 3   port: 7001
 4 
 5 spring:
 6   application:
 7     name: nacos-order-consumer
 8   cloud:
 9     nacos:
10       discovery:
11         server-addr: localhost:8848

  四、編寫啓動類,以下:

1 @EnableDiscoveryClient
2 @SpringBootApplication
3 public class OrderMain7001 {
4     public static void main(String[] args) {
5         SpringApplication.run(OrderMain7001.class, args);
6     }
7 }

  五、編輯配置類,代碼以下,注入RestTemplate

 1 @Configuration
 2 public class AppConfig {
 3 
 4     @Bean
 5     @LoadBalanced
 6     public RestTemplate restTemplate(){
 7         return new RestTemplate();
 8     }
 9 
10 }

   六、編寫一個Controller用於訪問

@RestController
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping(value = "/consumer/payment/nacos/{id}")
    public String paymentInfo(@PathVariable("id") Integer id) {
        return restTemplate.getForObject("http://nacos-payment-provider" + "/payment/nacos/" + id, String.class);
    }

}

  七、測試

    1)啓動Nacos服務,啓動服務提供者模塊,啓動服務消費者模塊

    2)訪問地址:http://localhost:9001/payment/nacos/11,正常獲取內容

       訪問地址:http://localhost:7001/consumer/payment/nacos/11,正常獲取內容

      

    3)訪問Nacos的Web界面,查看服務列表菜單,分別能夠看到一個nacos-payment-provider服務,一個nacos-order-consumer服務

    4)在搭建一個(springcloud-provider-alibaba-payment9002)服務提供者模塊,與9001模塊內容相同,只有端口的區別

    5)啓動9002模塊,使用地址:http://localhost:7001/consumer/payment/nacos/11,訪問獲取內容

      獲取的內容,是輪流從9001與9002這兩個節點上獲取

    6)訪問Nacos的Web界面,查看服務列表菜單

      

相關文章
相關標籤/搜索