Springboot 项目集成 Nacos 实现服务注册发现与配置管理(二)

启动 Producer,并且手动调用接口,启动 Producer 过后,我们在 Nacos 的服务注册列表可以看如下所示的内容,在 test1 的命名空间下,已经有了我们创建的 Producer 服务。

Springboot 项目集成 Nacos 实现服务注册发现与配置管理(二)

通过手动调用 Producer 的接口 http://127.0.0.1:8080/producer/getUsername 显示如下内容

Springboot 项目集成 Nacos 实现服务注册发现与配置管理(二)

并且我们看下此时 Nacos 的配置中心里面配置文件com.example.properties 里面的内容正是这个,这个时候我们手动把配置里面password 参数的值改成JavaGeek666,再次访问接口,我们会发现接口的输出也自动改变了。

Springboot 项目集成 Nacos 实现服务注册发现与配置管理(二)

修改配置内容如下:

Springboot 项目集成 Nacos 实现服务注册发现与配置管理(二)

再次访问结果如下:

Springboot 项目集成 Nacos 实现服务注册发现与配置管理(二)


服务调用者 Consumer

前面我们已经完成了Producer 的服务注册与配置动态生效的功能,这个时候基本已经可以使用了,不过我们还需要更进一步通过 Nacos 来实现服务发现,接下来我们创建 ConsumerSpringBoot 的项目,配置文件和pom.xml 文件基本一致,只要修改端口以及对应地方,下面贴一下不一样的地方

  1. 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
  1. 启动类,配置上 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);
    }
}
  1. 编写调用 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;
    }
}
  1. 启动Consumer,我们可以看到在 Nacos 如下图所示

Springboot 项目集成 Nacos 实现服务注册发现与配置管理(二)

调用 Consumer 的接口consumer/testNacos,结果如下图所示,同样的如果此时更改了 Nacos 配置文件中的内容,Consumer 这边也是可以实时更新的,感兴趣的小伙伴可以自己试试。

Springboot 项目集成 Nacos 实现服务注册发现与配置管理(二)

今天主要给大家介绍了一下如何通过 SpringBoot 项目来接入 Naocs 实现服务注册与发现,以及配置管理和动态刷新,相关的代码已经上传到 GitHub 了,公众号回复【源码】获取地址。


上一篇:ubuntu 12.04 安装 mariadb-server10.0


下一篇:u盘安装ubuntu10.04 server.txt