物联网架构成长之路(51)-Nacos微服务配置中心、服务注册服务发现

0.前言

  之前练习微服务是用Eureka,最近发现BladeX是用的Nacos,发现Nacos还比较简单实用。所以练习一下这个,为物联网V2版本积累经验。

1. 下载、安装、启动

  下载Nacos

1 https://github.com/alibaba/nacos/releases
2 https://nacos.io/zh-cn/docs/quick-start.html

  解压启动

  访问http://127.0.0.1:8848/nacos/index.html
  默认帐号密码 nacos/nacos

 

2. Nacos作为配置中心

  参考官方文档,实现nacos-config

https://github.com/alibaba/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-docs/src/main/asciidoc/nacos-config.adoc

  注意,springcloud使用配置中心,那么配置文件是对应的 bootstrap.properties

bootstrap.properties

1 spring.cloud.nacos.config.server-addr=127.0.0.1:8848
2 spring.cloud.nacos.config.prefix=example
3 spring.profiles.active=demo
4 spring.cloud.nacos.config.file-extension=properties
5 
6 # ${prefix}-${spring.profile.active}.${file-extension}
7 # example-demo.properties

pom.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <parent>
 6         <groupId>org.springframework.boot</groupId>
 7         <artifactId>spring-boot-starter-parent</artifactId>
 8         <version>2.2.2.RELEASE</version>
 9         <relativePath/> <!-- lookup parent from repository -->
10     </parent>
11     <groupId>com.wunaozai.demo</groupId>
12     <artifactId>NacosConfig</artifactId>
13     <version>0.0.1-SNAPSHOT</version>
14     <name>NacosConfig</name>
15     <description>Nacos配置中心</description>
16 
17     <properties>
18         <java.version>1.8</java.version>
19         <spring-cloud-alibaba.version>2.2.0.RELEASE</spring-cloud-alibaba.version>
20         <maven-jar-plugin.version>3.0.0</maven-jar-plugin.version>
21     </properties>
22 
23     <dependencies>
24         <dependency>
25             <groupId>org.springframework.boot</groupId>
26             <artifactId>spring-boot-starter-web</artifactId>
27         </dependency>
28         <dependency>
29             <groupId>com.alibaba.cloud</groupId>
30             <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
31         </dependency>
32         <dependency>
33             <groupId>com.google.guava</groupId>
34             <artifactId>guava</artifactId>
35             <version>15.0</version>
36             <scope>compile</scope>
37         </dependency>
38 
39         <dependency>
40             <groupId>org.springframework.boot</groupId>
41             <artifactId>spring-boot-devtools</artifactId>
42             <scope>runtime</scope>
43             <optional>true</optional>
44         </dependency>
45         <dependency>
46             <groupId>org.springframework.boot</groupId>
47             <artifactId>spring-boot-starter-test</artifactId>
48             <scope>test</scope>
49             <exclusions>
50                 <exclusion>
51                     <groupId>org.junit.vintage</groupId>
52                     <artifactId>junit-vintage-engine</artifactId>
53                 </exclusion>
54             </exclusions>
55         </dependency>
56     </dependencies>
57 
58     <dependencyManagement>
59         <dependencies>
60             <dependency>
61                 <groupId>com.alibaba.cloud</groupId>
62                 <artifactId>spring-cloud-alibaba-dependencies</artifactId>
63                 <version>${spring-cloud-alibaba.version}</version>
64                 <type>pom</type>
65                 <scope>import</scope>
66             </dependency>
67         </dependencies>
68     </dependencyManagement>
69 
70     <repositories>
71         <repository>
72             <id>maven-ali</id>
73             <url>http://maven.aliyun.com/nexus/content/groups/public//</url>
74             <releases>
75                 <enabled>true</enabled>
76             </releases>
77             <snapshots>
78                 <enabled>true</enabled>
79                 <updatePolicy>always</updatePolicy>
80                 <checksumPolicy>fail</checksumPolicy>
81             </snapshots>
82         </repository>
83     </repositories>
84     
85     <build>
86         <plugins>
87             <plugin>
88                 <groupId>org.springframework.boot</groupId>
89                 <artifactId>spring-boot-maven-plugin</artifactId>
90             </plugin>
91         </plugins>
92     </build>
93 
94 </project>

NacosConfigApplication.java

 1 package com.wunaozai.demo;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.context.ConfigurableApplicationContext;
 6 
 7 @SpringBootApplication
 8 public class NacosConfigApplication {
 9 
10     public static void main(String[] args) {
11         ConfigurableApplicationContext context = 
12                 SpringApplication.run(NacosConfigApplication.class, args);
13         String username = context.getEnvironment().getProperty("user.name");
14         String userage  = context.getEnvironment().getProperty("user.age");
15         System.out.println("user.name=" + username + "  " + "user.age=" + userage);
16     }
17 
18 }

