美文网首页
Nacos 入门

Nacos 入门

作者: markeNick | 来源:发表于2021-01-19 15:58 被阅读0次

    简介

    【官方文档】

    • 前四个字母为Nameing和Configuration的前两个字母,最后的s为Service。Nacos是一个更易于构建原生应用的动态服务发现、配置管理和服务管理平台。

    • 能够替代Eureka做服务注册中心、替代Config做服务配置中心

    • Nacos支持AP、CP的切换

    Docker安装单机版Nacos

    • 进入一个文件夹,然后Clone项目
    git clone https://github.com/nacos-group/nacos-docker.git
    cd nacos-docker
    
    • 使用MySQL5.7
    docker-compose -f example/standalone-mysql-5.7.yaml up
    
    • 使用MySQL8
    docker-compose -f example/standalone-mysql-8.yaml up
    

    Nacos 作为服务注册中心

    服务提供者

    新建module
    pom.xml的依赖
    <dependencies>
            <!--SpringCloud ailibaba nacos -->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            </dependency>
            <!-- SpringBoot整合Web组件 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    </dependencies>
    
    application.yml
    server:
      port: 9001
    
    spring:
      application:
        name: nacos-payment-provider
      cloud:
        nacos:
          discovery:
            server-addr: 192.168.138.135:8848 # 配置Nacos地址
    
    management:
      endpoints:
        web:
          exposure:
            include: '*'
    
    启动类
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    
    @SpringBootApplication
    @EnableDiscoveryClient
    public class PaymentApplication9001 {
        public static void main(String[] args) {
            SpringApplication.run(PaymentApplication9001.class,args);
        }
    }
    
    一个简单的测试controller
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class TestController {
    
        @Value("${server.port}")
        private String serverPort;
    
        @GetMapping(value = "/payment/nacos")
        public String getPayment() {
    
            String result = "nacos registry, serverPort: " + serverPort;
    
            return result;
        }
    }
    

    参考上面步骤,再新建一个提供者实例,端口9002,或者使用idea启动多实例,用于下面演示负载均衡

    服务消费者

    新建module
    pom.xml的依赖
    <dependencies>
            <!--SpringCloud ailibaba nacos -->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            </dependency>
            <!-- SpringBoot整合Web组件 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    </dependencies>
    
    application.yml
    server:
      port: 8400
    
    spring:
      application:
        name: nacos-order-consumer
      cloud:
        nacos:
          discovery:
            server-addr: 192.168.138.135:8848 # 配置Nacos地址
    
    
    # 消费者将要去访问的微服务名称(注册成功进nacos的微服务提供者)
    service-url:
      nacos-user-service: http://nacos-payment-provider
    
    启动类
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    
    @SpringBootApplication
    @EnableDiscoveryClient
    public class OrderApplication8400 {
    
        public static void main(String[] args) {
            SpringApplication.run(OrderApplication8400.class, args);
        }
    
    }
    
    RestTemplate配置类
    import org.springframework.cloud.client.loadbalancer.LoadBalanced;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.client.RestTemplate;
    
    @Configuration
    public class ApplicationContextConfig {
    
        @Bean
        @LoadBalanced
        public RestTemplate restTemplate() {
            return new RestTemplate();
        }
    
    }
    
    一个简单的测试controller
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    
    @RestController
    public class TestController {
    
        @Autowired
        private RestTemplate restTemplate;
    
        @Value("${service-url.nacos-user-service}")
        private String serverUrl;
    
        @GetMapping("/consumer/payment/nacos")
        public String getPayment() {
            String url = serverUrl + "/payment/nacos";
            return restTemplate.getForObject(url, String.class);
        }
    }
    

    测试

    启动2个提供者和1个消费者服务

    浏览器反复访问 http://localhost:8400/consumer/payment/nacos 可以看到会轮询请求提供者服务的不同实例

    之所以可以实现负载均衡,是因为nacos的依赖默认引入了ribbon

    Nacos作为配置中心

    新建module
    pom.xml
    <dependencies>
            <!--nacos-config-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            </dependency>
            <!--nacos-discovery-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            </dependency>
            <!--web + actuator-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
      </dependencies>
    
    bootstrap.yml
    server:
      port: 3377
    
    spring:
      application:
        name: nacos-config-client
      cloud:
        nacos:
          discovery:
            server-addr: 192.168.138.135:8848 # Nacos服务注册中心地址
          config:
            server-addr: 192.168.138.135:8848 #Nacos作为配置中心地址
            file-extension: yaml #指定yaml格式的配置
    #        group: DEFAULT_GROUP
    #        namespace: eeba7661-6b5e-46c6-aa45-5df57f48091f
    
    
    # ${spring.application.name}-${spring.profile.active}.${spring.cloud.nacos.config.file-extension}
    # nacos-config-client-dev.yaml
    
    # nacos-config-client-test.yaml   ----> config.info
    
    application.yml
    spring:
      profiles:
        active: dev # 表示开发环境
        #active: test # 表示测试环境
    
    启动类
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    
    @SpringBootApplication
    @EnableDiscoveryClient
    public class ConfigApplication3377 {
    
        public static void main(String[] args) {
            SpringApplication.run(ConfigApplication3377.class, args);
        }
    
    }
    
    测试controller
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.cloud.context.config.annotation.RefreshScope;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RefreshScope // 支持Nacos的动态刷新功能
    public class TestController {
    
        @Value("${config.info}")
        private String configInfo;
    
        @GetMapping("/config/info")
        public String getConfigInfo() {
            return configInfo;
        }
    }
    

    Naocs的配置文件的dataId命名规则:${prefix}-${spring.profile.active}.${file-extension}

    如:nacos-config-client-dev.yaml

    这里的后缀应该是yaml而不是yml

    • prefix 默认为 spring.application.name 的值,也可以通过配置项 spring.cloud.nacos.config.prefix来配置。
    • spring.profiles.active 即为当前环境对应的 profile,详情可以参考 Spring Boot文档注意:当 spring.profiles.active 为空时,对应的连接符 - 也将不存在,dataId 的拼接格式变成 ${prefix}.${file-extension}
    • file-exetension 为配置内容的数据格式,可以通过配置项 spring.cloud.nacos.config.file-extension 来配置。目前只支持 propertiesyaml 类型

    在控制面板中配置一个yaml文件,用于测试

    配置内容如下:

    然后启动该服务,就会自动去读取 nacos-config-client-dev.yaml的配置内容

    访问:http://localhost:3377/config/info 可以看到配置信息

    接着,在Nacos控制面板编辑配置内容,将version=1改为version=2

    再访问http://localhost:3377/config/info 可以看到信息发生了改变,实现了自动刷新功能

    相关文章

      网友评论

          本文标题:Nacos 入门

          本文链接:https://www.haomeiwen.com/subject/hptczktx.html