使用Nacos注册和配置SpringCloud微服务

前言 上篇我们讲到如何使用k8s搭建nacos,这篇就来讲讲微服务如何通过搭建好的nacos服务注册和配置。
操作 A服务、B服务,A使用Feign调用B服务里面方法。
A、B服务引入相关依赖包,

  • springcloud版本:2020.0.0
  • springboot版本:2.4.2
  • alibaba版本:2021.1
    org.springframework.boot spring-boot-starter-parent 2.4.2 【使用Nacos注册和配置SpringCloud微服务】1.8 2.4.2 2020.0.0 2021.1 org.springframework.boot spring-boot-dependencies ${spring-boot.version} pom import org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import com.alibaba.cloud spring-cloud-alibaba-dependencies ${spring-cloud-alibaba.version} pom import org.springframework.cloud spring-cloud-starter-openfeign org.springframework.cloud spring-cloud-starter-loadbalancer com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config org.springframework.cloud spring-cloud-starter-bootstrap

    A、B服务注册和配置到Nacos服务的bootstrap.yml文件:
    spring: application: name: a-service # b-service profiles: active: test cloud: nacos: #注册A、B服务到Nacos discovery: server-addr: http://nacos-headless.default.svc.cluster.local:8848 namespace: xxx-xxx-xxx-xxx service: a-service # b-service #配置A、B服务到Nacos config: server-addr: http://nacos-headless.default.svc.cluster.local:8848 file-extension: yaml prefix: a-service # b-service namespace: xxx-xxx-xxx-xxx server: port: a-port # b-port

可以直接在Nacos上面创建A、B服务的配置文件:
使用Nacos注册和配置SpringCloud微服务
文章图片

使用Nacos注册和配置SpringCloud微服务
文章图片

B服务方法:
@SpringBootApplication @EnableDiscoveryClient @RestController @RefreshScope public class BServiceApplication { public static void main(String[] args) { SpringApplication.run(BServiceApplication.class, args); }@Value("${server.port}") String port; @GetMapping("/hi") public String hi(@RequestParam(value = "https://www.it610.com/article/name", defaultValue = "https://www.it610.com/article/forezp",required = false) String name) { return "hello " + name + ", i'm provider ,my port:" + port; } }

A服务想要调用B服务方法,首先创建BFeignClient类:
@FeignClient("b-service") public interface BFeignClient { @GetMapping("/hi") String hi(@RequestParam(value = "https://www.it610.com/article/name", defaultValue = "https://www.it610.com/article/forezp", required = false) String name); }

接着,创建调用的方法:
@SpringBootApplication @RestController @EnableDiscoveryClient @EnableFeignClients @RefreshScope public class AServiceApplication {public static void main(String[] args) { SpringApplication.run(AServiceApplication.class, args); }@Autowired BFeignClient bFeignClient; @GetMapping("/hi-feign") public String hiFeign(){ return bFeignClient.hi("feign"); } }

运行起来,看看两个服务是否在Nacos注册成功,如图所示:
使用Nacos注册和配置SpringCloud微服务
文章图片

注册成功之后,我们就可以测试A服务调用B服务是否成功:
使用Nacos注册和配置SpringCloud微服务
文章图片

总结 1、因为我们使用的是service headless,所以我们要使用dns查找nacos,默认是你的service name 加上固定写法:default.svc.cluster.local,我的nacos service name是nacos-headless,即:nacos-headless.default.svc.cluster.local
2、因为nacos使用的是最新版本,所以discovery要加service属性,否则报错
discovery: server-addr: http://nacos-headless.default.svc.cluster.local:8848 namespace: xxx-xxx-xxx-xxx service: a-service# b-service

3、因为使用的是springcloud:2020.0版本,所以pom引入bootstrap包,否则bootstrap.yml不起作用
org.springframework.cloud spring-cloud-starter-bootstrap

引用 SpringCloud 2020版本教程1:使用nacos作为注册中心和配置中心
k8s集群内部怎么通过dns域名使得业务系统注册到nacos集群说明-方法论,其他通过域名访问服务一样的
k8s部署nacos 3各节点....N各节点均可 集群
Kubernetes Nacos

    推荐阅读