用SpringCloud Alibaba搭建属于自己的微服务(十四)~基础搭建~使用springcloud openfeign实现rpc调用
一.概述
既然分布式系统,那么就避免不了服务之间的调用,我们这里使用的是springcloud的openfeign.
二.openfeign是什么?和feign、resetTemplate、httpUtils、ribbon是什么关系?
(1)openfeign出现的背景.
springcloud最初是以集成Netflix的feign来实现rpc调用的,之后的事情众所周知Netflix的这些微服务组件都是停更或者不再维护,所以springcloud得自己出一套rpc调用框架,openfeign就出现了,openfeign是在feign的基础上演变的,基本的使用是没有变的.
(2)openfeign和feign、restTemplate、httpUtils、ribbon是什么关系
服务调用:openfein、feign、restTemplate、httpUtils.
openfeign:
以feign为基础的开发的,属于springcloud自己维护的,是伪声明示rpc调用框架,底层实质干活的是resttemplate.
feign:
Netflix的rpc调用框架,是伪声明示rpc调用框架,底层实质干活的是resttemplate.
restTemplate:
便捷的rpc调用工具.
httpUtils:
apche组织维护http调用工具.
负载均衡:ribbon.
ribbon
客户端负载均衡算法提供者,常见的负载均衡规则有轮询、hash等.
三.集成openfein.
(1) 在ccm-mall.pom我们已经引入了spring-cloud-dependencies版本管理的依赖,在子工程中我们引入spring-cloud-starter-openfeign时就无需再声明版本号.
<!--springcloud版本管理依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
(2) 要求:server-user调用server-basic,我们以server-user为模板新建服务server-basic并暴露一个接口.
a.启动类.
package com.ccm.server.basic;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.ComponentScan;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @Description server-basic服务启动类
* @Author zhouzhiwu
* @CreateTime 2020/7/17 9:35
*/
@EnableDiscoveryClient //注册中心客户端
@ComponentScan(basePackages = "com.ccm")
@EnableSwagger2
@SpringBootApplication //声明为一个启动类
public class ServerBasicApplication {
public static void main(String[] args) {
SpringApplication.run(ServerBasicApplication.class,args);
}
}
b.配置文件(bootstrap.yml).
server:
port: 2001 #服务端口
spring:
application:
name: server-basic #服务名称
cloud:
nacos:
discovery:
server-addr: 47.96.131.185:8848
config:
server-addr: 47.96.131.185:8848 #nacos config配置中心ip和端口
file-extension: yaml #文件扩展名格式,针对于默认的{spring.application.name}-${profile}.${file-extension:properties}配置
enabled: true #开启或关闭配置中心
c.暴露接口.
package com.ccm.server.basic.controller;
import com.ccm.common.exception.result.ResultSet;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Description openfeign测试
* @Author zhouzhiwu
* @CreateTime 2020/7/17 9:38
*/
@RestController
@RequestMapping(value = "openFeignTest")
@Api(tags = "openfeign测试服务端")
public class OpenFeignTestController {
@ApiOperation(value = "openfeign测试服务端暴露接口")
@GetMapping(value = "test01")
public ResultSet<String> test01() {
return ResultSet.success("我是server-basic服务的数据");
}
}
(3) server-user.pom中实际引入依赖.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
(4) server-user服务启动类上加@EnableFeignClients表示开启feign调用.
package com.ccm.server.user;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @Description server-user服务启动类
* @Author zhouzhiwu
* @CreateTime 2020/07/03 14:04
*/
@EnableFeignClients
@EnableDiscoveryClient //注册中心客户端
@ComponentScan(basePackages = "com.ccm")
@EnableSwagger2
@MapperScan(basePackages = "com.ccm.server.user.dao.mysql.mapper")
@SpringBootApplication //声明为一个启动类
@Import(value = PaginationInterceptor.class)
public class ServerUserApplication {
public static void main(String[] args) {
SpringApplication.run(ServerUserApplication.class,args);
}
}
(4) 测试代码编写.
a.OpenFeignTestController.java
package com.ccm.server.user.controller;
import com.ccm.common.exception.ServerException;
import com.ccm.common.exception.result.ResultSet;
import com.ccm.server.user.openfeign.ServerBasicFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.omg.CORBA.SystemException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @Description openfeign测试
* @Author zhouzhiwu
* @CreateTime 2020/7/17 9:38
*/
@RestController
@RequestMapping(value = "openFeignTest")
@Api(tags = "openfeign测试客户端")
public class OpenFeignTestController {
@Autowired
private ServerBasicFeign serverBasicFeign;
@ApiOperation(value = "server-user服务调用server-basic服务")
@GetMapping(value = "test01")
public ResultSet<String> test01() {
ResultSet<String> feignResultset = serverBasicFeign.test01();
if(feignResultset.getCode() == 0) {
return ResultSet.success(feignResultset.getData());
}
throw new ServerException("调用feign服务失败");
}
}
b.ServerBasicFeign.java
@FeignClient(name = “server-basic”)
代表这个interface调用server-basic微服务.
@GetMapping(value = “openFeignTest/test01”)
代表用get请求调用server-basic服务路径为openFeignTest/test01的接口.
ResultSet<String>
代表使用ResultSet接收调用接口返回的数据.
package com.ccm.server.user.openfeign;
import com.ccm.common.exception.result.ResultSet;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
/**
* @Description 调用server-basic的feign层
* @Author zhouzhiwu
* @CreateTime 2020/07/17 9:46
*/
@FeignClient(name = "server-basic")
public interface ServerBasicFeign {
@GetMapping(value = "openFeignTest/test01")
ResultSet<String> test01();
}
(5) swagger接口测试.
成功拿到server-basic接口返回的数据,完事.
源码地址:https://gitee.com/chouchimoo/ccm-mall.git(本章节分支:zj-14)
本文地址:https://blog.csdn.net/theOldCaptain/article/details/107401762
推荐阅读
-
用SpringCloud Alibaba搭建属于自己的微服务(十四)~基础搭建~使用springcloud openfeign实现rpc调用
-
用SpringCloud Alibaba搭建属于自己的微服务(四)~基础搭建~maven工程管理
-
用SpringCloud Alibaba搭建属于自己的微服务(十二)~基础搭建~alibaba nacos的服务注册和发现
-
用SpringCloud Alibaba搭建属于自己的微服务(十四)~基础搭建~使用springcloud openfeign实现rpc调用
-
用SpringCloud Alibaba搭建属于自己的微服务(四)~基础搭建~maven工程管理