伴随着spring cloud alibaba 登上主板之后,我就去了解下感受仍是蛮不错的。说实话第一次看见Nacos好长一段时间连读法都不知道...(/nɑ:kəʊs/)。按照官方的话说Nacos是:一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。嗯...就是注册中心 + 配置中心的组合。
听说 Nacos 在阿里巴巴内部有超过 10 万的实例运行,已通过了相似双十一等各类大型流量的考验。因此能够放心的用。html
官方中文文档:https://nacos.io/zh-cn/docs/what-is-nacos.htmljava
下载地址:https://github.com/alibaba/nacos/releasesgit
不少人觉得nacos和eureka同样,须要本身搭建一个项目,再一springboot的方式启动,然而并非。
直接从下载的地址中找到nacos-server-*.*.*.zip或nacos-server-*.*.*.zip.gzgithub
启动命令(standalone表明着单机模式运行,非集群模式):web
sh startup.sh -m standalone
spring
若是您使用的是ubuntu系统,或者运行脚本报错提示[[符号找不到,可尝试以下运行: bootstrap
bash startup.sh -m standalone
ubuntu
启动命令:springboot
cmd startup.cmd
bash
或者双击startup.cmd运行文件。
访问:127.0.0.1:8848/nacos/index.html 出现登陆界面,启动成功。(用户名和秘密都是nacos)
首先Nacos规定必须是bootstrap配置文件才能注入。我项目中用yml,示例以下:
server: port: 8060 spring: application: name: power-match #项目名 profiles: active: local cloud: nacos: config: server-addr: 127.0.0.1:8848 #注册中心地址 discovery: server-addr: 127.0.0.1:8848
其次启动类示例以下(我使用了feign):
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; @EnableFeignClients @EnableDiscoveryClient @SpringBootApplication public class PowerMatchApplication { public static void main(String[] args) { SpringApplication.run(PowerMatchApplication.class, args); } }
运行后去Nacos页面查看,效果以下:
表示注册成功。
在这我只举例官方推荐的方法,别的就再也不介绍了。仍是同一个nacos,登陆——找到命名空间——新建命名空间,输入内容后就会生成命名空间ID
以application-local.yml配置为例:
spring: cloud: nacos: config: namespace: e503611c-9c54-4669-baff-e12770b3e948 discovery: namespace: e503611c-9c54-4669-baff-e12770b3e948 ribbon: ReadTimeout: 60000 ConnectTimeout: 60000
启动后,去看服务列表的test下面就有注册的服务了,只会服务调用就会只在local中调用。
他支持多种服务消费方式WebClient、Feign、RestTemplate。
@EnableDiscoveryClient @SpringBootApplication public class TestApplication { public static void main(String[] args) { SpringApplication.run(TestApplication.class, args); } @Slf4j @RestController static class TestController { @Autowired private WebClient.Builder webClientBuilder; @GetMapping("/test") public Mono<String> test() { Mono<String> result = webClientBuilder.build() .get() .uri("http://alibaba-nacos-discovery-server/hello?name=didi") .retrieve() .bodyToMono(String.class); return result; } } @Bean @LoadBalanced public WebClient.Builder loadBalancedWebClientBuilder() { return WebClient.builder(); } }
上面介绍的RestTemplate和WebClient都是Spring本身封装的工具,下面介绍一个Netflix OSS中的成员,经过它能够更方便的定义和使用服务消费客户端。下面也举一个具体的例子,其实现内容与上面两种方式结果一致:
第一步:在pom.xml
中增长openfeign的依赖:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
第二步:定义Feign客户端和使用Feign客户端:
@EnableDiscoveryClient @SpringBootApplication @EnableFeignClients public class TestApplication { public static void main(String[] args) { SpringApplication.run(TestApplication.class, args); } @Slf4j @RestController static class TestController { @Autowired Client client; @GetMapping("/test") public String test() { String result = client.hello("word"); return "Return : " + result; } } @FeignClient("alibaba-nacos-discovery-server") interface Client { @GetMapping("/hello") String hello(@RequestParam String name); } }
@EnableDiscoveryClient @SpringBootApplication public class TestApplication { public static void main(String[] args) { SpringApplication.run(TestApplication.class, args); } @Slf4j @RestController static class TestController { @Autowired RestTemplate restTemplate; @GetMapping("/test") public String test() { String result = restTemplate.getForObject("http://alibaba-nacos-discovery-server/hello?name=word", String.class); return result; } } @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } }
总的来讲,还说和之前没的什么区别。