springcloud 服务调用的两种方式

spring-cloud调用服务有两种方式,一种是Ribbon+RestTemplate, 另外一种是Feign。
Ribbon是一个基于HTTP和TCP客户端的负载均衡器,其实feign也使用了ribbon, 只要使用@FeignClient时,ribbon就会自动使用。

一、Ribbon

1.1
新建模块client-a
pom文件


 
 
  1. <?xml version= "1.0" encoding= "UTF-8"?>
  2. <project xmlns= "http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>spring-cloud</artifactId>
  7. <groupId>com.feng</groupId>
  8. <version> 0.0.1</version>
  9. </parent>
  10. <modelVersion> 4.0.0</modelVersion>
  11. <artifactId>client-a</artifactId>
  12. <dependencies>
  13. <dependency>
  14. <groupId>org.springframework.cloud</groupId>
  15. <artifactId>spring-cloud-starter-ribbon</artifactId>
  16. </dependency>
  17. </dependencies>
  18. </project>

 

新建application.yml


 
 
  1. server:
  2. port: 8910
  3. eureka:
  4. client:
  5. serviceUrl:
  6. defaultZone: http: //localhost:8010/eureka/
  7. spring:
  8. application:
  9. name: client-a

 

ClientApplication, 这里我们需要注册一个RestTemplate,并且使用@LoadBalanced开启负载功能


 
 
  1. @SpringBootApplication
  2. @EnableDiscoveryClient
  3. public class ClientApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(ClientApplication.class, args);
  6. }
  7. @Bean @LoadBalanced RestTemplate restTemplate(){
  8. return new RestTemplate();
  9. }
  10. }

 

测试controller


 
 
  1. @RestController
  2. public class TestController {
  3. @Autowired
  4. RestTemplate restTemplate;
  5. @RequestMapping( "/hi")
  6. public String hi(@RequestParam String id){
  7. return restTemplate.getForObject( "http://service-a/hi?id="+id, String.class);
  8. }
  9. }

复制代码

1.2
为了测试负载功能,这里要再新建一个模块service-b, 和上一篇的service-a的代码基本相同,只把端口修改了就可以。
把client-a和service-b都启动成功后,打开eureka中心应该看到:

1.3
打开http://localhost:8910/hi?id=123

可以看到服务已经成功调用。

然后刷新页面

看到端口已经改变,说明负载功能成功实现

二、feign

2.1
新建模块client-b
pom文件


 
 
  1. <?xml version= "1.0" encoding= "UTF-8"?>
  2. <project xmlns= "http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>spring-cloud</artifactId>
  7. <groupId>com.feng</groupId>
  8. <version> 0.0.1</version>
  9. </parent>
  10. <modelVersion> 4.0.0</modelVersion>
  11. <artifactId>client-b</artifactId>
  12. <dependencies>
  13. <dependency>
  14. <groupId>org.springframework.cloud</groupId>
  15. <artifactId>spring-cloud-starter-feign</artifactId>
  16. </dependency>
  17. </dependencies>
  18. </project>

 

bootstrap.yml


 
 
  1. server:
  2. port: 8911
  3. eureka:
  4. client:
  5. serviceUrl:
  6. defaultZone: http: //localhost:8010/eureka/
  7. spring:
  8. application:
  9. name: client-b

 

ClientApplication, 使用@EnableFeignClients开启feiginClient功能


 
 
  1. @SpringBootApplication
  2. @EnableDiscoveryClient
  3. @EnableFeignClients
  4. public class ClientApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(ClientApplication.class, args);
  7. }
  8. }

 

 

这里新建一个ServiceAFeignClient来调用service-a服务

 


 
 
  1. @Component
  2. @FeignClient(value = "service-a") //这里的name对应调用服务的spring.applicatoin.name
  3. public interface ServiceAFeignClient {
  4. @RequestMapping(value = "/hi")
  5. String hi(@RequestParam("id") String id);
  6. }
Controller

 
 
  1. @RestController
  2. public class TestController {
  3. @Autowired
  4. ServiceAFeignClient serviceAFeignClient;
  5. @RequestMapping( "/hi")
  6. public String hi(@RequestParam String id){
  7. return serviceAFeignClient.hi(id);
  8. }
  9. }

 

 

运行后的结果应该是和ribbon的相同。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值