Spring Cloud Alibaba教程:Nacos

Nacos是什么

Nacos 致力于幫助您發(fā)現(xiàn)、配置和管理微服務,它 提供了一組簡單易用的特性集,幫助您快速實現(xiàn)動態(tài)服務發(fā)現(xiàn)、服務配置、服務元數(shù)據(jù)及流量管理。

注冊中心

nacos-server

可以直接從GitHub上下載安裝包:https://github.com/alibaba/nacos/releases
啟動成功后,瀏覽器訪問:http://127.0.0.1:8848/nacos/index.html Nacos控制臺,默認的賬號密碼為nacos/nacos

服務提供者

@RestController
@EnableDiscoveryClient
@SpringBootApplication
public class NacosProvideApplication {

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

        @GetMapping("/helloNacos")
        public String helloNacos(){
                return "hello,nacos!";
        }
}

server:
    port: 9527
spring:
    application:
        name: nacos-provide
    cloud:
        nacos:
            discovery:
                server-addr: 127.0.0.1:8848

服務消費者

@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class NacosConsumerApplication {

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

        @Autowired
        private RestTemplate restTemplate;

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

        @GetMapping("/consumer")
        public String test1() {
                String result = restTemplate.getForObject("http://nacos-provide/helloNacos", String.class);
                return "Return : " + result;
        }
}

server:
    port: 9528
spring:
    application:
        name: nacos-consumer
    cloud:
        nacos:
            discovery:
                server-addr: 127.0.0.1:8848

對接OpenFeign

定義遠程接口

通過@FeginClient注解指定被調(diào)用方的服務名,通過fallback屬性指定RemoteHystrix類,來進行遠程調(diào)用的熔斷和降級處理。

@FeignClient(name = "nacos-provide",fallback = RemoteHystrix.class)
public interface RemoteClient {

        @GetMapping("/helloNacos")
        String helloNacos();
}

@Component
public class RemoteHystrix implements RemoteClient {
        @Override
        public String helloNacos() {
                return "請求超時了";
        }
}

調(diào)用遠程接口

@SpringBootApplication
@RestController
@EnableDiscoveryClient
@EnableFeignClients
public class NacosFeignApplication {

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

        @Autowired
        private RemoteClient remoteClient;

        @GetMapping("/feign")
        public String test() {
                return remoteClient.helloNacos();
        }
}

server:
    port: 9529

spring:
    application:
        name: nacos-feign
    cloud:
        nacos:
            discovery:
                server-addr: 127.0.0.1:8848

配置中心

在Nacos-Server中新建配置,其中Data ID它的定義規(guī)則是:{prefix}-{spring.profile.active}.${file-extension}

  • prefix 默認為 spring.application.name 的值,也可以通過配置項 spring.cloud.nacos.config.prefix 來配置。

  • spring.profile.active 即為當前環(huán)境對應的 profile,可以通過配置項 spring.profile.active 來配置。

  • file-exetension 為配置內(nèi)容的數(shù)據(jù)格式,可以通過配置項 spring.cloud.nacos.config.file-extension 來配置。目前只支持 properties 和 yaml 類型。

  • ** 注意:當 spring.profile.active 為空時,對應的連接符 - 也將不存在,dataId 的拼接格式變成 {prefix}.{file-extension}**
    spring:
    application:
    name: nacos-config
    cloud:
    nacos:
    discovery:
    server-addr: 127.0.0.1:8848
    config:
    server-addr: 127.0.0.1:8848
    file-extension: yml

      @SpringBootApplication
      @EnableDiscoveryClient
      @RestController
      @RefreshScope
      public class NacosConfigApplication {
    
              public static void main(String[] args) {
                      SpringApplication.run(NacosConfigApplication.class, args);
              }
    
              @Value("${nacos.config}")
              private String config;
    
              @RequestMapping("/getValue")
              public String getValue() {
                      return config;
              }
      }
    

多環(huán)境配置

Data ID方案

Data ID的命名規(guī)則為:{prefix}-{spring.profile.active}.${file-extension},通過其中的spring.profile.active屬性即可進行多環(huán)境下配置文件的讀取

Group方案(不推薦使用)

首先配置Group為自定義Group
其次修改項目配置文件bootstrap.yml
spring:
application:
name: nacos-config
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
config:
server-addr: 127.0.0.1:8848
prefix: ${spring.application.name}
file-extension: yml
group: DEV_GROUP

命名空間方案

先創(chuàng)建命名空間,然后在命名空間下建Data ID
spring:
application:
name: nacos-config
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
config:
server-addr: 127.0.0.1:8848
prefix: ${spring.application.name}
file-extension: yml
namespace: edbd013b-b178-44f7-8caa-e73071e49c4d

共享配置

共享配置文件與項目自身配置文件在同一Group中

    spring:
        application:
            name: nacos-config-share
        cloud:
            nacos:
                discovery:
                    server-addr: 127.0.0.1:8848
                config:
                    server-addr: 127.0.0.1:8848
                    prefix: ${spring.application.name}
                    file-extension: yml
                    shared-dataids: shareconfig1.yml,shareconfig2.yml
                    refreshable-dataids: shareconfig1.yml,shareconfig2.yml

共享配置文件與項目自身配置文件不在同一Group中

    spring:
        application:
            name: nacos-config-share
        cloud:
            nacos:
                discovery:
                    server-addr: 127.0.0.1:8848
                config:
                    server-addr: 127.0.0.1:8848
                    prefix: ${spring.application.name}
                    file-extension: yml
                    shared-dataids: shareconfig1.yml,shareconfig2.yml
                    refreshable-dataids: shareconfig1.yml,shareconfig2.yml
                    ext-config:
                        - data-id: shareconfig3.yml
                            group: SHARE3_GROUP
                            refresh: true
                        - data-id: shareconfig4.yml
                            group: SHARE4_GROUP
                            refresh: true

持久化

在0.7版本之前,在單機模式時nacos使用嵌入式數(shù)據(jù)庫實現(xiàn)數(shù)據(jù)的存儲,不方便觀察數(shù)據(jù)存儲的基本情況。0.7版本增加了支持mysql數(shù)據(jù)源能力

集群部署

添加mysql數(shù)據(jù)源

一定要集成數(shù)據(jù)庫共享數(shù)據(jù),修改Nacos-server目錄conf/下的application.properties文件

修改集群配置

修改conf/下的cluster.conf.example文件,將其命名為cluster.conf,內(nèi)容如下
10.1.8.27:8848
10.1.8.28:8848
10.1.8.29:8848

配置Nginx

upstream nacos-server {
    server 127.0.0.1:8849;
    server 127.0.0.1:8850;
    server 127.0.0.1:8851;
}

server {
    listen 8848;
    server_name  localhost;
    location /nacos/ {
        proxy_pass http://nacos-server/nacos/;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容