简而言之,nacos与eureka的不同之处有三:后台老板、部署方式、功能。nacos是阿里的,eureka是奈飞的;nacos有自己的安装包,需要独立部署,eureka仅作为一个服务组件,引入jar包即可;nacos=eureka+config,它既是注册中心,也是配置中心。

  如何使用nacos?先下载安装包(下载地址https://github.com/alibaba/nacos/releases),打开下载页 -> 点击目前最新版本1.1.3跳到下载页 -> 点击压缩包 -> 下载到本地(E盘download目录)后解压 -> 通过命令行启动nacos:

C:\Users\wulf>e:

E:\>cd download\nacos\bin

E:\download\nacos\bin>startup.cmd -m standalone

         ,--.
,--.'|
,--,: : | Nacos 1.1.
,`--.'`| ' : ,---. Running in stand alone mode, All function modules
| : : | | ' ,'\ .--.--. Port:
: | \ | : ,--.--. ,---. / / | / / ' Pid: 32916
| : ' '; | / \ / \. ; ,. :| : /`./ Console: http://10.10.100.110:8848/nacos/index.html
' ' ;. ;.--. .-. | / / '' | |: :| : ;_
| | | \ | \__\/: . .. ' / ' | .; : \ \ `. https://nacos.io
' : | ; .' ," .--.; |' ; :__| : | `----. \
| | '`--' / / ,. |' | '.'|\ \ / / /`--' /
' : | ; : .' \ : : `----' '--'. /
; |.' | , .-./\ \ / `--'---'
'---' `--`---' `----' -- ::, INFO Bean 'org.springframework.security.config.annotation.configuration.ObjectPostProcessorConfiguration' of type [org.springframework.security.config.annotation.configuration.ObjectPostProcessorConfiguration$$EnhancerBySpringCGLIB$$209b3f7a] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) -- ::, INFO Bean 'objectPostProcessor' of type [org.springframework.security.config.annotation.configuration.AutowireBeanFactoryObjectPostProcessor] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) -- ::, INFO Bean 'org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler@6e9175d8' of type [org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) -- ::, INFO Bean 'org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration' of type [org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration$$EnhancerBySpringCGLIB$$456fe22c] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) -- ::, INFO Bean 'methodSecurityMetadataSource' of type [org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) -- ::, INFO Tomcat initialized with port(s): (http) -- ::, INFO Root WebApplicationContext: initialization completed in ms -- ::, INFO Initializing ExecutorService 'applicationTaskExecutor' -- ::, INFO Adding welcome page: class path resource [static/index.html] -- ::, INFO Creating filter chain: Ant [pattern='/'], [] -- ::, INFO Creating filter chain: Ant [pattern='/**/*.css'], [] -- ::, INFO Creating filter chain: Ant [pattern='/**/*.js'], [] -- ::, INFO Creating filter chain: Ant [pattern='/**/*.html'], [] -- ::, INFO Creating filter chain: Ant [pattern='/**/*.map'], [] -- ::, INFO Creating filter chain: Ant [pattern='/**/*.svg'], [] -- ::, INFO Creating filter chain: Ant [pattern='/**/*.png'], [] -- ::, INFO Creating filter chain: Ant [pattern='/**/*.ico'], [] -- ::, INFO Creating filter chain: Ant [pattern='/console-fe/public/**'], [] -- ::, INFO Creating filter chain: Ant [pattern='/v1/auth/login'], [] -- ::, INFO Creating filter chain: Ant [pattern='/v1/console/health/**'], [] -- ::, INFO Creating filter chain: Ant [pattern='/v1/cs/**'], [] -- ::, INFO Creating filter chain: Ant [pattern='/v1/ns/**'], [] -- ::, INFO Creating filter chain: Ant [pattern='/v1/cmdb/**'], [] -- ::, INFO Creating filter chain: Ant [pattern='/actuator/**'], [] -- ::, INFO Creating filter chain: Ant [pattern='/v1/console/server/**'], [] -- ::, INFO Creating filter chain: any request, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@6c8bca63, org.springframework.security.web.context.SecurityContextPersistenceFilter@6d24ffa1, org.springframework.security.web.header.HeaderWriterFilter@74cadd41, org.springframework.security.web.authentication.logout.LogoutFilter@42a9e5d1, com.alibaba.nacos.console.filter.JwtAuthenticationTokenFilter@2364305a, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@773f7880, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@4c51bb7, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@470a696f, org.springframework.security.web.session.SessionManagementFilter@55e8ec2f, org.springframework.security.web.access.ExceptionTranslationFilter@62fad19, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@5536379e] -- ::, INFO Exposing endpoint(s) beneath base path '/actuator' -- ::, INFO Initializing ExecutorService 'taskScheduler' -- ::, INFO Tomcat started on port(s): (http) with context path '/nacos' -- ::, INFO Nacos Log files: E:\download\nacos/logs/ -- ::, INFO Nacos Conf files: E:\download\nacos/conf/ -- ::, INFO Nacos Data files: E:\download\nacos/data/ -- ::, INFO Nacos started successfully in stand alone mode. -- ::, INFO Initializing Servlet 'dispatcherServlet' -- ::, INFO Completed initialization in ms

  我们通过localhost:8848/nacos可以访问到它,默认用户名密码都是nacos:

  我们创建个项目来注册到这个nacos,三板斧再来:

  1、pom引入spring boot、spring cloud、spring cloud alibaba和nacos依赖:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.wlf</groupId>
