springcloud整合nacos实现服务的注册与发现

首先新建父工程

添加依赖如下:

   <dependencies>
            <!-- spring-cloud-alibab-dependencies -->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba</artifactId>
                <version>2.1.0.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- spring-cloud-dependencies -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!-- spring-boot-dependencies -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.1.3.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

 

接着新建提供者模块依赖于父工程

依赖如下:

    <dependencies>

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

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

    </dependencies>

 

application.yml配置文件如下:

server:
  port: 56010


spring:
  application:
    name: quickstart-provider
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
logging:
  level:
    root: info
    org.springframework: info

 

新建个controller,使用api测试

package com.ckf.nacos.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author serene
 * @date 2020/8/22 16:18
 */

@RestController
public class ProviderController {

    private static final Logger logger = LoggerFactory.getLogger(ProviderController.class);

    @GetMapping("/service")
    public String service(){
        logger.info("provider invoke");
        return "provider invoke";
    }
}

 

启动类

package com.ckf.nacos;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @EnableDiscoveryClient 服务发现客户端
 * @EnableFeignClients feign客户端 进行远程调用
 * @author: serene
 * @date: 2020/8/022 16:26
 * @description:
 */

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ProviderApplication {

    public static void main(String[] args) {
        System.out.println("服务提供者启动成功");
        SpringApplication.run(ProviderApplication.class, args);
    }
}

 

接着新建消费者模块依赖于父工程

新建feign客户端

package com.ckf.nacos.client;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

/**@FeignClient 调用服务的名称  yml配置文件的服务名
 * @author serene
 * @date 2020/8/22 16:40
 */

@FeignClient(value = "quickstart-provider")
public interface ProviderClient {

    /**
     * 调用服务的方法
     * @return
     */
    @GetMapping("/service")
    public String service();
}

 

新建controller

package com.ckf.nacos.controller;

import com.ckf.nacos.client.ProviderClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author serene
 * @date 2020/8/22 16:46
 */
@RestController
public class ConsumerController {

    private static final Logger logger = LoggerFactory.getLogger(ConsumerController.class);

    /**
     * 动态代理对象,内部远程调用服务生产者
     */
    @Autowired
    private ProviderClient providerClient;


    @GetMapping("/service")
    public String service(){
        logger.info("consumer invoke");
        //远程调用
        String service = providerClient.service();
        return "comsumer invoke "+"|"+" + service";
    }
}

 

application.yml配置如下:

server:
port: 56020


spring:
application:
name: quickstart-consumer
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
logging:
level:
root: info
org.springframework: info

 

服务消费者启动类如下:

package com.ckf.nacos;

import com.ckf.nacos.controller.ConsumerController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @EnableDiscoveryClient 服务发现客户端
 * @EnableFeignClients feign客户端 进行远程调用
 * @author: serene
 * @date: 2020/8/022 16:26
 * @description:
 */

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerApplication {

    private static final Logger logger = LoggerFactory.getLogger(ConsumerController.class);

    public static void main(String[] args) {

        logger.info("服务消费者启动成功");
        System.out.println("服务消费者启动成功");
        SpringApplication.run(ConsumerApplication.class, args);
    }
}

 

然后启动nacos,再启动服务提供者,就可以看到服务已经注册上来了。

 

再启动服务消费者

这样nacos的服务提供者与消费者就完成了,如有需要进行调用就好了。

 

 

 

接下来测试多实例负载均衡

首先以传参的形式定义端口号,这样在启动脚本里可以指定具体的端口号。

修改提供者的yml配置文件

 

在启动脚本里可以指定端口号(这里创建两个脚本实现多实例,ProviderApplication1 与 ProviderApplication2)

 

 

 

然后启动刚修改的两个脚本,nacos服务列表显示如下:

由此可以看出有两个实例了

 

 

 

 

 

然后再启动消费者服务,我们访问消费者的api

 

 

 每次启动在控制台都是以轮回的方式打印(提供者1与提供者2,也就是我们刚创建的启动脚本)

 

 

 

项目地址:https://gitee.com/ckfeng/nacos-discovery.git

 

 如遇到问题进qq群讨论:837146509

posted @ 2020-08-23 23:44  安详的苦丁茶  阅读(5404)  评论(0编辑  收藏  举报