千家信息网

NACOS多环境配置的过程

发表于:2025-02-03 作者:千家信息网编辑
千家信息网最后更新 2025年02月03日,这篇文章主要介绍"NACOS多环境配置的过程",在日常操作中,相信很多人在NACOS多环境配置的过程问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"NACOS多环境配置的
千家信息网最后更新 2025年02月03日NACOS多环境配置的过程

这篇文章主要介绍"NACOS多环境配置的过程",在日常操作中,相信很多人在NACOS多环境配置的过程问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"NACOS多环境配置的过程"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

Linux/Unix/Mac

启动命令(standalone代表着单机模式运行,非集群模式):

sh startup.sh -m standalone

如果您使用的是ubuntu系统,或者运行脚本报错提示[[符号找不到,可尝试如下运行:

bash startup.sh -m standalone

Windows

启动命令:

cmd startup.cmd

或者双击startup.cmd运行文件。

访问:127.0.0.1:8848/nacos/index.html 出现登录界面,启动成功。(用户名和秘密都是nacos)

服务注册到NACOS

首先Nacos规定必须是bootstrap配置文件才能注入。我项目中用yml,示例如下:

server:  port: 8060spring:  application:    name: power-match #项目名  profiles:     active: local  cloud:    nacos:      config:        server-addr: 127.0.0.1:8848 #注册中心地址      discovery:         server-addr: 127.0.0.1:8848

其次启动类示例如下(我使用了feign):

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.openfeign.EnableFeignClients;@EnableFeignClients@EnableDiscoveryClient@SpringBootApplicationpublic class PowerMatchApplication {                public static void main(String[] args) {                SpringApplication.run(PowerMatchApplication.class, args);        }}

运行后去Nacos页面查看,效果如下:

表示注册成功。

分环境注册

在这我只举例官方推荐的方法,别的就不再介绍了。还是同一个nacos,登录--找到命名空间--新建命名空间,输入内容后就会生成命名空间ID

以application-local.yml配置为例:

spring:  cloud:    nacos:      config:        namespace: e503611c-9c54-4669-baff-e12770b3e948      discovery:        namespace: e503611c-9c54-4669-baff-e12770b3e948ribbon:      ReadTimeout: 60000      ConnectTimeout: 60000

启动后,去看服务列表的test下面就有注册的服务了,只会服务调用就会只在local中调用。

他支持多种服务消费方式WebClient、Feign、RestTemplate。

WebClient

@EnableDiscoveryClient@SpringBootApplicationpublic class TestApplication {    public static void main(String[] args) {        SpringApplication.run(TestApplication.class, args);    }    @Slf4j    @RestController    static class TestController {        @Autowired        private WebClient.Builder webClientBuilder;        @GetMapping("/test")        public Mono test() {            Mono result = webClientBuilder.build()                    .get()                    .uri("http://alibaba-nacos-discovery-server/hello?name=didi")                    .retrieve()                    .bodyToMono(String.class);            return result;        }    }    @Bean    @LoadBalanced    public WebClient.Builder loadBalancedWebClientBuilder() {        return WebClient.builder();    }}

使用Feign

上面介绍的RestTemplate和WebClient都是Spring自己封装的工具,下面介绍一个Netflix OSS中的成员,通过它可以更方便的定义和使用服务消费客户端。下面也举一个具体的例子,其实现内容与上面两种方式结果一致:

第一步:在pom.xml中增加openfeign的依赖:

    org.springframework.cloud    spring-cloud-starter-openfeign

第二步:定义Feign客户端和使用Feign客户端:

@EnableDiscoveryClient@SpringBootApplication@EnableFeignClientspublic class TestApplication {    public static void main(String[] args) {        SpringApplication.run(TestApplication.class, args);    }    @Slf4j    @RestController    static class TestController {        @Autowired        Client client;        @GetMapping("/test")        public String test() {            String result = client.hello("word");            return "Return : " + result;        }    }    @FeignClient("alibaba-nacos-discovery-server")    interface Client {        @GetMapping("/hello")        String hello(@RequestParam String name);    }}

使用RestTemplate

@EnableDiscoveryClient@SpringBootApplicationpublic class TestApplication {    public static void main(String[] args) {        SpringApplication.run(TestApplication.class, args);    }    @Slf4j    @RestController    static class TestController {        @Autowired        RestTemplate restTemplate;        @GetMapping("/test")        public String test() {            String result = restTemplate.getForObject("http://alibaba-nacos-discovery-server/hello?name=word", String.class);            return result;        }    }    @Bean    @LoadBalanced    public RestTemplate restTemplate() {        return new RestTemplate();    }}

总的来说,还说和以前没的什么区别。

到此,关于"NACOS多环境配置的过程"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

0