<artifactId>spring-cloud-alibaba-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
</parent> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.9.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

  2、application配置nacos地址:

#端口
server.port=
#应用名
spring.application.name=lxytrans-provider #注册中心
spring.cloud.nacos.discovery.server-addr=localhost:

  3、启动类加注解@EnableDiscoveryClient:

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; @EnableDiscoveryClient
@SpringBootApplication
public class TransProviderApplication { public static void main(String[] args) {
SpringApplication.run(TransProviderApplication.class, args);
} @Slf4j
@RestController
static class TestController { @GetMapping("/hello")
public String hello() {
return "hello";
} @GetMapping("/hey")
public String hey() throws InterruptedException {
Thread.sleep(2000);
return "hey";
} }
}

  跑起来后我们看下nacos控制台实例:

   

  以上是服务提供方注册到nacos, 消费方的注册跟服务方没啥不同,这里就不再列出。nacos更多介绍参见https://nacos.io/zh-cn/docs/what-is-nacos.html。至于消费方如何通过nacos调用提供方服务,参见0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例

  

0.9.0.RELEASE版本的spring cloud alibaba nacos实例的更多相关文章

  1. 0.9.0.RELEASE版本的spring cloud alibaba sentinel实例

    sentinel即哨兵,相比hystrix断路器而言,它的功能更丰富.hystrix仅支持熔断,当服务消费方调用提供方发现异常后,进入熔断:sentinel不仅支持异常熔断,也支持响应超时熔断,另外还 ...

  2. 0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例

    这里的feign依然是原来的feign,只不过将注册中心由eureka换成了nacos.服务提供方参见0.9.0.RELEASE版本的spring cloud alibaba nacos实例,消费方跟 ...

  3. 0.9.0.RELEASE版本的spring cloud alibaba nacos+gateway网关实例

    gateway就是用来替换zuul的,功能都差不多,我们看下它怎么来跟nacos一起玩.老套路,三板斧: 1.pom: <?xml version="1.0" encodin ...

  4. 0.9.0.RELEASE版本的spring cloud alibaba sentinel+gateway网关实例

    sentinel除了让服务提供方.消费方用之外,网关也能用它来限流.我们基于上次整的网关(参见0.9.0.RELEASE版本的spring cloud alibaba nacos+gateway网关实 ...

  5. 0.9.0.RELEASE版本的spring cloud alibaba sentinel+feign降级处理实例

    既然用到了feign,那么主要是针对服务消费方的降级处理.我们基于0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例添油加醋,把sentinel功能加 ...

  6. 0.9.0.RELEASE版本的spring cloud alibaba sentinel限流、降级处理实例

    先看服务提供方的,我们在原来的sentinel实例(参见0.9.0.RELEASE版本的spring cloud alibaba sentinel实例)上加上限流.降级处理,三板斧只需在最后那一斧co ...

  7. Spring Cloud Alibaba | Nacos服务中心初探

    目录 Spring Cloud Alibaba | Nacos服务中心初探 1. 什么是Nacos? 1.1 Nacos 1.0 1.2 Nacos 2.0 2. Nacos 架构及概念 2.1 服务 ...

  8. Spring Cloud Alibaba | Nacos服务注册与发现

    目录 Spring Cloud Alibaba | Nacos服务注册与发现 1. 服务提供者 1.1 pom.xml项目依赖 1.2 配置文件application.yml 1.3 启动类Produ ...

  9. Spring Cloud Alibaba | Nacos配置管理

    目录 Spring Cloud Alibaba | Nacos配置管理 1. pom.xml 项目依赖 2. 在 bootstrap.properties 中配置 Nacos server 的地址和应 ...

随机推荐

  1. 我花了2个月时间,整理了100篇Linux技术精华,技术人必看

    一个以技术为立身根基的教育机构做出来的微信号,干货程度会有多高? 马哥Linux运维公众号运营五年,从一开始的定位就是给技术人分享加薪干货的地方.这五年里,公众号运营最重的任务就是做内容.内容并不好做 ...

  2. Java8 中的 Optional 相关用法

    基本方法: ofNullable() 为可能 null 的值创建一个 Optional 实例,  然后可以对该实例遍历/过滤, 判断是否存在,或者为空时执行.. ifPresent(...) 如果值存 ...

  3. springboot整合shiro引用配置文件配置redis信息报空指针异常

    1.问题现象: 上面这些属性是从application.properties配置文件中获取的,按常理来说应该能顺利获取到,但均未赋上值. 2.解决办法:(不得不说百度,千篇一律,最后用谷歌找到的) 最 ...

  4. python 杂记-unittest

    介绍单元测试的好文:https://mp.weixin.qq.com/s/njxc8GXSlc3z_RibK70ROg setUpModule/tearDownModule:在整个模块的开始和结束时被 ...

  5. 使用Java实现数据库编程-----------查询学生记录

    查询所有学生记录,包含年级名称 @Override public LIst<Student>getAllStudent() throws Exception{ List<Studen ...

  6. 肤浅的聊聊关联子查询,数据集连接,TiDB代码,关系代数,等等

    本章涉及的内容是TiDB的计算层代码,就是我们编译完 TiDB 后在bin目录下生成的 tidb-server 的可执行文件,它是用 go 实现的,里面对 TiPD 和 TiKV实现了Mock,可以单 ...

  7. Spring的注解@Qualifier用法

    Spring的注解@Qualifier用法在Controller中需要注入service那么我的这个server有两个实现类如何区分开这两个impl呢?根据注入资源的注解不同实现的方式有一点小小的区别 ...

  8. bzoj2738矩阵乘法

    题意: 给你一个N*N的矩阵,没有修改,每次询问一个子矩形中的第K小数. 题目链接 思路: 当它只有一列时,其实就是区间第K大,也就是整体二分可以解决的. 现在到了二维,只需要将之前的树状数组改成二维 ...

  9. Mac 下 安装 Nginx

    ---恢复内容开始--- Mac 下 安装nginx 首先确定自己有安装homebrew 安装 nginx brew install nginx 启动nginx 1.15版本下 安装是 直接在ngin ...

  10. maven 聚合工程 > 坑总结

    maven聚合工程子项目无法构建:父工程未 install tomcat可以运行,maven 无法编译报错,检查是否添加红色语句: <dependency> <groupId> ...