首页 文章 精选 留言 我的

精选列表

搜索[伪集群],共10007篇文章
优秀的个人博客,低调大师

Spring Cloud实战小贴士:Feign的继承特性(RPC模式)

通过之前发布的《Spring Cloud构建微服务架构:服务消费者(Feign)》,我们已经学会如何使用Spring MVC的注解来绑定服务接口。我们几乎完全可以从服务提供方的Controller中依靠复制操作,来构建出相应的服务接口客户端,或是通过Swagger生成的API文档来编写出客户端,亦或是通过Swagger的代码生成器来生成客户端绑定。即便如此,有很多的方式来产生Feign的客户端程序,依然有很多开发者热衷于利用公共的依赖接口来连接服务提供者和服务消费者的方式。由此,Feign的继承特性就能很好的派上用处。下面,我们来详细看看如何使用Spring Cloud Feign的继承特性。 动手试一试 接下来的示例将分为三个模块: 服务接口定义模块:通过Spring MVC注解定义抽象的interface服务接口 服务接口实现模块:实现服务接口定义模块的interface,该模块作为服务提供者注册到eureka 服务接口消费模块:服务接口定义模块的客户端实现,该模块通过注册到eureka来消费服务接口 服务接口的定义 创建一个Spring Boot项目:eureka-feign-api,pom.xml的主要内容如下: <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR2</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> 使用Spring MVC注解来定义服务接口: public interface HelloService { @GetMapping("/hello") String hello(@RequestParam(value = "name") String name); } 完成了上述构建之后,我们使用mvn install将该模块构建到本地的Maven仓库中。 服务接口的实现 创建一个Spring Boot项目:eureka-feign-client,pom.xml的主要内容如下: <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>com.didispace</groupId> <artifactId>eureka-feign-api</artifactId> <version>1.0.0</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR2</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> 该模块需要依赖上面定义的eureka-feign-api,将使用上述定义的HelloService接口来实现对应的REST服务。同时依赖Eureka是为了将该服务注册到Eureka上供服务消费者发现。 创建应用主类。使用@EnableDiscoveryClient注解开启服务注册与发现,并实现HelloService接口的REST服务: @EnableDiscoveryClient @SpringBootApplication public class Application { @RestController class HelloController implements HelloService { @Override public String hello(String name) { return "hello " + name; } } public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); } } 编辑application.properties配置内容: spring.application.name=eureka-feign-client server.port=2101 eureka.client.serviceUrl.defaultZone=http://eureka.didispace.com/eureka/ 配置了服务提供者的名称eureka-feign-client,服务提供者的端口号2101,并将该服务注册到我的公益Eureka注册中心上。启动该项目,我们可以通过访问:http://eureka.didispace.com/ ,在该页面中找到它。 服务接口的消费 创建一个Spring Boot项目:eureka-feign-consumer,pom.xml的主要内容如下: <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <dependency> <groupId>com.didispace</groupId> <artifactId>eureka-feign-api</artifactId> <version>1.0.0</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR2</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> 该模块较服务提供者的依赖增加了Feign的依赖,因为这里将使用Feign来绑定服务接口的客户端。下面我们将使用Feign的继承特性来轻松的构建Feign客户端。 创建应用主类。使用@EnableDiscoveryClient注解开启服务注册与发现,并通过@FeignClient注解来声明服务绑定客户端: @EnableFeignClients @EnableDiscoveryClient @SpringBootApplication public class Application { @FeignClient("eureka-feign-client") interface HelloServiceClient extends HelloService { } @RestController class TestController { @Autowired private HelloServiceClient helloServiceClient; @GetMapping("/test") public String test(String name) { return helloServiceClient.hello(name); } } public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); } } 从上述代码中我们可以看到,利用Feign的继承特性,@FeignClient注解只需要通过声明一个接口来继承在API模块中定义的公共interface就能产生服务接口的Feign客户端了。而@FeignClient中的值需要填写该服务的具体服务名(服务提供者的spring.application.name配置值)。 编辑服务消费者的application.properties配置内容,将服务消费者注册到eureka上来消费服务: spring.application.name=eureka-feign-consumer server.port=2102 eureka.client.serviceUrl.defaultZone=http://eureka.didispace.com/eureka/ 启动eureka-feign-consumer之后,我们可以通过访问:http://localhost:2102/test ,来实验eureka-feign-consumer对eureka-feign-client接口的调用。 本文示例 码云 GitHub 程序清单: eureka-feign-api:服务接口定义 eureka-feign-client:服务接口实现的提供方 eureka-feign-consumer:服务接口的调用方 欢迎使用公益Eureka调试您的Spring Cloud程序:http://eureka.didispace.com/ 相关阅读 Spring Cloud构建微服务架构:服务注册与发现(Eureka、Consul) Spring Cloud构建微服务架构:服务消费者(基础) Spring Cloud构建微服务架构:服务消费者(Ribbon) Spring Cloud构建微服务架构:服务消费者(Feign) Spring Cloud构建微服务架构:分布式配置中心 Spring Cloud构建微服务架构:服务容错保护(hystrix服务降级) Spring Cloud构建微服务架构:服务容错保护(hystrix依赖隔离) Spring Cloud构建微服务架构:服务容错保护(hystrix断路器) Spring Cloud构建微服务架构:Hystrix监控面板 Spring Cloud构建微服务架构:Hystrix监控数据聚合 更多Spring Cloud内容…

