Spring Cloud Alibaba教程:Nacos

Nacos是什么

Nacos 致力于帮助您发现、配置和管理微服务,它 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。

注册中心

nacos-server

可以直接从GitHub上下载安装包:github.com/alibaba/nac… 启动成功后,浏览器访问: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注解指定被调用方的服务名,通过fallback属性指定RemoteHystrix类,来进行远程调用的熔断和降级处理。

@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 "请求超时了";
        }
}复制代码

调用远程接口

@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它的定义规则是:${prefix}-${spring.profile.active}.${file-extension}

  • prefix 默认为 spring.application.name 的值,也可以通过配置项 spring.cloud.nacos.config.prefix 来配置。

  • spring.profile.active 即为当前环境对应的 profile,可以通过配置项 spring.profile.active 来配置。

  • file-exetension 为配置内容的数据格式,可以通过配置项 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;
              }
      }复制代码

    多环境配置

    Data ID方案

    Data ID的命名规则为:${prefix}-${spring.profile.active}.${file-extension},通过其中的spring.profile.active属性即可进行多环境下配置文件的读取

    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复制代码

    命名空间方案

    先创建命名空间,然后在命名空间下建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使用嵌入式数据库实现数据的存储,不方便观察数据存储的基本情况。0.7版本增加了支持mysql数据源能力

  • 进入nacos-server\nacos\conf目录,初始化文件:nacos-mysql.sql

  • Nacos-server其实就是一个Java工程或者说是一个Springboot项目,他的配置文件在nacos-server-1.0.1\nacos\conf目录下,名为 application.properties,在文件底部添加数据源配置:

      spring.datasource.platform=mysql
      db.num=1
      db.url.0=jdbc:mysql://127.0.0.1:3306/mynacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
      db.user=root
      db.password=123456复制代码
  • 启动Nacos-server即可。

    集群部署

    添加mysql数据源

    修改Nacos-server目录conf/下的application.properties文件

修改集群配置

修改conf/下的cluster.conf.example文件,将其命名为cluster.conf,内容如下 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/;
    }
}复制代码

本文由博客群发一文多发等运营工具平台 OpenWrite 发布

猜你喜欢

转载自juejin.im/post/5f17a037e51d453496768b3e