文章目录
- 1.远程调用
- 2.代码解析
- 2.1 Eureka微服务
- 2.1 微服务结构以及配置类
- 2.2 公共类
- 2.3 provider微服务
- 2.4 Feign-Order微服务
- 3.结果展示
1.远程调用
- 本地过程调用(Local Procedure Call,LPC),是指同一台机器上运行的不同进程之间的互相通信,即在多进程操作系统中,运行的不同进程之间可以通过 LPC 进行函数调用。
- 远程过程调用(Remote Procedure Call,RPC),是指不同机器上运行的进程之间的相互通信,某一机器上运行的进程在不知道底层通信细节的情况下,就像访问本地服务一样,去调用远程机器上的服务。
被调用方(服务器)有一个开放的接口,然后调用方(用户)通过 Browser 使用这个接口,来间接调用被调用方相应的服务,从而实现远程调用。
-
SpringCloud对Feign进行了增强,使Feign支持了SpringMVC注解 (OpenFeign)
-
OpenFeign = RestTemplate+Ribbon (远程调用+负载均衡)
2.代码解析
这里通过Eureka注册中心注册Feign微服务和Payment微服务
开启3个微服务: Eureka + Feign-Order80 + Payment8001
Feign-Order80和Payment8001导入的依赖为openfeign和eureka client的
<!--openfeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
Eureka微服务导入的依赖为Eureka server
<!--eureka-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
2.1 Eureka微服务
application.yml
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com #eureka服务端的实例名称
client:
register-with-eureka: false #false表示不向注册中心注册自己。
fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service-url:
#集群指向其它eureka
#defaultZone: http://eureka7002.com:7002/eureka/
#单机就是7001自己
defaultZone: http://eureka7001.com:7001/eureka/
2.1 微服务结构以及配置类
- Cloud-api-commons: 公共微服务
- Cloud-provider-payment8001: 微服务提供者
- Cloud-consumer-feign-order80: 微服务消费者, 调用feign接口远程调用提供者微服务
Provider8001微服务:
application.yml
server:
port: 8001
spring:
application:
name: cloud-payment-service
# zipkin:
# base-url: http://localhost:9411
# sleuth:
# sampler:
# #采样率值介于 0 到 1 之间,1 则表示全部采集
# probability: 1
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包
url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true。
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetchRegistry: true
service-url:
#单机版
defaultZone: http://localhost:7001/eureka
# 集群版
#defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
instance:
instance-id: payment8001
#访问路径可以显示IP地址
prefer-ip-address: true
#Eureka客户端向服务端发送心跳的时间间隔,单位为秒(默认是30秒)
#lease-renewal-interval-in-seconds: 1
#Eureka服务端在收到最后一次心跳后等待时间上限,单位为秒(默认是90秒),超时将剔除服务
#lease-expiration-duration-in-seconds: 2
mybatis:
mapperLocations: classpath:mapper/*.xml
type-aliases-package: com.atguigu.springcloud.entities # 所有Entity别名类所在包
OpenFeign-Order80微服务:
application.yml
server:
port: 80
eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
#设置feign客户端超时时间(OpenFeign默认支持ribbon)
ribbon:
#指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间
ReadTimeout: 5000
#指的是建立连接后从服务器读取到可用资源所用的时间
ConnectTimeout: 5000
logging:
level:
# feign日志以什么级别监控哪个接口
com.atguigu.springcloud.service.PaymentFeignService: debug
2.2 公共类
cloud-api-commons 微服务
结果类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult<T> {
private Integer code;
private String message;
private T data;
public CommonResult(Integer code, String message){
this(code, message, null);
}
}
实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Payment {
private Long id;
private String serial;
}
2.3 provider微服务
dao
@Mapper
public interface PaymentDao {
public int create(Payment payment);
public Payment getPaymentById(@Param("id") Long id);
}
service
public interface PaymentService {
public int create(Payment payment);
public Payment getPaymentById(@Param("id") Long id);
}
serviceImpl
@Service
public class PaymentServiceImpl implements PaymentService {
@Resource
private PaymentDao paymentDao;
@Override
public int create(Payment payment) {
return paymentDao.create(payment);
}
@Override
public Payment getPaymentById(Long id) {
return paymentDao.getPaymentById(id);
}
}
controller
@RestController
@Slf4j
public class PaymentController {
@Value("${server.port}")
public String serverPort;
@Resource
private PaymentService paymentService;
@Resource
private DiscoveryClient discoveryClient;
@PostMapping("/payment/create")
public CommonResult create(@RequestBody Payment payment){
int i = paymentService.create(payment);
log.info("result = {}", i);
if (i > 0){
return new CommonResult(200, "插入数据成功 + " + serverPort, i);
}else {
return new CommonResult(444, "插入数据失败", null);
}
}
@GetMapping("/payment/get/{id}")
public CommonResult getPaymentById(@PathVariable("id")Long id){
Payment result = paymentService.getPaymentById(id);
log.info("result = {}", result);
if (result != null){
return new CommonResult(200, "查询成功数据, serverPort : " + serverPort, result);
}else {
return new CommonResult(444, "查询数据失败" + id, null);
}
}
@GetMapping("/payment/discovery")
public Object discovery(){
List<String> services = discoveryClient.getServices();
for (String service : services) {
log.info("service : " + service);
}
List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
for (ServiceInstance instance : instances) {
log.info("instance : " + instance);
}
return discoveryClient;
}
}
2.4 Feign-Order微服务
service
@Component
@FeignClient(name = "CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService {
@GetMapping("/payment/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id);
}
controller
@RestController
@Slf4j
public class OrderFeignController {
@Autowired
private PaymentFeignService paymentFeignService;
@GetMapping("/consumer/payment/get/{id}")
public CommonResult<Payment> get(@PathVariable("id") Long id){
return paymentFeignService.getPaymentById(id);
}
}
3.结果展示
http://localhost:7001/
Feign-Order80微服务调用Provider8001微服务得到结果