Nacos的基础总结(一)

目录

一,Nacos的简单介绍

二,Nacos的安装和开机自启设置

2.1 Nacos的安装启动(这里是支持Linux版的)

2.2,测试

https://github.com/alibaba/nacos/tagshttps://github.com/alibaba/nacos/tags


一,Nacos的简单介绍

    Nacos的全写是Naming Configuration Service,表示配置中心,注册中心。属于阿里巴巴的开源项目。

二,Nacos的安装和开机自启设置

2.1 Nacos的安装启动(这里是支持Linux版的)

1)安装:选择需要的版本下载并解压到 Linux的usr/local中。(下载地址在文末)

[root@localhost ~]# cd /usr/upload
[root@localhost upload]# tar -zxvf nacos-server-1.4.1.tar.gz -C /usr/local    

2)启动:(startup.cmd表示windows的启动操作)

[root@localhost local]# cd nacos/bin/
[root@localhost bin]# ./startup.sh -m standalone #非集群模式启动
... ...
nacos is starting with standalone
nacos is starting,you can check the /usr/java/nacos/logs/start.out

 关闭:(shutdown.cmd 表示windows的关闭操作)

[root@localhost bin]# ./shutdown.sh
The nacosServer(3543) is running...
Send shutdown request to nacosServer(3543) OK

3)经常使用的话可以设置成开机自启,节省时间。

(1)在文件nacos.service中添加以下内容 

 vim /lib/systemd/system/nacos.service

[Unit]
Description=nacos
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nacos/bin/startup.sh -m standalone
ExecReload=/usr/local/nacos/bin/shutdown.sh
ExecStop=/usr/local/nacos/bin/shutdown.sh
PrivateTmp=true

[Install]
WantedBy=multi-user.target

(2)修改启动 startup.sh文件 

 vim startup.sh

(3)设置开机启动

systemctl daemon-reload        #重新加载服务配置
systemctl enable nacos.service #设置为开机启动

systemctl start nacos.service  #启动nacos服务
systemctl stop nacos.service   #停止nacos服务

2.2,测试

1,sprispringcloud_common

pojo工具类

public class User {
    private Integer id;
    private String name;
    private Integer age;

    public User() {
    }

    public User(Integer id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

2,nacos_provider

pox 文件

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.bjpowernode</groupId>
            <artifactId>springcloud_common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
<!--        Nacos服务中心的启动器-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
    </dependencies>

yml文件

server:
  port: 8090
spring:
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.122.131:8848 #nacos服务的地址
  application:
    name: nacos-provider #注册到nacos的服务名

 controller

@RestController
@RequestMapping("/provider")
public class ProviderController {

    @Autowired
    private UserService userService;

    @RequestMapping("/getUserById/{id}")
    public User getUserById(@PathVariable Integer id){
        return userService.getUserById(id);
    }
}

启动类

@SpringBootApplication
@EnableDiscoveryClient //把当前服务注册到nacos,并发现其它服务
public class NacosProviderApp {

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

 service

//实现类
@Service
public class UserServiceImpl implements UserService {
    @Override
    public User getUserById(Integer id) {
        return new User(id,"@遗失的风",18);
    }
}


//接口
public interface UserService {
    User getUserById(Integer id);
}

3,nacos_consumer

pom文件

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.bjpowernode</groupId>
            <artifactId>springcloud_common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
    </dependencies>

 yml文件

server:
  port: 80

spring:
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.122.131:8848
  application:
    name: nacos-consumer

启动类 

@SpringBootApplication
@EnableDiscoveryClient //开启nacos,允许注册当前服务,并发现其它服务
public class ConsumerApp {

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

 config

@Configuration
public class BeanConfig {

    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

 controller 

@RestController
@RequestMapping("/consumer")
public class ConsumerController {

    @Autowired
    public RestTemplate restTemplate;
    @Autowired
    public DiscoveryClient discoveryClient;

    @RequestMapping("/getUserById/{id}")
    public User getUserById(@PathVariable Integer id){
        //获取nacos中注册的所有服务信息
        List<String> serviceList = discoveryClient.getServices();
        for (String service : serviceList) {
            System.out.println(service);
        }
        ServiceInstance instance = discoveryClient.getInstances("nacos-provider").get(0);
        String url =                 
         "http://"+instance.getHost()+":"+instance.getPort()+"/provider/getUserById/"+id;
        return restTemplate.getForObject(url, User.class);
    }
}

 结果:

nacos_consumer   调用服务  nacos_provider

 nacos_provider  调用服务  nacos_provider

https://github.com/alibaba/nacos/tagshttps://github.com/alibaba/nacos/tags

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值