栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

nacos搭建和使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

nacos搭建和使用

nacos搭建和使用
      • 第一步:安装nacos
      • 第二步:运行服务端
      • 第三步client 服务

分三步, 最大的坑是版本 版本 还是版本!依赖一定要用对版本

第一步:安装nacos

https://github.com/alibaba/nacos/releases/tag/1.0.1 最后面的是版本
下载下来后 先导入 nacos-mysql.sql 到本地数据库 然后 配置 application 主要配置

spring.datasource.platform=mysql

### Count of DB:
db.num=1

### Connect URL of DB:
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?serverTimezone=GMT%2B8&characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=root
db.password=root

然后在bin目录运行 (win10): ./startup.cmd -m standalone
访问 http://127.0.0.1:8848/nacos 默认账密 nacos / nacos
注意 mysql8 需要2.0的nacos 不然会报连不上数据库

第二步:运行服务端

pom 注意 springboot和 nacos版本依赖



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.3.RELEASE
    
    com.terry
    nacos_platform
    0.0.1-SNAPSHOT
    nacos_platform
    nacos_platform
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
            2.2.3.RELEASE
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

    

 
    
        
            spring-milestones
            Spring Milestones
            https://repo.spring.io/milestone
        
        
            spring-snapshots
            Spring Snapshots
            https://repo.spring.io/snapshot
            
                true
            
        
    
    
        
            spring-milestones
            Spring Milestones
            https://repo.spring.io/milestone
        
        
            spring-snapshots
            Spring Snapshots
            https://repo.spring.io/snapshot
            
                true
            
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


服务端 yml

server:
  port: 8070

spring:
  application:
    name: nacos-provider
    cloud:
      nacos:
        discovery:
          server-addr: 127.0.0.1:8848 # nacos服务地址


# 自定义参数
myName: nacos-server

java启动类

package com.cn;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;


@SpringBootApplication
@EnableDiscoveryClient //开启服务注册发现功能
public class nacosProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(nacosProviderApplication.class, args);
    }

}

第三步client 服务

yml 配置

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

myName: nacosConsumer

pom



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.3.RELEASE
    
    com.terry
    customer
    0.0.1-SNAPSHOT
    customer
    customer
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
            2.2.3.RELEASE
        
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
            2.2.3.RELEASE
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


客户服务启动类

package com.terry.customer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class CustomerApplication {

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

}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/950789.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号