风止雨歇

服务注册中心 ------ Nacos

一、服务的注册中心

自己设计一个服务的注册中心

 

 (1)注册:服务启动的时候将服务信息注册到注册中心;(insert)

(2)心跳:服务内部的定时任务 Timer1 定时发送心跳给注册中心,更新 last_heatTime 字段;(update)

(3)服务间调用:因为 Timer2 定时从注册中心获取其他服务的信息并缓存到服务内部,所以服务1调用服务2的时候直接从服务1内存缓存中得到服务2的ip及端口号。(select)

(4)清理无心跳的服务:Timer3 定时任务将长时间没有收到心跳的实例状态修改为 down。(update)

(5)服务注销:服务在停止的时候注销实例信息。(delete)

二、nacos的安装

1、nacos的下载

wget https://github.com/alibaba/nacos/releases/download/1.4.1/nacos-server-1.4.1.tar.gz

2、解压nacos的包: tar -zxvf nacos-server-1.4.1.tar.gz 

 3、启动nacos

cd nacos/bin
sh startup.sh -m standalone

4、检查nacos是否启动起来: lsof -i:8848 

 5、在浏览器访问 nacos 的地址:http://192.168.1.1:8848/nacos

用户名和密码: nacos/nacos

注意: 

(1)虚拟机要把防火墙关闭或者将对应的端口打开;

(2)浏览器使用 360的极速模式,兼容模式会有问题(登录页面不显示)。(坑~~~)

三、服务注册到nacos

nacos的官方文档:https://nacos.io/zh-cn/docs/quick-start.html

1、父工程的maven坐标

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/>
    </parent>

    <dependencyManagement>
        <dependencies>
            <!--引入springcloud的版本-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!-- 引入spring cloud alibaba -->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.1.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

2、服务的maven坐标引入

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- nacos client  -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-nacos-discovery</artifactId>
        </dependency>

    </dependencies>

3、启动类增加  @EnableDiscoveryClient 注解

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

4、配置文件

spring:
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.172.20:8848
  application:
    name: order-center

server:
  port:8001

5、启动服务

 

posted on 2020-11-16 19:21  风止雨歇  阅读(712)  评论(0编辑  收藏  举报

导航