springcloud(5) nacos服务注册和发现
一。Nacos下载
Nacos依赖于Java环境,所以必须安装Java环境。然后从官网下载Nacos的解压包,安装稳定版的,下载地址:https://github.com/alibaba/nacos/releases
下载完成后,解压,在解压后的文件的/bin目录下,windows系统点击startup.cmd就可以启动nacos。nacos的端口是8848
启动成功,在浏览器上访问:http://localhost:8848/nacos,会跳转到登陆界面,默认的登陆用户名为nacos,密码也为nacos。
登陆成功后,展示的界面如下:
从界面可知,此时没有服务注册到Nacos上。
二。Nacos服务注册和发现
1. nacos-provider 服务提供者
pom.xml添加依赖,Spring boot版本为2.1.4.RELEASE,Spring Cloud 版本为Greenwich.RELEASE
<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.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
application.yml 中配置 Nacos server 的地址
server:
port: 8762
spring:
application:
name: nacos-provider
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
通过 Spring Cloud 原生注解 @EnableDiscoveryClient
开启服务注册发现功能:
@SpringBootApplication @EnableDiscoveryClient public class NacosProviderApplication { public static void main(String[] args) { SpringApplication.run(NacosProviderApplication.class, args); } @RestController class EchoController { @RequestMapping(value = "/echo/{string}", method = RequestMethod.GET) public String echo(@PathVariable String string) { return "Hello Nacos Discovery " + string; } } }
2.nacos-consumer 服务消费端
pom.xml和nacos-provider一样
<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.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 使用Feign--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
application.yml
server:
port: 8763
spring:
application:
name: nacos-consumer
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
通过 Spring Cloud 原生注解 @EnableDiscoveryClient
开启服务注册发现功能。调用服务两种方式:
(1)给 RestTemplate 实例添加 @LoadBalanced
注解,开启 @LoadBalanced
与 Ribbon 的集成
(2)使用@EnableFeignClients //启用feign进行远程调用
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients //启用feign进行远程调用 public class NacosConsumerApplication { public static void main(String[] args) { SpringApplication.run(NacosConsumerApplication.class, args); } @LoadBalanced @Bean public RestTemplate restTemplate(){ return new RestTemplate(); } @RestController public class TestController{ @Autowired private RestTemplate restTemplate; @Autowired private ProviderClient providerClient; /** * RestTemplate方式调用接口 * @param str * @return */ @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET) public String echo(@PathVariable String str){ return restTemplate.getForObject("http://nacos-provider/echo/"+ str, String.class); } /** * Feign方式调用接口,providerClient为接口 * @param str * @return */ @RequestMapping(value = "/echo-feign/{str}", method = RequestMethod.GET) public String echoFeign(@PathVariable String str){ return providerClient.echo(str); } } }
写一个FeignClient,调用nacos-provider的服务
@FeignClient("nacos-provider") public interface ProviderClient { @RequestMapping(value = "/echo/{string}", method = RequestMethod.GET) public String echo(@PathVariable String string); }
启动服务后,nacos中注册了这两个服务
访问http:8763/echo-feign/2222 就能看到页面显示
参考:https://www.fangzhipeng.com/springcloud/2019/05/30/sc-nacos-discovery.html
https://nacos.io/zh-cn/docs/quick-start-spring-cloud.html