优秀的个人博客,低调大师

Android应用BeNews的攻击机制是怎样的?

Hacking Team数据泄露事故表明,伪装Android应用可通过使用动态加载技术绕过Google Play Store中的过滤功能。这种攻击的工作原理是什么,用户和企业可如何检测这些恶意应用? Michael Cobb:备受争议的IT安全公司Hacking Team沦为网络攻击的受害者,攻击者宣布他们正在通过Hacking Team自己的Twitter账号将其客户文件、合同、财务资料和内部邮件提供公众下载。(该公司专门向政府、执法机构和企业销售“攻击性”入侵和监控工具及服务,这400GB泄露数据向我们揭露了政府和企业监控及间谍活动的精彩世界。) 在这些泄露的文件中还包含文件解释如何使用Hacking Team的软件,以及某些应用的源代码。安全软件公司Trend Micro研究人员在这些数据中发现了恶意Android应用样本,该应用伪装成新闻app,并使用BeNews作为名称(这是现已解散的新闻网站的名称),这个app看似合法,并且,该应用不包含漏洞利用代码,在安装时只要求三个权限。这种无害的伪装让它可通过Google Play的审批过程。然而,在安装该应用后,它会使用动态加载技术执行额外的代码。 动态加载让应用只加载需要的组件,在某些相关组件不总是需要时,这种被用来减小可执行文件的大小以及提高性能。在这个假Android应用的情况下,这种技术被用来延迟恶意代码的加载,直至app通过审核以及被安装。它安装Hacking Team的RCSAndroid监控程序,这被安全专家认为是最复杂的Android恶意软件。它可捕捉屏幕截图、监控剪贴板中的内容、收集密码、联系人和信息,并可使用手机的麦克风录音。该app利用了特权升级漏洞—CVE-2014-3153,这是Android 2.2到4.4.4中存在的漏洞,用以绕过设备安全以及允许远程攻击者访问。 在从Google Play移除之前,这个假的BeNews应用被下载多达50次,现在这个应用以及Hacking Team的其他软件的源代码已经公开,网络罪犯肯定会利用它来添加新的或改进的功能到他们自己的攻击工具。不过,从好的方面来看,泄露的数据包含大量信息可供安全研究人员用于调查从未被披露或修复的其他漏洞。Hacking Team也建议可使用沙箱技术来阻止攻击者利用漏洞攻击设备。希望这将鼓励供应商开发更好的工具以及更多地利用这种缓解技术。 企业应该通过安全消息推送随时了解这个快速变化的移动安全环境,并确保联网的移动设备保持更新和修复。现在有很多移动保护套件可提供额外的保护以防止恶意应用绕过app store和OS安全措施,这包括Trend Micro的Mobile Security for Android、ESET Mobile Security for Android以及McAfee Mobile Security,这些都是企业版本。 作者:Michael Cobb 来源:51CTO

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

用户登录
用户注册