一:怎么引用依赖

dependencyManagement:spring cloud alibaba 的版本,由于spring cloud alibaba 还未纳入spring cloud 的主版本管理中,所以需要自己加入。

我们在Parent 项目中引入Spring Cloud:

Spring Cloud Alibaba 项目都是基于 Spring Cloud,而 Spring Cloud 项目又是基于 Spring Boot 进行开发,并且都是使用 Maven 做项目管理工具。在实际开发中,我们一般都会创建一个依赖管理项目作为 Maven 的 Parent 项目使用,这样做可以极大的方便我们对 Jar 包版本的统一管理。

具体的版本介绍请下如下配置:

Spring Cloud Finchley 是较老一个版本,对应springboot 2.0.x

最开始这样加载依赖:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>

升级后:

<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.0.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>

Spring Cloud Greenwich 是较新一个版本,对应对应springboot 2.1.x

最开始这样加载依赖:

<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>

升级后:

<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>

dependencies:当前应用要使用的依赖内容。这里主要新加入了 Nacos 的服务注册与发现模块:spring-cloud-starter-alibaba-nacos-discovery。由于 dependencyManagement 中已经引入了版本,这里就不用指定具体版本了。

服务提供者,添加如下依赖:

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

nacos的配置中心,添加如下依赖:

<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

配置:

Spring Boot 配置文件的加载顺序,其中 bootstrap.properties 配置为最高优先级:
依次为 bootstrap.properties -> bootstrap.yml -> application.properties -> application.yml

首先在 bootstrap.yml 文件里,进行nacos的配置拉取:

示例:

spring:
application:
name: ace-admin
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848
file-extension: yaml
profiles:
active: dev

服务注册地址:

spring:
application:
name: nacos-provider
cloud:
nacos:
discovery:
server-addr: 192.168.233.150:8848 server:
port: 8082 management:
endpoints:
web:
exposure:
include: "*"
user:
name: caps

入口函数添加注解:@EnableDiscoveryClient

在项目中找一个 Controller ,增加一个请求方法,测试配置更新效果

// 注入配置文件上下文
@Autowired
private ConfigurableApplicationContext applicationContext; // 从上下文中读取配置
@GetMapping(value = "/hi")
public String sayHi() {
return "Hello " + applicationContext.getEnvironment().getProperty("user.name");
}

使用 FeignClient 优化调用服务

首先在pom.xml中添加FeignClient引用

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>

通过 Spring Cloud 原生注解 @EnableFeignClients 开启

编写远程服务调用接口

package com.ichochy.nacos.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; @FeignClient(value = "service-provider")
public interface ServiceInterface { @RequestMapping("/service/echo/{name}")
public String echo(@PathVariable String name); }

远程调用服务

package com.ichochy.nacos.controller;

import com.ichochy.nacos.service.ServiceInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; @RestController
@RequestMapping("/result")
public class ResultController { @Autowired
private ServiceInterface serviceInterface; @RequestMapping("feignClient/{name}")
public String feignClient(@PathVariable String name){
return serviceInterface.echo(name);
} }

访问地址:http://127.0.0.1:8080/result/feignClient/ichochy

参考资料:

https://www.cnblogs.com/faramita/p/11340475.html

