Nacos 致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,帮助您实现动态服务发现、服务配置管理、服务及流量管理。
Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。 Nacos 是构建以“服务”为中心的现代应用架构(例如微服务范式、云原生范式)的服务基础设施
一、首先需要先下载 Nacos 并启动 Nacos server,下载地址:Nacos下载
Nacos 依赖 Java 环境来运行。如果您是从代码开始构建并运行Nacos,还需要为此配置 Maven环境,请确保是在以下版本环境中安装使用:
二、启动方式
Linux/Unix/Mac
启动命令(standalone代表着单机模式运行,非集群模式):
sh startup.sh -m standalone
如果您使用的是ubuntu系统,或者运行脚本报错提示[[符号找不到,可尝试如下运行:
bash startup.sh -m standalone
Windows
启动命令:
cmd startup.cmd
或者双击startup.cmd运行文件。
附:端口默认8848,我们可以在conf文件夹下的application.properties文件上更改
最后输入 localhost:8848/nacos 打开控制台,我们就可以看到这样一个界面
三、我们先创建一个消费者项目,项目名为nacos-consumer
1、我们在配置文件下写上这个
server: port: 8851 #消费者者的端口 spring: application: name: nacos-consumer cloud: nacos: discovery: server-addr: 10.0.30.60:8848 feign: hystrix: enabled: true
2、我们引入这两个东西
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>0.9.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>2.2.0.RELEASE</version> </dependency>
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 { @Autowired TestService 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 { @Override public String hello(String name) { return "系统繁忙,请稍后再试!"; } }
至此,最简单的一个消费者基本上就好了,如果不是高需求,不需要进行额外的配置
四、把上面的消费者项目复制一份,易容变成生产者,改名为 nacos-producer1
1、配置文件如下
server: port: 8849 #提供者的端口 spring: application: name: nacos-producer cloud: nacos: discovery: server-addr: 10.0.30.60:8848
2、依赖不变,依旧是上面的两个依赖
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 这个消费者的接口,结果如下
至此,最基本最简单的配置完成,接下来就可以深入的进行其他骚操作了