Spring Cloud+Dubbo对Feign进行RPC改造

因为Spring Cloud Feign是基于Http Restful的调用,在高并发下的性能不够理想(虽然他是基于Ribbon以及带有熔断机制,可以防止雪崩),成为性能瓶颈,所以我们今天对Feign进行Dubbo的RPC改造。

我们Spring Cloud的项目结构如下

8ac5ef9af73d1af5795d71d10f23d178fdf.jpg

其中user-center是我们的用户中心,game-center是我们的游戏中心,以游戏中心调用用户中心的Feign如下

@Component
@FeignClient("user-center")
public interface UserClient {

    @PutMapping("/api-u/users-anon/internal/updateAppUser")
    AppUser updateUser(@RequestBody AppUser appUser);

    @PostMapping("/api-u/users-anon/internal/users/updateUserBanlance")
    String updateUserBanlance(@RequestParam("id") long id, @RequestParam("banlance") BigDecimal banlance);
}

我们先来改造用户中心作为Dubbo的提供者,pom添加Dubbo的引用

<dependency>
   <groupId>com.alibaba.spring.boot</groupId>
   <artifactId>dubbo-spring-boot-starter</artifactId>
   <version>2.0.0</version>
</dependency>
<dependency>
   <groupId>com.github.sgroschupf</groupId>
   <artifactId>zkclient</artifactId>
   <version>0.1</version>
</dependency>

将service接口放入公共模块api-model

public interface AppUserService {
   void addTestUser(AppUser user);

   void addAppUser(AppUser appUser);

   void updateAppUser(AppUser appUser);

   LoginAppUser findByUsername(String username);

   AppUser findById(Long id);

   void setRoleToUser(Long id, Set<Long> roleIds);

   void updatePassword(Long id, String oldPassword, String newPassword);

   void updateWithdrawal(Long id, String oldPassword, String newPassword);

   Page<AppUser> findUsers(Map<String, Object> params);

   Set<SysRole> findRolesByUserId(Long userId);

   void bindingPhone(Long userId, String phone);

   int updateUserBanlance(long id, BigDecimal banlance);

   Map<String, Object> findUserMapById(long userId);

   Page<Map<String, Object>> findUsers(String username, BigDecimal minBanlance, BigDecimal maxBanlance, String startTime, String endTime, Integer groupId, Integer control, int pageNo, int pageSize);

   void deleteTestUser(Assist assist);
}

用户中心资源配置,添加dubbo配置

spring:
  application:
    name: user-center
  cloud:
    config:
      discovery:
        enabled: true
        serviceId: config-center
      profile: dev
  dubbo:
    application:
      id: user-center-dubbo-prodiver
      name: user-center-dubbo-prodiver
    registry:
      address: zookeeper://192.168.5.129:2181
    server: true
    protocol:
      name: dubbo
      port: 20880

接口实现类的标签修改