二:nacos 的服务注册的更多相关文章

  1. Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现

    自Spring Cloud Alibaba发布第一个Release以来,就备受国内开发者的高度关注.虽然Spring Cloud Alibaba还没能纳入Spring Cloud的主版本管理中,但是凭 ...

  2. Spring Cloud(二):服务注册与发现 Eureka【Finchley 版】

    Spring Cloud(二):服务注册与发现 Eureka[Finchley 版]  发表于 2018-04-15 |  更新于 2018-05-07 |  上一篇主要介绍了相关理论,这一篇开始我们 ...

  3. nacos 的服务注册与发现

    nacos的服务注册于发现. 这个要求服务统一注册到注册中心,然后调用的时候就不需要通过ip来调用,直接通过服务名即可. 服务提供者 pom.xml配置,需要spring-cloud-starter- ...

  4. Spring Cloud Alibaba 使用Nacos作为服务注册中心

    为什么需要注册中心? 在分布式架构中,服务会注册到这里,当服务需要调用其它服务时,就到这里找到服务的地址,进行调用:服务管理,核心是有个服务注册表,心跳机制动态维护 : 服务注册 创建普通Spring ...

  5. SpringCloud第二代实战系列:一文搞定Nacos实现服务注册与发现

    一.背景:SpringCloud 生态圈 在正式开始本篇文章之前我们先岔开来讲一下SpringCloud的生态圈. SpringCloud大家都比较熟悉了,它制定了分布式系统的标准规范,做了高度抽象和 ...

  6. springcloud(二):Eureka服务注册与发现

    Spring Cloud Netflix  该项目是Spring Cloud的子项目之一,主要内容是对Netflix公司一系列开源产品的包装,它为Spring Boot应用提供了自配置的Netflix ...

  7. SofaBoot使用Nacos进行服务注册发现

    前提 最近创业公司的项目组基于业务需要,开发一套新的微服务,考虑到选用的组件必须是主流.社区活跃.生态完善以及方便迁移到云上等因素,引入了SOFAStack全家桶.微服务开发里面,一个很重要的功能就是 ...

  8. Spring Cloud Alibaba系列(一)nacos作为服务注册中心

    Spring Cloud Alibaba各组件版本关系 Spring Cloud Alibaba Version Sentinel Version Nacos Version RocketMQ Ver ...

  9. SpringCloudAlibaba - 整合 Nacos 实现服务注册与发现

    目录 前言 环境 Nacos是什么? 服务发现原理 搭建 Nacos Server Nacos Server 下载地址 Nacos Server 的版本选择 运行 Nacos Server Nacos ...

  10. 微服务学习笔记二:Eureka服务注册发现

    Eureka服务注册发现 服务发现:云端负载均衡,一个基于 REST 的服务,用于定位服务,以实现云端的负载均衡和中间层服务器的故障转移. 1. Service Discovery: Eureka S ...

随机推荐

  1. CENTOS 6.4 编译安装APACHE PHP MYSQL ZEND【转载未测试】

    CENTOS 6.4 编译安装APACHE PHP MYSQL ZEND 由 cache 发布于 WWW 2013-07-21 [ 5560 ] 次浏览 [ 0 ] 条评论 标签: LAMP 搞网站跑 ...

  2. 行业软件开发商怎样来抢 BI 这块蛋糕?

    随着企业信息化建设的深入,很多机构的生产系统已建设完成,在保证生产的有序进行后,如何更有效地进行经营决策成了管理者面临的最大任务.商业智能(BI)就是在这种背景成为支撑企业经营分析的一大利器. 根据 ...

  3. C内存操作API的实现原理

    我们在编写C代码时,会使用两种类型的内存,一种是栈内存,另外一种是堆内存,其中栈内存的申请和释放是由编译器来隐式管理的,我们也称为自动内存,这种变量是最简单而且最常用的,然后就是堆内存,堆的申请和释放 ...

  4. 牛客网-SQL专项练习1

    ①检索所有比"王华"年龄大的学生姓名.年龄和性别.SQL语句: 解析: 第一步:先找到王华的年龄 SELECT AGE FROM S WHRE SN = "王华" ...

  5. 真正的HTAP对用户和开发者意味着什么?

    简介: Gartner 2016 年首次提出 HTAP(Hybrid Transaction / Analytical Processing,混合事务分析处理)并给出明确的定义:即同时支持 OLTP ...

  6. Flutter+FaaS一体化任务编排的思考与设计

    作者:闲鱼技术-古风 Flutter+Serverless三端一体研发架构,客户端不仅仅是编写双端的代码,而是扩展了客户端的工作边界,形成完整的业务闭环.在新的研发模式落地与实践的过程中,一直在思考如 ...

  7. 如何在零停机的情况下迁移 Kubernetes 集群

    ​简介:本文将通过集群迁移的需求.场景以及实践方式,介绍如何基于阿里云容器服务 ACK,在零停机的情况下迁移 Kubernetes 集群. 作者:顾静(子白)|阿里云高级研发工程师:谢瑶瑶(初扬)|阿 ...

  8. 前端使用 Konva 实现可视化设计器(7)- 导入导出、上一步、下一步

    请大家动动小手,给我一个免费的 Star 吧~ 这一章实现导入导出为JSON文件.另存为图片.上一步.下一步. github源码 gitee源码 示例地址 导出为JSON文件 提取需要导出的内容 ge ...

  9. dotnet C# 多次对一个对象调用构造函数会发生什么

    今天来玩一点变态的,使用反射获取到某个类型的构造函数,接着多次对此类型的某个对象调用构造函数方法.请问此时会发生什么 假定有一个类型 Foo 的定义如下 class Foo : IDisposable ...

  10. IEC104 从站/服务端模拟器 调试工具推荐

    目录 IEC104 从站/服务端模拟器 调试工具推荐 主要功能 软件截图 IEC104 从站/服务端模拟器 调试工具推荐 下载地址:http://www.redisant.cn/iec104serve ...