赞
踩
Nacos【Dynamic Naming and Configuration Service】提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。
下载地址:Releases · alibaba/nacos · GitHub
在window环境学习我下载了zip包
复制cluster.conf.example 去掉后缀【.example】生成配置文件
启动成功
在浏览器上访问
账号密码默认为:nacos
Nacos环境搭建完成
- <spring-boot.version>2.0.4.RELEASE</spring-boot.version>
- <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
- <nacos.version>0.2.2.RELEASE</nacos.version>
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-dependencies</artifactId>
- <version>${spring-cloud.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-dependencies</artifactId>
- <version>${spring-boot.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-alibaba-dependencies</artifactId>
- <version>${nacos.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter</artifactId>
- </dependency>
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
- <version>2.2.5.RELEASE</version>
- </dependency>
为了springboot版本和springcloud版本兼容,这里我修改了
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.3.9.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
@EnableDiscoveryClient
- server.port=8087
- spring.application.name=provider
- spring.cloud.nacos.discovery.server-addr=192.168.110.179:8848
- @RestController
- public class ProviderController {
-
- @GetMapping("/helloProvider")
- public String helloProvider(){
- return "你好,我是提供服务者";
- }
- }
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
- <version>2.2.5.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>com.netflix.archaius</groupId>
- <artifactId>archaius-core</artifactId>
- <version>0.7.6</version>
- <exclusions>
- <exclusion>
- <groupId>com.google.guava</groupId>
- <artifactId>guava</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- <!-- 服务调用 -->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-openfeign</artifactId>
- <version>2.1.1.RELEASE</version>
- </dependency>
为了springboot版本和springcloud版本兼容,这里我修改了
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.3.9.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- @EnableDiscoveryClient
- @EnableFeignClients
- server.port=8099
- spring.application.name=consumer
- spring.cloud.nacos.discovery.server-addr=192.168.110.179:8848
- @FeignClient(name = "provider", fallback = ConsumerServiceImpl.class)
- @Service
- public interface ConsumerService {
-
- @GetMapping("/helloProvider")
- String getHello();
- }
- @Component
- public class ConsumerServiceImpl implements ConsumerService {
- @Override
- public String getHello() {
- return "服务出错";
- }
- }
- @RestController
- public class ConsumerController {
- @Resource
- private ConsumerService consumerService;
-
- @GetMapping("/getHello")
- public String getHello(){
- return consumerService.getHello();
- }
- }
把创建的提供者服务与消费者服务运行起来,再回到nacos服务管理后台页面看
我们会看到有两个服务在nacos服务上注册成功了
我们创建一个网页访问消费者服务的接口
我们会发现成功调用了提供者服务
在这里我把消费者实现配置中心功能
在nacos服务后台中的配置列表中创建一个配置文件相当于application.properties或者yaml等配置
这里我使用的是application.properties并配置了
- <!-- **************************************************** -->
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
- <version>2.2.1.RELEASE</version>
- </dependency>
-
- <dependency>
- <groupId>com.alibaba.nacos</groupId>
- <artifactId>nacos-client</artifactId>
- <version>1.2.0</version>
- </dependency>
-
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-lang3</artifactId>
- <version>3.7</version>
- </dependency>
- <!-- **************************************************** -->
@NacosPropertySource(dataId = "test-dev.properties", autoRefreshed = true)
- spring.cloud.nacos.config.server-addr=192.168.110.179:8848
- spring.profiles.active=dev
- spring.cloud.nacos.config.file-extension=properties
- spring.cloud.nacos.config.group=DEFAULT_GROUP
- @RestController
- @RefreshScope
- @Validated
- public class ConsumerController {
-
- @NacosValue(value = "${datasource.url:123}", autoRefreshed = true)
- private String url;
-
- @NacosValue(value = "${datasource.username:123}", autoRefreshed = true)
- private String username;
-
- @NacosValue(value = "${datasource.password:123}", autoRefreshed = true)
- private String password;
-
- @Autowired
- private RestTemplate restTemplate;
- @Resource
- private ConsumerService consumerService;
-
- @GetMapping("/getHello")
- public String getHello(){
- System.out.println(url);
- System.out.println(username);
- System.out.println(password);
- return consumerService.getHello();
-
- }
- }
服务输出为
服务输出为
项目源码
链接:https://pan.baidu.com/s/12yC3cjK12_izzFO9imcPjQ
提取码:k5sj
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。