SpringCloud之Nacos篇

:
springcloud 是使用配置优先的。

比如我现在有一个应用为:

ruoyi-system

系统会按照如下的配置进行查找

1.先找 ruoyi-system-dev.yml,如果找到了则返回,如果找不到则进行下一步。

2.查找 jpaas-form.yml 如果找不到,则查找共享的配置,根据上面的配置他会查找common.yml

例子
在这里插入图片描述
在这里插入图片描述
这样就能读取到配置在Nacos的数据库配置

.下拉数据存Nacos

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
得到的是key-value型 在这里插入图片描述
从数据库读的是数字 转换成汉字

toString 很重要,不然结果会是null



//这是服务提供者
server:
  port: 9000
spring:
  application:
    name: spring-cloud-nacos-producer
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
	@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello(@RequestParam String name) {
        return "hello "+name+",producer is ready";
    }

}
启动服务producer,在浏览器访问链接:http://localhost:9000/hello?name=nacos, 可以看到页面显示hello nacos,producer is ready。


三.Nacos可以和Feign集成使用
	Feign远程调用
package com.spring.nacoscosumers.remote;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(name= "spring-cloud-nacos-producer")//这个是提供者服务名
public interface HelloRemote {
    @RequestMapping(value = "/hello")
    String hello(@RequestParam(value = "name") String name);
}
//控制层调用
@RestController
public class HelloController {

    @Autowired
    HelloRemote helloRemote;

    @RequestMapping("/hello/{name}")
    public String index(@PathVariable("name") String name) {
        return helloRemote.hello(name);
    }
}
启动服务消费者nacos-consumers,打开浏览器访问链接:http://localhost:8080/hello/nacos, 这时页面正常返回hello nacos,producer is ready,证明我们的已经通过Nacos作为注册中心已经正常提供了服务注册与发现。
列子:https://www.geekdigging.com/2019/08/31/3763985100/
. 集成Gateway网关
server:
  port: 8088
spring:
  application:
    name: spring-cloud-nacos-gateway
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    gateway:
      discovery:
        locator:
          enabled: true
      routes:
        - id: hello_route
          #格式为:lb://应用注册服务名
          uri: lb://spring-cloud-nacos-producer
          predicates:
            - Method=GET
我们启动Gateway,打开浏览器访问链接:http://localhost:8088/spring-cloud-nacos-producer/hello?name=nacos, 浏览器正常返回:hello nacos,producer is ready, 证明我们通过服务网关来访问服务是正常的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值