以上代码实现方式与传统 Spring Cloud 的方式无异,下一步启动引导类NacosDiscoveryConsumerSampleApplication
?,并测试运行结果:
% curl http://127.0.0.1:9090/call/echo/Hello,World [ECHO] : Hello,World
结果符合期望,说明 Nacos Discovery 整合 @LoadBalanced RestTemplate 的实现与标准 Spring Cloud 实现的差异仅体现在 Maven 依赖 starter 以及外部化配置上。接下来,应用 nacos-discovery-consumer-sample 将继续与 Spring Cloud OpenFeign 整合。
5.3 Nacos Discovery 整合 Spring Cloud OpenFeign
Spring Cloud OpenFeign 是 Spring Cloud 基于 REST 客户端框架 OpenFeign 而构建,使得服务发现和负载均衡透明,开发人员只需关注服务消费者接口契约。同时,Spring Cloud OpenFeign 可以与 @LoadBalanced RestTemplate 共存,因此,可在原有应用 nacos-discovery-consumer-sample 的基础上,增加 Maven 依赖和代码实现整合。
关于 Spring Cloud OpenFeign 的技术细节,可参考官方文档: https://docs.spring.io/spring-cloud-openfeign/docs/current/reference/html/
5.3.1 服务消费者增加 Spring Cloud OpenFeign Maven 依赖
在 nacos-discovery-consumer-sample 项目 pom.xml 中追加 Spring Cloud OpenFeign Maven 依赖:
<!-- Spring Cloud OpenFeign --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>2.2.2.RELEASE</version> </dependency>
下一步,则是新增 Spring Cloud OpenFeign 服务声明接口
5.3.2 服务消费者增加 Spring Cloud OpenFeign 服务声明接口
由于需要消费应用 nacos-discovery-provider-sample 提供的 REST 服务/echo/{message}
,根据 Spring Cloud OpenFeign 的要求,需要在消费者应用增加 REST 服务声明接口,即:
@FeignClient("nacos-discovery-provider-sample") // 指向服务提供者应用 public interface EchoService { @GetMapping("/echo/{message}") String echo(@PathVariable("message") String message); }
不难发现,echo(String)
? 方法在 Spring MVC 请求映射的方式与 nacos-discovery-provider-sample 中的ServiceController
?基本相同,唯一区别在于 @PathVariable 注解指定了 value 属性 “message”,这是因为默认情况,Java 编译器不会讲接口方法参数名添加到 Java 字节码中。
下一步,激活 Spring Cloud OpenFeign 服务声明接口。
5.3.3 服务消费者激活 Spring Cloud OpenFeign 服务声明接口
激活 Spring Cloud OpenFeign 服务声明接口的方法非常简单,仅需在引导类标注@EnableFeignClients
?,如果声明接口与引导类不在同一个包的话,请使用basePackages
?属性指定。由于本例的EchoService
?与引导类位于同一包下,因此,无需指定:
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients // 激活 @FeignClient public class NacosDiscoveryConsumerSampleApplication { public static void main(String[] args) { SpringApplication.run(NacosDiscoveryConsumerSampleApplication.class, args); } }
激活步骤就此完成,下一步为 Spring Cloud OpenFeign 服务接口增加RestController
? 实现。
5.3.4 服务消费者使用 Spring Cloud OpenFeign 服务声明接口实现服务调用
新增名为OpenFeignController
?的实现类:
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; @RestController public class OpenFeignController { @Autowired private EchoService echoService; @GetMapping("/feign/echo/{message}") public String feignEcho(@PathVariable String message) { return echoService.echo(message); } }
重启引导类NacosDiscoveryConsumerSampleApplication
?,并测试/feign/echo/{message}
?结果:
% curl http://127.0.0.1:9090/feign/echo/Hello,World [ECHO] : Hello,World
结果符合期望,说明 Nacos Discovery 整合 Spring Cloud OpenFeign 与传统方式也是相同的。
综上所述,Nacos Discovery 在 Spring Cloud 服务调用是无侵入的。
6. Nacos Discovery 更多配置项信息
更多关于 Nacos Discovery Starter 的配置项如下所示:
配置项 | Key | 默认值 | 说明 |
服务端地址 | spring.cloud.nacos.discovery.server-addr |
Nacos Server 启动监听的ip地址和端口 | |
服务名 | spring.cloud.nacos.discovery.service |
${spring.application.name} |
注册的服务名 |
权重 | spring.cloud.nacos.discovery.weight |
1 |
取值范围 1 到 100,数值越大,权重越大 |
网卡名 | spring.cloud.nacos.discovery.network-interface |
当IP未配置时,注册的IP为此网卡所对应的IP地址,如果此项也未配置,则默认取第一块网卡的地址 | |
注册的IP地址 | spring.cloud.nacos.discovery.ip |
优先级最高 | |
注册的端口 | spring.cloud.nacos.discovery.port |
-1 |
默认情况下不用配置,会自动探测 |
命名空间 | spring.cloud.nacos.discovery.namespace |
常用场景之一是不同环境的注册的区分隔离,例如开发测试环境和生产环境的资源(如配置、服务)隔离等 | |
AccessKey | spring.cloud.nacos.discovery.access-key |
当要上阿里云时,阿里云上面的一个云账号名 | |
SecretKey | spring.cloud.nacos.discovery.secret-key |
当要上阿里云时,阿里云上面的一个云账号密码 | |
Metadata | spring.cloud.nacos.discovery.metadata |
使用Map格式配置,用户可以根据自己的需要自定义一些和服务相关的元数据信息 | |
日志文件名 | spring.cloud.nacos.discovery.log-name |
||
集群 | spring.cloud.nacos.discovery.cluster-name |
DEFAULT |
Nacos集群名称 |
接入点 | spring.cloud.nacos.discovery.endpoint |
地域的某个服务的入口域名,通过此域名可以动态地拿到服务端地址 | |
是否集成Ribbon | ribbon.nacos.enabled |
true |
一般都设置成true即可 |
是否开启Nacos Watch | spring.cloud.nacos.discovery.watch.enabled |
7. Nacos Discovery Actuator Endpoint
Nacos Discovery 内部提供了一个 Endpoint, 对应的 endpoint id 为nacos-discovery
,其 Actuator Web Endpoint URI 为/actuator/nacos-discovery
注:使用 Nacos Config Spring Cloud 1.x 版本的话,其 URI 地址则为
/nacos-discovery
)
Endpoint 暴露的 json 中包含了两种属性:
- subscribe: 显示了当前服务有哪些服务订阅者
- NacosDiscoveryProperties: 当前应用 Nacos 的基础配置信息
由于 Aliyun Java Initializr 所生成的应用工程默认激活 Spring Boot Actuator Endpoints(JMX 和 Web),具体配置存放在application.properties
文件中,同时,Actuator Web 端口设置为 8081,内容如下:
management.endpoints.jmx.exposure.include=* management.endpoints.web.exposure.include=* management.endpoint.health.show-details=always # Actuator Web 访问端口 management.server.port=8081
因此,应用 nacos-discovery-provider-sample 无需调整,直接访问:http://127.0.0.1:8081/actuator/nacos-discovery,服务响应的内容如下:
{ "subscribe": [ { "jsonFromServer": "", "name": "nacos-provider", "clusters": "", "cacheMillis": 10000, "hosts": [ { "instanceId": "30.5.124.156#8081#DEFAULT#nacos-provider", "ip": "30.5.124.156", "port": 8081, "weight": 1.0, "healthy": true, "enabled": true, "cluster": { "serviceName": null, "name": null, "healthChecker": { "type": "TCP" }, "defaultPort": 80, "defaultCheckPort": 80, "useIPPort4Check": true, "metadata": { } }, "service": null, "metadata": { } } ], "lastRefTime": 1541755293119, "checksum": "e5a699c9201f5328241c178e804657e11541755293119", "allIPs": false, "key": "nacos-provider", "valid": true } ], "NacosDiscoveryProperties": { "serverAddr": "127.0.0.1:8848", "endpoint": "", "namespace": "", "logName": "", "service": "nacos-provider", "weight": 1.0, "clusterName": "DEFAULT", "metadata": { }, "registerEnabled": true, "ip": "30.5.124.201", "networkInterface": "", "port": 8082, "secure": false, "accessKey": "", "secretKey": "" } }