Nacos如何使用以及如何编写配置文件

本地nacos的相关配置

spring:
  application:
    name: product-service
  cloud:
    nacos:
      server-addr: 192.168.26.141:8848
      username: nacos
      password: nacos
      discovery:
        namespace: 3fe590e0-91a7-4926-91e3-8bc86e78b4d8
        group: A_GROUP
        service: ${spring.application.name}
      config:
        namespace: ${spring.cloud.nacos.discovery.namespace}
        group: ${spring.cloud.nacos.discovery.group}
        prefix: ${spring.application.name}
        file-extension: yml
        # 共享配置文件
        shared-configs:
          - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
  profiles:
    active: dev   #配置文件的适用环境为dev-开发环境

远程nacos配置中心的配置文件

远程配置文件的

DataID为product-service-dev.yml

Group为A_Group

server:
  port: 8082

虽然给定引用未直接提及Nacos结合OpenFeign配置文件编写方法,但可以从相关概念和使用场景推导部分配置要点。 ### 服务提供者(nacos - producer)配置 在Spring Boot项目里,一般使用`application.properties`或`application.yml`作为配置文件。对于Nacos作为注册中心的服务提供者,配置文件示例如下: #### application.yml ```yaml spring: application: name: nacos - producer # 服务名称 cloud: nacos: discovery: server - addr: 127.0.0.1:8848 # Nacos服务器地址 server: port: 8081 # 服务端口 ``` ### 服务消费者(nacos - openfeign)配置 服务消费者除了要配置Nacos注册中心地址,还需开启OpenFeign功能。 #### application.yml ```yaml spring: application: name: nacos - openfeign # 服务名称 cloud: nacos: discovery: server - addr: 127.0.0.1:8848 # Nacos服务器地址 feign: hystrix: enabled: false # 若使用Hystrix熔断,可开启此配置 server: port: 8082 # 服务端口 ``` #### 开启OpenFeign注解 如果Spring Cloud版本在E之后可省略此步骤,需要在启动类添加`@EnableDiscoveryClient`注解和`@EnableFeignClients`注解,示例代码如下: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class NacosOpenfeignApplication { public static void main(String[] args) { SpringApplication.run(NacosOpenfeignApplication.class, args); } } ``` ### OpenFeign接口配置 在服务消费者中定义OpenFeign接口,示例如下: ```java import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; @FeignClient(name = "nacos - producer") // 服务提供者的名称 public interface NacosProducerFeignClient { @GetMapping("/hello") // 服务提供者的接口路径 String hello(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值