启动 Producer
,并且手动调用接口,启动 Producer
过后,我们在 Nacos
的服务注册列表可以看如下所示的内容,在 test1
的命名空间下,已经有了我们创建的 Producer
服务。
通过手动调用 Producer
的接口 http://127.0.0.1:8080/producer/getUsername
显示如下内容
并且我们看下此时 Nacos
的配置中心里面配置文件com.example.properties
里面的内容正是这个,这个时候我们手动把配置里面password
参数的值改成JavaGeek666
,再次访问接口,我们会发现接口的输出也自动改变了。
修改配置内容如下:
再次访问结果如下:
服务调用者 Consumer
前面我们已经完成了Producer
的服务注册与配置动态生效的功能,这个时候基本已经可以使用了,不过我们还需要更进一步通过 Nacos
来实现服务发现,接下来我们创建 Consumer
的 SpringBoot
的项目,配置文件和pom.xml
文件基本一致,只要修改端口以及对应地方,下面贴一下不一样的地方
-
boostrap.properties
内容如下,因为这里我们只调用Producer
的接口,不需要接入Nacos
的配置中心,所以这里只配置发服务注册与发现
spring.application.name=consumer ######################服务注册发现配置################################## spring.cloud.nacos.discovery.cluster-name=TEST1_GROUP spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 spring.cloud.nacos.discovery.namespace=caeser-adsys-naming
- 启动类,配置上
feignClient
需要扫描的包路径
package com.ziyou.nacos.demo.consumer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cloud.openfeign.EnableFeignClients; /** * <br> * <b>Function:</b><br> * <b>Author:</b>@author ziyou<br> * <b>Date:</b>2021-04-11 17:07<br> * <b>Desc:</b>无<br> */ @SpringBootApplication(scanBasePackages = "com.ziyou.nacos") @EnableFeignClients(basePackages = {"com.ziyou.nacos.demo.consumer.rpc"}) @EnableCaching public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } }
- 编写调用
Producer
的接口,FeignClient
里面的value
就是Producer
的应用名称
package com.ziyou.nacos.demo.consumer.rpc; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.GetMapping; /** * <br> * <b>Function:</b><br> * <b>Author:</b>@author ziyou<br> * <b>Date:</b>2021-04-11 20:01<br> * <b>Desc:</b>无<br> */ @FeignClient(value = "producer") @Component public interface IProducerFeign { /** * 获取生产者名称接口 * * @return */ @GetMapping("/producer/getUsername") String getUsername(); }
package com.ziyou.nacos.demo.consumer.controller; import com.ziyou.nacos.demo.consumer.rpc.IProducerFeign; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * <br> * <b>Function:</b><br> * <b>Author:</b>@author ziyou<br> * <b>Date:</b>2021-04-11 19:59<br> * <b>Desc:</b>无<br> */ @RestController @RequestMapping(value = "consumer") public class TestNacosController { private IProducerFeign iProducerFeign; @GetMapping("/testNacos") private String testNacos() { return iProducerFeign.getUsername(); } @Autowired public void setiProducerFeign(IProducerFeign iProducerFeign) { this.iProducerFeign = iProducerFeign; } }
- 启动
Consumer
,我们可以看到在Nacos
如下图所示
调用 Consumer
的接口consumer/testNacos
,结果如下图所示,同样的如果此时更改了 Nacos
配置文件中的内容,Consumer
这边也是可以实时更新的,感兴趣的小伙伴可以自己试试。
今天主要给大家介绍了一下如何通过 SpringBoot
项目来接入 Naocs 实现服务注册与发现,以及配置管理和动态刷新,相关的代码已经上传到 GitHub 了,公众号回复【源码】获取地址。