史上最全 SpringCloudAlibaba入门教程,从零开始带你深入♂学习(三)——Nacos客户端开发和openfeign服务间通信

SpringCloudAlibaba(三)——Nacos客户端开发和openfeign服务间通信

nacos客户端开发

1、新建一个模块springclouAlibaba-nacos-client-8989

image

2、导入相关依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>
 <!--加群1025684353一起吹水聊天-->
<!--引入nacos client的依赖-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency>

3、编写application.properties配置文件

server.port=8989
spring.application.name=NACOSCLIENT

#指定服务名称
spring.cloud.nacos.server-addr=47.106.105.80:8848

4、编写主启动类

package com.study.springcloudAlibaba;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
// 加群1025684353一起吹水聊天-->
@SpringBootApplication
@EnableDiscoveryClient   //开启服务的注册(可以省略不写)
public class NacosClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(NacosClientApplication.class,args);
    }
}

5、启动测试

image

openfeign服务间的通信

一、消费者模块

1、新建一个提供者模块springcloudAlibaba-products-9090

领取资料
image

2、导入相关依赖
 <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
     <!--加群1025684353一起吹水聊天-->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
</dependencies>

3、编写application.properties配置文件
server.port=9090
spring.application.name=PRODUCTS
# 加群1025684353一起吹水聊天-->
#指定nacos server地址
spring.cloud.nacos.server-addr=47.106.105.80:8848

4、编写主启动类
package com.sutdy.springcloudAlibaba;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

5、编写controller层
package com.sutdy.springcloudAlibaba.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProductsController {

    @Value("${server.port}")
    private int port;
	 //加群1025684353一起吹水聊天-->
    @GetMapping("/product/{id}")
    public String products(@PathVariable("id") Integer id){
        return "调用商品服务返回:"+id+",当前提供服务的端口为:"+port;
    }
}

二、消费者模块

1、新建一个消费者模块springcloudAlibaba-users-8989

领取资料
image

2、导入相关依赖
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--引入nacos client的依赖-->
         <!--加群1025684353一起吹水聊天-->
        <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>
    </dependencies>

3、编写application.properties配置文件
server.port=8989
spring.application.name=USERS

#指定nacos server地址
spring.cloud.nacos.server-addr=47.106.105.80:8848

4、编写主启动类,添加@EnableFeignClients注解开启Openfeign
package com.study.springcloudAlibaba;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients //开启Openfeign调用支持
public class UserApplication_8989 {
    public static void main(String[] args) {
     //加群1025684353一起吹水聊天-->
        SpringApplication.run(UserApplication_8989.class,args);
    }
}

5、编写openfeign客户端接口,添加@FeignClient("PRODUCTS")注解调用名称为"PRODUCTS"的微服务,实现服务之间的通信。
package com.study.springcloudAlibaba.feignclients;

import org.springframework.cloud.openfeign.FeignClient;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient("PRODUCTS")
public interface ProductClient {
    @RequestMapping("/product/{id}")
    String product(@PathVariable("id") Integer id);
}

6、编写controller层实现服务通信接口
package com.study.springcloudAlibaba.controller;

import com.study.springcloudAlibaba.feignclients.ProductClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @Autowired
    private ProductClient productClient;

    @GetMapping("/invoke")
    public String invokeProduct(){
        String result = productClient.product(21);
        return "调用用户服务成功.....调用商品服务结果:{}"+result;
    }
}

三、启动测试

1、点击 编辑配置

领取资料
image

2、选择ProductsApplication_9090,再点击复制

image

3、点击复制出来的ProductsApplication_9091,再点击配置,修改名称和端口号

image

4、启动服务提供者9090和9091,再启动消费者8989

image

5、进入 http://localhost:8989/invoke 页面

领取资料
image

刷新
image

最后,祝大家早日学有所成,拿到满意offer,快速升职加薪,走上人生巅峰。 可以的话请给我一个三连支持一下我哟,我们下期再见

领取资料

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值