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

我们Spring Cloud的项目结构如下

其中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主控台中看到

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

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

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控制台中的消费者中,我们可以看到

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

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

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

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

转载至链接:https://my.oschina.net/u/3768341/blog/2395878。

Spring Cloud+Dubbo对Feign进行RPC改造的更多相关文章

  1. Spring Cloud(Dalston.SR5)--Feign 声明式REST客户端

    Spring Cloud 对 Feign 进行了封装,集成了 Ribbon 并结合 Eureka 可以实现客户端的负载均衡,Spring Cloud 实现的 Feign 客户端类名为 LoadBala ...

  2. Spring Cloud 入门 之 Feign 篇(三)

    原文地址:Spring Cloud 入门 之 Feign 篇(三) 博客地址:http://www.extlight.com 一.前言 在上一篇文章<Spring Cloud 入门 之 Ribb ...

  3. 0.9.0.RELEASE版本的spring cloud alibaba sentinel+feign降级处理实例

    既然用到了feign,那么主要是针对服务消费方的降级处理.我们基于0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例添油加醋,把sentinel功能加 ...

  4. spring cloud & dubbo

    区别 来源(背景): Dubbo,是阿里巴巴服务化治理的核心框架,并被广泛应用于阿里巴巴集团的各成员站点. Spring Cloud,从命名我们就可以知道,它是Spring Source的产物,Spr ...

  5. Spring Cloud Alibaba(8)---Feign服务调用

    Feign服务调用 有关Spring Cloud Alibaba之前写过五篇文章,这篇也是在上面项目的基础上进行开发. Spring Cloud Alibaba(1)---入门篇 Spring Clo ...

  6. Spring Cloud中关于Feign的常见问题总结

    一.FeignClient接口,不能使用@GettingMapping 之类的组合注解 代码示例: @FeignClient("microservice-provider-user" ...

  7. Spring Cloud系列之Feign的常见问题总结

    一.FeignClient接口,不能使用@GettingMapping 之类的组合注解 代码示例: @FeignClient("microservice-provider-user" ...

  8. spring cloud ribbon和feign的区别

    spring cloud的Netflix中提供了两个组件实现软负载均衡调用:ribbon和feign. Ribbon 是一个基于 HTTP 和 TCP 客户端的负载均衡器 它可以在客户端配置 ribb ...

  9. spring cloud 学习(3) - feign入门

    feign 是一个让rest服务调用更简洁的开源项目,很多介绍文章或书也称它为声明式REST调用.传统的web service中,通过引用wsdl来自动生成一些client的代理类(或stub代码), ...

随机推荐

  1. CNVD-C-2019-48814 漏洞

    CNVD-C-2019-48814 WebLogic wls9-async反序列化远程命令执行漏洞 网上均有详细的说明(https://github.com/jas502n/CNVD-C-2019-4 ...

  2. [java,2019-01-25] 图片和二进制互转

    import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOu ...

  3. DLC 基本逻辑运算

    逻辑代数:分析设计数字电路的数学基础是逻辑代数 变量的取值只能是 0 1 逻辑代数中只有三种基本逻辑运算,即 与 或 非 与逻辑运算: 只有决定一件事情的全部条件都具备时,这事件才成立.这样的因果关系 ...

  4. Tomcat版本对照表

    导入程序前环境要先配好,环境要想配的正确,版本信息一定要了解. Tomcat版本   6.0 7.0 8.0 8.5 9.0 JDK ≥5.0   ≥6.0       ≥7.0     ≥7.0   ...

  5. 一些常见的Java面试题 & 面试感悟

    < 前言 > 近期在面试,深感这个行业的浮躁,一些菜不辣基的弱鸡开出的工资待遇要求,超过了我.不知道他们是怎么拿到那么高的工资的,难道是他在公司有亲戚朋友吗?有后台吗?是行业热钱真的过多了 ...

  6. python 编码规范 (http://www.runoob.com/w3cnote/google-python-styleguide.html)

    命名约定 所谓"内部(Internal)"表示仅模块内可用, 或者, 在类内是保护或私有的. 用单下划线(_)开头表示模块变量或函数是protected的(使用import * f ...

  7. [C++]数组指针,数组引用,函数指针

    数组指针是指一个指向数组的指针,例如有一个数组指针p指向一个数组a[],则 *p是取到这个数组,也就是说 *p=a,因此 **p =a[0], 它的定义为: ]; ]=&a; (*c)表示它是 ...

  8. mezzanine的breadcrumb

    page_menu tag为什么能渲染出当前页的面包屑路径??靠的是request的page对象,要是page的自己或是它的的祖先才渲染,是根据page的属性page.is_current_or_as ...

  9. Java小白不走弯路学习Java流程以及学习误区

    学习Java编程技术肯定是以就业拿到高薪工作为主要目的的,可是高薪不会那么轻易拿到,这是一个最简单的道理.没有付出就没有回报,在整个学习Java编程技术的过程中,你需要付出时间.精力.金钱.废话不多说 ...

  10. 深度学习原理与框架- tf.nn.atrous_conv2d(空洞卷积) 问题:空洞卷积增加了卷积核的维度,为什么不直接使用7*7呢

    空洞卷积, 从图中可以看出,对于一个3*3的卷积,可以通过使用增加卷积的空洞的个数,来获得较大的感受眼, 从第一幅图中可以看出3*3的卷积,可以通过补零的方式,变成7*7的感受眼,这里补零的个数为1, ...