美文网首页
spring cloud 使用nacos

spring cloud 使用nacos

作者: 目标技术宅 | 来源:发表于2020-08-06 14:00 被阅读0次
    nacos 文档地址:https://nacos.io/zh-cn/index.html
    github地址:https://github.com/alibaba/nacos

    demo 使用版本如下
    spring boot 2.3.1
    spring cloud Hoxton.SR6
    nacos 1.3.1

    POM引用

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR6</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    

    配置信息

    server:
      port: 8123
    spring:
      application:
        name: nacos-test
      profiles:
        active: dev
      cloud:
        nacos:
          server-addr: ${server:localhost}:${port:8848}
          discovery:
            group: TEST_GROUP
          config:
            group: CF_GROUP
            name: imp
            file-extension: yaml
    

    Application.java

    @SpringBootApplication
    @EnableDiscoveryClient
    public class NacosTestApplication {
        public static void main(String[] args) {
            SpringApplication.run(NacosTestApplication.class, args);
        }
    }
    

    ControllerDemo.java

    @RestController
    @RefreshScope
    @Slf4j
    public class TestController {
     
        @Value("${oss.endpoint}")
        private String name;
     
        @GetMapping("/test")
        public String test(){
            log.info("name:" + name);
            return name;
        }
    }
    
    config 配置
    image2020-7-30_10-22-58.png
    discovery 注册结果
    image2020-7-30_10-22-33.png

    nacos 运维 nacos + Prometheus + Grafana

    docker部署,官方文档地址:https://nacos.io/zh-cn/docs/quick-start-docker.html

    使用docker部署时,做了些修改
    docker-compose.yml

    version: "2"
    services:
      nacos:
        image: nacos/nacos-server:latest
        container_name: nacos-standalone
        env_file:
          - ./nacos.env
        volumes:
        - ./standalone-logs/:/home/nacos/logs
        - ./init.d/custom.properties:/home/nacos/init.d/custom.properties
        ports:
        - "3848:8848"
      prometheus:
        container_name: prometheus
        image: prom/prometheus:latest
        volumes:
          - ./prometheus-standalone.yaml:/etc/prometheus/prometheus.yml
        ports:
          - "3090:9090"
        depends_on:
          - nacos
        restart: on-failure
      grafana:
        container_name: grafana
        image: grafana/grafana:latest
        ports:
          - 3000:3000
        restart: on-failure
    

    prometheus.yml

    # my global config
    global:
      scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
      evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
      # scrape_timeout is set to the global default (10s).
     
    # Alertmanager configuration
    alerting:
      alertmanagers:
        - static_configs:
            - targets:
              # - alertmanager:9093
     
    # Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
    rule_files:
    # - "first_rules.yml"
    # - "second_rules.yml"
     
    # A scrape configuration containing exactly one endpoint to scrape:
    # Here it's Prometheus itself.
    scrape_configs:
      # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
      - job_name: 'prometheus'
     
        # metrics_path defaults to '/metrics'
        # scheme defaults to 'http'.
     
        static_configs:
          - targets: ['10.228.81.30:3090']
     
      - job_name: 'nacos'
        metrics_path: '/nacos/actuator/prometheus'
        static_configs:
          - targets: ['10.228.81.30:3848']
    

    nacos 启动参数 nacos.env

    MODE=standalone
    NACOS_SERVER_PORT=3848
    SPRING_DATASOURCE_PLATFORM=mysql
    MYSQL_SERVICE_HOST=10.228.81.19
    MYSQL_SERVICE_PORT=38309
    MYSQL_SERVICE_DB_NAME=nacos_config
    MYSQL_SERVICE_USER=root
    MYSQL_SERVICE_PASSWORD=Chenfan@123.com.cn..
    

    相关文章

      网友评论

          本文标题:spring cloud 使用nacos

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