NacosController.java

 1 package com.wunaozai.demo.controller;
 2 
 3 import org.springframework.beans.factory.annotation.Value;
 4 import org.springframework.cloud.context.config.annotation.RefreshScope;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 import org.springframework.web.bind.annotation.RestController;
 7 
 8 @RefreshScope
 9 @RestController
10 @RequestMapping(value="/nacos")
11 public class NacosController {
12 
13     @Value("${str:---}")
14     private String str;
15     
16     @RequestMapping(value="/get")
17     public String get() {
18         return str;
19     }
20 }

然后在nacos管理界面上配置如下信息

1 user.name=demo
2 user.age=99
3 server.port=9988
4 str=wunaozai

配置后,启动NacosConfig服务,并访问浏览器,效果如下。

 修改nacos上的配置文件后,保存。程序会自动更新配置。

 

3. Nacos作为服务中心,服务注册与服务发现

3.1 服务提供者Provider

pom.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <parent>
 6         <groupId>org.springframework.boot</groupId>
 7         <artifactId>spring-boot-starter-parent</artifactId>
 8         <version>2.2.5.RELEASE</version>
 9         <relativePath/> <!-- lookup parent from repository -->
10     </parent>
11     <groupId>com.wunaozai.demo</groupId>
12     <artifactId>NacosDiscoveryProvider</artifactId>
13     <version>0.0.1-SNAPSHOT</version>
14     <name>NacosDiscoveryProvider</name>
15     <description>Nacos服务发现与注册</description>
16 
17     <properties>
18         <java.version>1.8</java.version>
19         <spring-cloud-alibaba.version>2.2.0.RELEASE</spring-cloud-alibaba.version>
20         <maven-jar-plugin.version>3.0.0</maven-jar-plugin.version>
21     </properties>
22 
23     <dependencies>
24         <dependency>
25             <groupId>org.springframework.boot</groupId>
26             <artifactId>spring-boot-starter-web</artifactId>
27         </dependency>
28         <dependency>
29             <groupId>com.alibaba.cloud</groupId>
30             <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
31         </dependency>
32         <dependency> 
33             <groupId>org.springframework.boot</groupId> 
34             <artifactId>spring-boot-starter-actuator</artifactId> 
35         </dependency>
36         <dependency>
37             <groupId>com.google.guava</groupId>
38             <artifactId>guava</artifactId>
39             <version>15.0</version>
40             <scope>compile</scope>
41         </dependency>
42 
43         <dependency>
44             <groupId>org.springframework.boot</groupId>
45             <artifactId>spring-boot-devtools</artifactId>
46             <scope>runtime</scope>
47             <optional>true</optional>
48         </dependency>
49         <dependency>
50             <groupId>org.springframework.boot</groupId>
51             <artifactId>spring-boot-starter-test</artifactId>
52             <scope>test</scope>
53             <exclusions>
54                 <exclusion>
55                     <groupId>org.junit.vintage</groupId>
56                     <artifactId>junit-vintage-engine</artifactId>
57                 </exclusion>
58             </exclusions>
59         </dependency>
60     </dependencies>
61 
62     <dependencyManagement>
63         <dependencies>
64             <dependency>
65                 <groupId>com.alibaba.cloud</groupId>
66                 <artifactId>spring-cloud-alibaba-dependencies</artifactId>
67                 <version>${spring-cloud-alibaba.version}</version>
68                 <type>pom</type>
69                 <scope>import</scope>
70             </dependency>
71         </dependencies>
72     </dependencyManagement>
73 
74     <build>
75         <plugins>
76             <plugin>
77                 <groupId>org.springframework.boot</groupId>
78                 <artifactId>spring-boot-maven-plugin</artifactId>
79             </plugin>
80         </plugins>
81     </build>
82 
83 </project>

application.properites

1 server.port=8081
2 spring.application.name=nacos-provider
3 spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

NacosProviderController.java

 1 package com.wunaozai.demo;
 2 
 3 import org.springframework.web.bind.annotation.RequestMapping;
 4 import org.springframework.web.bind.annotation.RestController;
 5 
 6 @RestController
 7 @RequestMapping(value="/provider")
 8 public class NacosProviderController {
 9 
10     @RequestMapping(value="/echo")
11     public String echo(String str) {
12         return "Hello Nacos Discovery: " + str;
13     }
14 }

NacosDiscoveryProviderApplication.java

 1 package com.wunaozai.demo;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
 6 
 7 @SpringBootApplication
 8 @EnableDiscoveryClient
 9 public class NacosDiscoveryProviderApplication {
10 
11     public static void main(String[] args) {
12         SpringApplication.run(NacosDiscoveryProviderApplication.class, args);
13     }
14 
15 }

 

3.2 服务消费者Consumer

pom.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <parent>
 6         <groupId>org.springframework.boot</groupId>
 7         <artifactId>spring-boot-starter-parent</artifactId>
 8         <version>2.2.5.RELEASE</version>
 9         <relativePath/> <!-- lookup parent from repository -->
