【Spring Cloud Alibaba】Nacos 服务注册与发现

1、服务注册

首先创建一个名为:spring-cloud-alibaba-nacos-discovery 的引用,相关依赖为以下内容,核心依赖 spring-cloud-starter-alibaba-nacos-discovery

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

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

        <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-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

修改 application.properties 配置文件

server.port=8081
spring.application.name=spring-cloud-alibaba-nacos-discovery

management.server.port=9091
management.endpoints.jmx.exposure.include=*
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

# Nacos注册信息
spring.cloud.nacos.discovery.server-addr=192.168.31.141:8848
spring.cloud.nacos.discovery.username=nacos
spring.cloud.nacos.discovery.password=nacos
spring.cloud.nacos.discovery.namespace=sandbox-configuration

然后在启动类增加@EnableDiscoveryClient 注解

package cn.tellsea;

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

/**
 * nacos 服务注册与发现 - 提供者
 *
 * @author Tellsea
 * @date 2021/12/20
 */
@SpringBootApplication
@EnableDiscoveryClient
public class SpringCloudAlibabaNacosDiscoveryApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudAlibabaNacosDiscoveryApplication.class, args);
    }

}

编写测试控制层

package cn.tellsea.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Tellsea
 * @date 2021/12/20
 */
@RestController
public class ServiceController {

    @GetMapping("/echo/{message}")
    public String echo(@PathVariable String message) {
        return "[ECHO] : " + message;
    }
}

启动应用,查看日志,提示注册成功

2021-12-20 13:38:08.542  INFO 9420 --- [           main] c.a.c.n.registry.NacosServiceRegistry    : nacos registry, DEFAULT_GROUP spring-cloud-alibaba-nacos-discovery 192.168.31.141:8081 register finished

查看 nacos 的服务注册
【Spring Cloud Alibaba】Nacos 服务注册与发现_spring boot
到此,服务注册完成

2、服务发现:@LoadBalanced + RestTemplate

创建一个新的应用名为 spring-cloud-alibaba-nacos-consumer

  • 依赖和上一节的一样
  • application.properties 和上一节的一样
  • 启动类增加@EnableDiscoveryClient 注解和上一节的一样

编写测试控制器

package cn.tellsea.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author Tellsea
 * @date 2021/12/20
 */
@RestController
public class RestTemplateController {

    @LoadBalanced
    @Autowired
    public RestTemplate restTemplate;

    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @GetMapping("/call/echo/{message}")
    public String callEcho(@PathVariable String message) {
        // 访问应用 spring-cloud-alibaba-nacos-discovery 的 REST "/echo/{message}"
        return restTemplate.getForObject("http://spring-cloud-alibaba-nacos-discovery/echo/" + message, String.class);
    }
}
启动应用,访问

http://localhost:8082/call/echo/66668888

发现浏览器返回

[ECHO] : 66668888

到这里,采用@LoadBalanced + RestTemplate 的方式实现了服务之间的调用

3、服务发现:Spring Cloud OpenFeign

首先消费者模式集成 Feign 依赖

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

启动类上增加注解,表名激活 Feign

@EnableFeignClients

然后编写远程调用的 service 接口

package cn.tellsea.service;

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

/**
 * 指向服务提供者应用
 *
 * @author Tellsea
 * @date 2021/12/20
 */
@FeignClient("spring-cloud-alibaba-nacos-discovery")
public interface EchoService {

    /**
     * 远程方法
     *
     * @param message
     * @return
     */
    @GetMapping("/echo/{message}")
    String echo(@PathVariable("message") String message);
}

Feign 的控制层的方法直接注入之后调用即可

package cn.tellsea.controller;

import cn.tellsea.service.EchoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * Spring Cloud OpenFeign 远程调用
 *
 * @author Tellsea
 * @date 2021/12/20
 */
@RestController
public class OpenFeignController {

    @Autowired
    private EchoService echoService;

    @GetMapping("/feign/echo/{message}")
    public String feignEcho(@PathVariable String message) {
        return echoService.echo(message);
    }
}

启动应用,访问

http://localhost:8082/feign/echo/66668888

发现浏览器返回

[ECHO] : 66668888

综上所述,Nacos Discovery 在 Spring Cloud 服务调用是无侵入的

4、断点监控

从上面的配置中不难看出,我们已经在配置文件中配置了端点监控的信息

management.server.port=9091
management.endpoints.jmx.exposure.include=*
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

Nacos Discovery 内部提供了一个 Endpoint, 对应的 endpoint id 为 nacosdiscovery,其 Actuator Web Endpoint URI 为/actuator/nacos-discovery

注:使用 Nacos Config Spring Cloud 1.x 版本的话,其 URI 地址则为/nacos-discovery )

服务注册端点监控

http://127.0.0.1:9091/actuator/nacos-discovery

【Spring Cloud Alibaba】Nacos 服务注册与发现_spring cloud_02
服务消费端点监控

http://127.0.0.1:9092/actuator/nacos-discovery

【Spring Cloud Alibaba】Nacos 服务注册与发现_spring_03

微信公众号

【Spring Cloud Alibaba】Nacos 服务注册与发现_spring boot_04