Nacos Spring Cloud实现服务的注册与发现

nacos官网

安装Nacos

下载安装包

  1. 最新稳定版本 下载 nacos-server-$version.zip 包。
  2. 解压安装包了,在bin目录下运行startup.cmd

运行截图

  1. 访问http://localhost:8848/nacos/#/login即可进入nacos控制台中心,账号密码默认都是nacos

Nacos discovery

Spring Cloud Alibaba Nacos Discovery

该项目通过自动配置以及其他 Spring 编程模型的习惯用法为 Spring Boot 应用程序在服务注册与发现方面提供和 Nacos 的无缝集成。 通过一些简单的注解,您可以快速来注册一个服务,并使用经过双十一考验的 Nacos 组件来作为大规模分布式系统的服务注册中心。

服务注册发现: Nacos Discovery Starter

服务发现是微服务架构体系中最关键的组件之一。如果尝试着用手动的方式来给每一个客户端来配置所有服务提供者的服务列表是一件非常困难的事,而且也不利于 服务的动态扩缩容。Nacos Discovery Starter 可以帮助您将服务自动注册到 Nacos 服务端并且能够动态感知和刷新某个服务实例的服务列表。除此之外,Nacos Discovery Starter 也将服务实例自身的一些元数据信息-例如 host,port,健康检查URL,主页等-注册到 Nacos 。

启动一个 Provider 应用

以下步骤向您展示了如何将一个服务注册到 Nacos。

1. 在项目的pom.xml中添加依赖

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

2. 配置application.properties(或application.yml)

spring:
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
  application:
    name: application-name  <!--项目名-->

3.将服务添加@EnableDiscoveryClient注解

@EnableDiscoveryClient
@SpringBootApplication
public class MallCouponApplication {
    public static void main(String[] args) {
        SpringApplication.run(MallCouponApplication.class, args);
    }
}

4. 将服务重启,即可在后台的控制中心的服务列表中查看到服务

服务注册成功

启动一个 Consumer 应用

1. 引用openfeign依赖

各微服务之间的通信我们使用openfeign。

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

2. 在Provider 应用Controller中写一个测试接口

@RequestMapping("/member/list")
public R membercoupons() {
    CouponEntity couponEntity = new CouponEntity();
    couponEntity.setCouponName("满100减10");
    return R.ok().put("coupon", Arrays.asList(couponEntity));
}

3. 在Consumer应用中编写一个接口,告诉SpringCloud这个接口需要调用远程服务

//这是一个声明式的远程调用
//声明每一个方法是调用哪个远程服务的哪一个方法
@FeignClient("mall-coupon")  //该处需要写我们在nacos控制中心注册的服务名,即告诉springcloud我们这个接口需要在哪个服务中调用
public interface CouponFeignService {
    //放入我们上一步中编写的测试接口的Mapping和方法名称
    @RequestMapping("/coupon/coupon/member/list")   //注意这里是完整路径
    public R membercoupons();
}

4. 开启远程调用功能,即添加@EnableFeignClients注解

@EnableFeignClients(basePackages = "com.atbgpi.mall.member.feign") //这里需要写上第3步中编写的接口的完整路径
@EnableDiscoveryClient
@SpringBootApplication()
public class MallMemberApplication {
    public static void main(String[] args) {
        SpringApplication.run(MallMemberApplication.class, args);
    }
}

5. 写一个调用Provide服务中接口的测试接口

@Autowired
private CouponFeignService couponFeignService; //这是第3步中我们编写的接口

@RequestMapping("/coupons")
public R test() {

    //会自动去调用Provide服务中的membercoupons方法
    R membercoupons = couponFeignService.membercoupons();
    System.out.println(membercoupons);

    return R.ok().put("coupons", membercoupons.get("coupon"));
}

6. 测试接口

访问第5步中的test接口,如果能正确返回就说明访问成功了。

版本对应关系

对于Spring Cloud和Spring Boot如果没有对应正确的版本,则会在上面的测试中出错,官方也给出了版本对应关系

Spring Cloud Version Spring Cloud Alibaba Version Spring Boot Version
Spring Cloud Hoxton.SR8 2.2.5.RELEASE 2.3.2.RELEASE
Spring Cloud Greenwich.SR6 2.1.3.RELEASE 2.1.13.RELEASE
Spring Cloud Hoxton.SR3 2.2.1.RELEASE 2.2.5.RELEASE
Spring Cloud Hoxton.RELEASE 2.2.0.RELEASE 2.2.X.RELEASE
Spring Cloud Greenwich 2.1.2.RELEASE 2.1.X.RELEASE
Spring Cloud Finchley 2.0.3.RELEASE 2.0.X.RELEASE
Spring Cloud Edgware 1.5.1.RELEASE(停止维护,建议升级) 1.5.X.RELEASE
posted @ 2021-03-13 17:59  bGpi  阅读(184)  评论(0编辑  收藏  举报