10     </parent>
11     <groupId>com.wunaozai.demo</groupId>
12     <artifactId>NacosDiscoveryConsumer</artifactId>
13     <version>0.0.1-SNAPSHOT</version>
14     <name>NacosDiscoveryConsumer</name>
15     <description>Nacos服务注册与发现</description>
16 
17     <properties>
18         <java.version>1.8</java.version>
19         <spring-cloud-alibaba.version>2.2.0.RELEASE</spring-cloud-alibaba.version>
20         <maven-jar-plugin.version>3.0.0</maven-jar-plugin.version>
21     </properties>
22 
23     <dependencies>
24         <dependency>
25             <groupId>org.springframework.boot</groupId>
26             <artifactId>spring-boot-starter-web</artifactId>
27         </dependency>
28         <dependency>
29             <groupId>com.alibaba.cloud</groupId>
30             <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
31         </dependency>
32         <dependency>
33             <groupId>com.google.guava</groupId>
34             <artifactId>guava</artifactId>
35             <version>15.0</version>
36             <scope>compile</scope>
37         </dependency>
38 
39         <dependency>
40             <groupId>org.springframework.boot</groupId>
41             <artifactId>spring-boot-devtools</artifactId>
42             <scope>runtime</scope>
43             <optional>true</optional>
44         </dependency>
45         <dependency>
46             <groupId>org.springframework.boot</groupId>
47             <artifactId>spring-boot-starter-test</artifactId>
48             <scope>test</scope>
49             <exclusions>
50                 <exclusion>
51                     <groupId>org.junit.vintage</groupId>
52                     <artifactId>junit-vintage-engine</artifactId>
53                 </exclusion>
54             </exclusions>
55         </dependency>
56     </dependencies>
57 
58     <dependencyManagement>
59         <dependencies>
60             <dependency>
61                 <groupId>com.alibaba.cloud</groupId>
62                 <artifactId>spring-cloud-alibaba-dependencies</artifactId>
63                 <version>${spring-cloud-alibaba.version}</version>
64                 <type>pom</type>
65                 <scope>import</scope>
66             </dependency>
67         </dependencies>
68     </dependencyManagement>
69 
70     <build>
71         <plugins>
72             <plugin>
73                 <groupId>org.springframework.boot</groupId>
74                 <artifactId>spring-boot-maven-plugin</artifactId>
75             </plugin>
76         </plugins>
77     </build>
78 
79 </project>

application.properites

1 server.port=8083
2 spring.application.name=nacos-consumer
3 spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

BeanConfig.java

 1 package com.wunaozai.demo.config;
 2 
 3 import org.springframework.cloud.client.loadbalancer.LoadBalanced;
 4 import org.springframework.context.annotation.Bean;
 5 import org.springframework.stereotype.Component;
 6 import org.springframework.web.client.RestTemplate;
 7 
 8 @Component
 9 public class BeanConfig {
10 
11     @LoadBalanced
12     @Bean
13     public RestTemplate restTemplate() {
14         return new RestTemplate();
15     }
16 }

NacosConsumerController.java

 1 package com.wunaozai.demo;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.web.bind.annotation.GetMapping;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 import org.springframework.web.bind.annotation.RestController;
 7 import org.springframework.web.client.RestTemplate;
 8 
 9 @RestController
10 @RequestMapping(value="/consumer")
11 public class NacosConsumerController {
12 
13     @Autowired
14     private RestTemplate restTemplate;
15     
16     @GetMapping(value="/echo")
17     public String getProviderEcho(String msg) {
18         return restTemplate.getForObject("http://nacos-provider/provider/echo?str=" + msg, String.class);
19     }
20 }

NacosDiscoveryConsumerApplication.java

 1 package com.wunaozai.demo;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
 6 
 7 @SpringBootApplication
 8 @EnableDiscoveryClient
 9 public class NacosDiscoveryConsumerApplication {
10 
11     public static void main(String[] args) {
12         SpringApplication.run(NacosDiscoveryConsumerApplication.class, args);
13     }
14 
15 }

浏览器访问结果

Nacos管理界面

 

参考资料:

  https://github.com/alibaba/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-docs/src/main/asciidoc/nacos-config.adoc
  https://github.com/alibaba/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-docs/src/main/asciidoc/nacos-discovery.adoc
  https://www.jianshu.com/p/3e13272a9c84

附件代码下载: https://files.cnblogs.com/files/wunaozai/IoT-51-Nacos.zip

本文地址:https://www.cnblogs.com/wunaozai/p/12378485.html
本系列目录: https://www.cnblogs.com/wunaozai/p/8067577.html
个人主页:https://www.wunaozai.com/

posted @ 2020-02-29 11:15  无脑仔的小明  阅读(1245)  评论(1编辑  收藏  举报