Spring-Cloud学习笔记(二)RestTemplate和Feign的使用方式以及部分注解的使用
2018-07-23 18:50
856 查看
通常,我们在搭建springcloud微服务架构时,会把我们的服务注册到高可用eureka服务注册发现中心上面,各个服务之间的通信就是无可避免的,此时我们就需要用到RestTemplate 或者Feign去进行各个服务之间的通信调用。
RestTemplate的三种使用方式
1、直接只用RestTemplate 方式调用,url写死。
[code] String msg =restTemplate.getForObject("http://localhost:8080/server/msg",String.class);
2、利用 loadBalancerClient 通过应用名(spring.application.name或者在eureka注册中心页面可以看到此名称)获取 url,然后在使用restTemplate
[code] @Autowired private LoadBalancerClient loadBalancerClient; String url = String.format("http://%s:%s/",serviceInstance.getHost(),serviceInstance.getPort())+"server/msg"; msg = restTemplate.getForObject(url,String.class); log.info("获取的msg为{}",msg);
3、方式三(利用@LoadBalanced注解,可在restTemplate中使用应用名字的方式去调用url)
[code]@Component public class RestTemplateConfig { @Bean @LoadBalanced public RestTemplate restTemplate(){ return new RestTemplate(); } } 或者 @Autowired @LoadBalanced private RestTemplate restTemplate; String msg = restTemplate.getForObject("http://PRODUCT/server/msg",String.class);
spring-cloud 中的负载均衡器rabbion,默认是轮询机制,可以通过以下方式更改为随机负载均衡机制。
[code]PRODUCT: #应用的Application名称 ribbon: NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
Feign的使用方式
添加依赖
[code]<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> <version>1.4.5.RELEASE</version> </dependency>
在启动类上面添加@EnableFeignClients注解
[code]@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients(basePackages = "com.example.order.client") #指定远程调用的服务的包名 public class OrderApplication { public static void main(String[] args) { SpringApplication.run(OrderApplication.class, args); } }
编写代码
[code]package com.example.order.client; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; /** * 指定应用名称 */ @FeignClient(name = "product") public interface ProductClient { @GetMapping("/server/msg") String getMsg(); }
调用服务
[code] @Autowired private ProductClient productClient; msg = productClient.getMsg(); return msg;
Feign采用声明式REST客户端(伪rpc)采用了基于接口的注解
,本质还是http客户端,通过feign能够对远程调用对于开发者完全透明,得到与调用本地方法一致的编码体验。
阅读更多相关文章推荐
- Spring学习笔记1——IOC: 尽量使用注解以及java代码(转)
- Spring学习笔记之RestTemplate使用小结
- spring学习笔记(12)——使用注解方式配置bean
- ITCAST视频-Spring学习笔记(使用Spring的注解方式实现AOP入门)
- ITCAST视频-Spring学习笔记(使用Spring的注解方式实现AOP的细节)
- Spring学习笔记(17)----使用Spring注解方式管理事务
- Spring学习笔记1——IOC: 尽量使用注解以及java代码
- SpringCloud 查找调用REST服务使用RestTemplate(ribbon负载)或feign模式 教程源码 火推
- Spring学习笔记1之IOC详解尽量使用注解以及java代码
- spring学习笔记7--使用spring进行面向切面的(AOP)编程(1)注解方式实现
- 玩转SpringCloud(F版本) 三.断路器(Hystrix)RestTemplate+Ribbon和Feign两种方式
- Spring学习笔记(14)----使用Spring的注解方式实现AOP
- Spring学习笔记(15)----使用Spring注解方式管理事务
- Spring学习笔记 关于Bean定义的重用-使用abstract以及parent标签属性
- 【Spring学习笔记】五、使用JDBCTemplate以对象方式操作数据库
- Spring 注解方式进行事务管理的用法介绍详解【Java学习笔记】
- Spring学习之使用标签来标记资源(@Component、@Repository、 @Service和@Controller)以及使用方式(包含如何在jsp中使用)
- Spring4学习笔记-AOP(基于注解的方式)
- spring aop的使用(注解方式以及基于xml配置方式)
- 学习《Spring实战(第3版)》扎记1:Spring使用注解配置Bean的方式