本文主要是介绍SpringCloud Alibaba微服务实战(三) - Nacos服务创建消费者(Feign),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
什么是Feign
Feign 是一个声明式的伪 Http 客户端,它使得写 Http 客户端变得更简单。使用 Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用 Feign 注解和 JAX-RS 注解。Feign 支持可插拔的编码器和解码器。Feign 默认集成了 Ribbon,Nacos 也很好的兼容了 Feign,默认实现了负载均衡的效果
- Feign 采用的是基于接口的注解
- Feign 整合了 ribbon
创建服务者注册,请参考文章:SpringCloud Alibaba微服务实战(二) - Nacos服务注册与发现
构建服务消费者cloud-consumer-feign进行服务调用
1.在父项目中创建子module项目名字为cloud-consumer-feign,在pom中引入nacos服务注册依赖
<!--Spring web--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--SpringCloud Alibaba nacos--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency>
完整服务消费者cloud-consumer-feign的pom.xml
<?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"><parent><artifactId>cloud-alibaba</artifactId><groupId>com.zsy.springcloud</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>cloud-consumer-feign</artifactId><dependencies><!--Spring web--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--SpringCloud Alibaba nacos--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build><repositories><!--指定阿里云镜像库下载依赖--><repository><id>central</id><name>aliyunmaven</name><url>http://maven.aliyun.com/nexus/content/groups/public/</url></repository></repositories>
</project>
2.配置application.yml或者properties文件
server:port: 8083
spring:#服务应用名字application:name: cloud-consumer-feigncloud:# 指定nacos控制台地址,配置注册ip:端口,注意即使是80端口也不可能省略nacos:discovery:server-addr: 127.0.0.1:8848
3.FeignClient接口定义AccountService
package com.zsy.springcloud.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.Map;
@FeignClient("cloud-account")
public interface AccountService {//获取客户信息@RequestMapping(value = "/getAccount", method = RequestMethod.GET)public Map<String,Object> getAccount();
}
4.创建消费者AccountConsumerController
package com.zsy.springcloud.controller;
import com.zsy.springcloud.service.AccountService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
@Log4j2
@RestController
public class AccountConsumerController {@Resourceprivate AccountService accountService;@RequestMapping(value = "/getAccount", method = RequestMethod.GET)public Map<String,Object> getAccount() {Map<String,Object> account = new HashMap<>();log.info("---------消费者开始------------");account = accountService.getAccount();log.info("---------消费者结束--------account{}----", account);return account;}
}
5.配置消费者启动服务的启动类
package com.zsy.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient //开启服务注册
@EnableFeignClients//开启feign客户端
public class FeignConsumerApplication {public static void main(String [] args) {SpringApplication.run(FeignConsumerApplication.class, args);}
}
6.启动cloud-consumer服务项目,启动成功,如下图
7. 验证服务,浏览器访问http://localhost:8083/getAccount,返回如下图
查看消费者cloud-consumer控制台,如下图
查看服务者cloud-account控制台,如下图
码云地址:https://gitee.com/zlzhaoe/cloud-alibaba
这篇关于SpringCloud Alibaba微服务实战(三) - Nacos服务创建消费者(Feign)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!