SpringCloudAlibaba+Nacos+openFiegn最新教程(送源码)

SpringCloudAlibaba最新上手教程(将原有项目引入分布式)

搭建服务注册与发现中心Nacos

下载并安装Nacos

Nacos这个单词是由NameServer注册中心和configuration即注册中心,这两个单词的首写字母组成的。至于s是指service指该注册/配置中心都是以服务为核心。
Nacos在这里下载
在这里插入图片描述

导包

SpringCloudAlibaba是即插即用的,代码侵入性很小。但是他的导包却对版本有着偏执的要求。

此项目的springBoot版本为2.6.7
在这里插入图片描述
更具官方文档描述,我在这里选用对应的springCloud和SpringCloudAlibaba版本。

官方文档地址为

在主POM文件中
<dependencies>
    <!--Spring Cloud Alibaba 的版本信息-->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-alibaba-dependencies</artifactId>
        <version>2021.0.1.0</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>
    <!--Spring Cloud 的版本信息-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>2021.0.1</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>
</dependencies>
在子POM文件中

在这里我们一步到位,也引入远程调用openFeign所需的包

    <dependencies>
        <!--Spring Cloud Alibaba Nacos discovery -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!--添加 OpenFeign 依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!--由于 Netflix Ribbon 进入停更维护阶段,因此新版本的 Nacos discovery 都已经移除了 Ribbon ,此时我们需要引入 loadbalancer 代替 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-loadbalancer</artifactId>
        </dependency>
    </dependencies>

书写配置文件

在application.properties中

#服务名
spring.application.name=ThirdParty
#Nacos Server 的地址
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

第一个为自己起的服务名在这里插入图片描述
第二个为nacos的地址和端口号

在启动类上加上注解

@EnableDiscoveryClient
以供Nacos进行扫描和注册
在这里插入图片描述

重启项目

重启项目,在浏览器输入网址。检查是否注册成功

http://192.168.127.1:8848/nacos/index.html#/login
服务列表又数据,既表示注册成功(服务名为自己取得名字)
在这里插入图片描述
账号和密码默认为nacos

引入远程调用组件openFeign

导包

我们在搭建Nacos时已经导过了就是下图
在这里插入图片描述
如果在这里提示找不到这两个包80%的原因是没有在主pom.xml文件中引入在这里插入图片描述

准备Feign接口

在这里插入图片描述
在其中一个子项目中新建feign包,feign包下新建feign接口用于远程调用

package com.zxl.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;


@Component
/**
 *
 * 服务提供者提供的服务名称,即 application.name
 *
 */
@FeignClient(value = "ThirdParty")
public interface DeptFeignService {

    @RequestMapping(value = "/phone/sendMessage", method = RequestMethod.GET)
    String sendMessage(@RequestParam("phoneNum") String phoneNum);

}

这里FeignClient后面跟的value是指想被调用的服务名,我们在application.perproties中配置的

第三方服务ThirdParty的名字

自动注入该接口

在想要远程调用的类中,自动注入该feign接口
在这里插入图片描述

测试

在这里插入图片描述
在这里插入图片描述
源码在GITEE

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值