@Slf4j
@Service(interfaceClass = AppUserService.class)
@Component
public class AppUserServiceImpl implements AppUserService {

其中这个@Service已经不再是spring的标签,而需要使用,

import com.alibaba.dubbo.config.annotation.Service;

的Dubbo标签

之前的spring @Service改用@Component

在Springboot的主类添加Dubbo的配置标签

@EnableDubboConfiguration
@EnableScheduling
@EnableSwagger2
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class UserCenterApplication {

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

}

此时启动用户中心项目,可以在Dubbo主控台中看到

8b0bce42ee39690cb24965553ec9208d330.jpg

点进去可以看到提供者注册信息

669d6eb73c0cd0bb337f5147e2af4ce9d20.jpg

然后再来看看游戏中心的消费者

pom依赖跟用户中心一样

资源文件配置添加Dubbo配置

spring:
  application:
    name: game-center
  cloud:
    config:
      discovery:
        enabled: true
        serviceId: config-center
      profile: dev
  dubbo:
    application:
      name: game-center-dubbo-consumer
      id: game-center-dubbo-consumer
    protocol:
      port: 20800
      name: dubbo
    registry:
      address: zookeeper://192.168.5.129:2181

在使用的Controller中注释掉之前的feign注入,使用Dubbo的接口

//    @Autowired
//    private UserClient userClient;
@Reference
private AppUserService appUserService;

因为该接口在公共模块api-model中,所以任何模块都可以识别的到,此时需要使用Dubbo的注释

import com.alibaba.dubbo.config.annotation.Reference;

在Springboot主类中添加Dubbo注释

@EnableDubboConfiguration
@EnableScheduling
@EnableSwagger2
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class GameCenterApplication {

   public static void main(String[] args) {
      ApplicationContext context = SpringApplication.run(GameCenterApplication.class, args);
      SpringBootUtil.setApplicationContext(context);
   }
}

启动游戏中心项目,在Dubbo控制台中的消费者中,我们可以看到

5f55162b28bbff3f3c7186f4ef25c95133d.jpg

点进去可以看到消费者注册信息

51020fbf37a6fff168eb7cef0a50977dabb.jpg

这样我们在实际使用中,将之前的feign代码改成直接使用该service接口就可以通过RPC的远程调用了

//调用userService更新用户信息 TODO
//        userClient.updateUser(user);
        appUserService.addAppUser(user);

最后就是进行压测,性能要绝对优于Feign调用的吞吐量。

另外Springboot Dubbo的调试直连提供者,只连接固定IP的提供者,绕过注册中心,不进行负载均衡

@Reference(url = "dubbo://192.168.5.33:20880")
private AppUserService appUserService;

此方法只能在测试时使用,生产环境中必须去掉。

消息只订阅

dubbo:
  application:
    id: user-center-dubbo-prodiver
    name: user-center-dubbo-prodiver
  registry:
    address: zookeeper://192.168.5.129:2188
    register: false
  server: true
  protocol:
    name: dubbo
    port: 20880

这么写,注册中心注册不到你,其他消费者调用不到你这个服务,只有像上面直连提供者那种才可以调用的到。

这个也只是在测试环境里面使用,生产环境必须去掉 register: false 

转载于:https://my.oschina.net/u/3768341/blog/2395878

Spring Cloud是一组用于构建分布式系统的开源框架,它为我们提供了一种轻量级的微服务架构的解决方案。而Dubbo是阿里巴巴开源的高性能RPC框架,用于构建分布式服务框架。而FeignSpring Cloud提供的一种声明式的Web服务客户端,它可以与Dubbo进行集成,提供更加便捷的服务调用方式。 在集成DubboFeign之前,我们需要先将Dubbo注册到Spring Cloud的注册中心,使得Dubbo服务可以被Spring Cloud所管理。这样,我们就可以使用Feign作为Spring Cloud中的服务消费者来调用Dubbo提供的服务。通过Feign,我们可以避免编写繁琐的服务调用代码,只需要简单的声明式接口,并使用注解来定义具体的服务调用方法,Feign会动态生成代理实现类,自动处理服务调用的细节。 在使用Feign集成Dubbo时,我们需要在Spring Boot配置文件中进行相应的配置,声明需要调用的Dubbo服务接口。然后,通过在代码中定义Feign的接口,使用注解来标识调用的Dubbo服务地址、服务方法等。当我们调用该接口时,Feign会自动发起对Dubbo服务的远程调用。 通过将FeignDubbo集成,我们可以通过Spring Cloud的整体架构来统一管理和调用Dubbo的服务,简化了代码编写和服务调用的过程。同时,Feign还具有负载均衡、容错等一系列特性,可以进一步提高系统的稳定性和可靠性。 综上所述,通过集成DubboFeign,我们可以更加方便地在Spring Cloud中调用和管理Dubbo的服务,提高系统的可维护性和可扩展性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值