初识 Nacos
Nacos 致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,帮助您实现动态服务发现、服务配置管理、服务及流量管理。
Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。 Nacos 是构建以“服务”为中心的现代应用架构(例如微服务范式、云原生范式)的服务基础设施
官方网址:https://nacos.io/zh-cn/
一、首先需要先下载 Nacos 并启动 Nacos server,下载地址:Nacos下载
Nacos 依赖 Java 环境来运行。如果您是从代码开始构建并运行Nacos,还需要为此配置 Maven环境,请确保是在以下版本环境中安装使用:
- 64 bit OS,支持 Linux/Unix/Mac/Windows,推荐选用 Linux/Unix/Mac。
- 64 bit JDK 1.8+;下载 & 配置。
- Maven 3.2.x+;下载 & 配置。
二、启动方式
Linux/Unix/Mac
启动命令(standalone代表着单机模式运行,非集群模式):
sh startup.sh -m standalone如果您使用的是ubuntu系统,或者运行脚本报错提示[[符号找不到,可尝试如下运行:
bash startup.sh -m standaloneWindows
启动命令:
cmd startup.cmd或者双击startup.cmd运行文件。
附:端口默认8848,我们可以在conf文件夹下的application.properties文件上更改
最后输入 localhost:8848/nacos 打开控制台,我们就可以看到这样一个界面
三、我们先创建一个消费者项目,项目名为nacos-consumer
1、我们在配置文件下写上这个
server:port: 8851 #消费者者的端口spring:application:name: nacos-consumercloud:nacos:discovery:server-addr: 10.0.30.60:8848feign:hystrix:enabled: true2、我们引入这两个东西
org.springframework.cloud spring-cloud-starter-alibaba-nacos-discovery 0.9.0.RELEASE org.springframework.cloud spring-cloud-starter-openfeign 2.2.0.RELEASE 3、我们在主方法上加上这两个注解 @EnableDiscoveryClient @EnableFeignClients
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients;/*** @author zp.wei*/ @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class NacosApplication {public static void main(String[] args) {SpringApplication.run(NacosApplication.class, args);}}4、创建一个简单粗暴的controller
package com.wzp.nacos;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;@RestController public class TestController {@AutowiredTestService testService;@GetMapping("/hello")public String test() {String result = testService.hello("wzp");return result;} }5、创建一个我们需要用到的service
package com.wzp.nacos;import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam;/*** @Author: zp.wei* @DATE: 2020/3/30 15:47*/ @FeignClient(name = "nacos-producer", fallback = TestFallback.class) public interface TestService {@GetMapping("/hello")String hello(@RequestParam(name = "name") String name);}6、我们再创建service需要用到的fallback,这个是为了在生产者无法提供服务的时候进行返回响应
package com.wzp.nacos;import org.springframework.stereotype.Component;/*** @author zp.wei*/ @Component public class TestFallback implements TestService {@Overridepublic String hello(String name) {return "系统繁忙,请稍后再试!";} }至此,最简单的一个消费者基本上就好了,如果不是高需求,不需要进行额外的配置
四、把上面的消费者项目复制一份,易容变成生产者,改名为 nacos-producer1
1、配置文件如下
server:port: 8849 #提供者的端口spring:application:name: nacos-producercloud:nacos:discovery:server-addr: 10.0.30.60:88482、依赖不变,依旧是上面的两个依赖
3、主方法上面留下该注解 @EnableDiscoveryClient,这个 @EnableFeignClients 注解不要了
4、创建一个controller
import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;/*** 创建服务** @author zp.wei*/ @RestController public class NacosProducerController {@Value("${server.port}")private Integer port;/*** 服务接口** @param name* @return*/@RequestMapping("/hello")public String sayHello(@RequestParam("name") String name) {System.out.println("name: " + name + "port: " + port);return "hello: " + name + " port:" + port;} }5、把生产者复制一份生成一个同胞的弟弟,顺便改个名字叫 nacos-producer2,然后别忘了改配置文件下的端口号
最后我们启动三个项目来看一下效果......
如图,我们可以发现已经被注册到注册与发现中心了,接下来我们测试一波,请求一下http://localhost:8851/hello 这个消费者的接口,结果如下
至此,最基本最简单的配置完成,接下来就可以深入的进行其他骚操作了
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!





