动力节点首页 全国咨询热线:400-8080-105

绑定手机号,登录
手机号

验证码

微信登录
手机号登录
手机号

验证码

微信登录与注册
微信扫码登录与注册

扫码关注微信公众号完成登录与注册
手机号登录
首页 > 文章

SpringCloud服务调用的两种方式

07-30 09:40 549浏览
举报 T字号
  • 大字
  • 中字
  • 小字

spring-cloud调用服务有两种方式,一种是Ribbon+RestTemplate, 另外一种是Feign。

Ribbon是一个基于HTTP和TCP客户端的负载均衡器,其实feign也使用了ribbon, 只要使用@FeignClient时,ribbon就会自动使用。

Ribbon

1.新建模块client-a

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-cloud</artifactId>
        <groupId>com.feng</groupId>
        <version>0.0.1</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>client-a</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
        </dependency>
    </dependencies>
</project>

新建bootstrap.yml

server:
  port: 8910
eureka:
  client:
    serviceUrl:
          defaultZone: http://localhost:8010/eureka/
spring:
  application:
      name: client-a

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

/**
 * @author fengzp
 * @date 17/5/9
 * @email fengzp@gzyitop.com
 * @company 广州易站通计算机科技有限公司
 */
@SpringBootApplication
@EnableDiscoveryClient
public class ClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ClientApplication.class, args);
    }
    @Bean
    @LoadBalanced
    RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

测试用的controller

/**
 * @author fengzp
 * @date 17/5/9
 * @email fengzp@gzyitop.com
 * @company 广州易站通计算机科技有限公司
 */
@RestController
public class TestController {
    @Autowired
    RestTemplate restTemplate;
    @RequestMapping("/hi")
    public String hi(@RequestParam String id){
        return restTemplate.getForObject("http://service-a/hi?id="+id, String.class);
    }
}

2.为了测试负载功能,这里要再新建一个模块service-b, 和上一篇的service-a的代码基本相同,只把端口修改了就可以。

把client-a和service-b都启动成功后,打开eureka中心应该看到:

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

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

然后刷新页面

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

feign

1.新建模块client-b

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-cloud</artifactId>
        <groupId>com.feng</groupId>
        <version>0.0.1</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>client-b</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
    </dependencies>
</project>

bootstrap.yml

server:
  port: 8911
eureka:
  client:
    serviceUrl:
          defaultZone: http://localhost:8010/eureka/
spring:
  application:
      name: client-b

ClientApplication, 使用@EnableFeignClients开启feiginClient功能

/**
 * @author fengzp
 * @date 17/5/9
 * @email fengzp@gzyitop.com
 * @company 广州易站通计算机科技有限公司
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ClientApplication.class, args);
    }

}

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

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

Controller

@RestController
public class TestController {
    @Autowired
    ServiceAFeignClient serviceAFeignClient;
    @RequestMapping("/hi")
    public String hi(@RequestParam String id){
        return serviceAFeignClient.hi(id);
    }
}

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

动力节点在线课程涵盖零基础入门,高级进阶,在职提升三大主力内容,覆盖Java从入门到就业提升的全体系学习内容。全部Java视频教程免费观看,相关学习资料免费下载!对于火爆技术,每周一定时更新!如果想了解更多相关技术,可以到动力节点在线免费观看SpringCloud入门视频教程哦!

0人推荐
共同学习,写下你的评论
0条评论
代码小兵124
程序员代码小兵124

99篇文章贡献336699字

作者相关文章更多>

推荐相关文章更多>

重启Docker容器命令

代码小兵87208-09 15:04

SpringBoot是什么框架

代码小兵99203-29 17:29

简述SpringBoot与Spring区别

杨晶珍08-02 11:09

使用Docker搭建开发环境

代码小兵98808-09 14:38

Docker集群详解

代码小兵28608-09 14:50

发评论

举报

0/150

取消