springcloud服务调⽤的两种⽅式
spring-cloud调⽤服务有两种⽅式,⼀种是Ribbon+RestTemplate, 另外⼀种是Feign。
Ribbon是⼀个基于HTTP和TCP客户端的负载均衡器,其实feign也使⽤了ribbon, ,ribbon就会⾃动使⽤。
⼀、Ribbon
1.1
新建模块client-a
pom⽂件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /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>
新建l
server:
port: 8910
eureka:
client:
serviceUrl:
defaultZone: localhost:8010/eureka/
spring:
application:
name: client-a
ClientApplication, 这⾥我们需要注册⼀个RestTemplate,
/**
* @author fengzp
* @date 17/5/9
* @email fengzp@gzyitop
* @company ⼴州易站通计算机科技有限公司
*/
@SpringBootApplication
@EnableDiscoveryClient
public class ClientApplication {
</span><span >public</span> <span >static</span> <span >void</span><span > main(String[] args) {
SpringApplication.run(ClientApplication.</span><span >class</span><span >, args);
}
springcloud和springboot@Bean
@LoadBalanced
RestTemplate restTemplate(){
</span><span >return</span> <span >new</span><span > RestTemplate();
}
}
测试⽤的controller
/**
* @author fengzp
* @date 17/5/9
* @email fengzp@gzyitop
* @company ⼴州易站通计算机科技有限公司
*/
@RestController
public class TestController {
@Autowired
RestTemplate restTemplate;
@RequestMapping(</span><span >"</span><span >/hi</span><span >"</span><span >)
</span><span >public</span><span > String hi(@RequestParam String id){
</span><span >return</span> ForObject(<span >"</span><span >service-a/hi?id=</span><span >"</span>+id, String.<span }
}
1.2
为了测试负载功能,这⾥要再新建⼀个模块service-b, 和上⼀篇的service-a的代码基本相同,只把端⼝修改了就可以。
把client-a和service-b都启动成功后,打开eureka中⼼应该看到:
可以看到服务已经成功调⽤。
然后刷新页⾯
看到端⼝已经改变,说明负载功能成功实现
⼆、feign
2.1
新建模块client-b
pom⽂件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /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>
server:
port: 8911
eureka:
client:
serviceUrl:
defaultZone: localhost:8010/eureka/
spring:
application:
name: client-b
ClientApplication,
/**
* @author fengzp
* @date 17/5/9
* @email fengzp@gzyitop
* @company ⼴州易站通计算机科技有限公司
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ClientApplication {
</span><span >public</span> <span >static</span> <span >void</span><span > main(String[] args) {
SpringApplication.run(ClientApplication.</span><span >class</span><span >, args);
}
}
这⾥新建⼀个ServiceAFeignClient来调⽤service-a服务
@Component
@FeignClient(value = "service-a") //这⾥的name对应调⽤服务的spring.applicatoin.name
public interface ServiceAFeignClient {
@RequestMapping(value </span>= <span >"</span><span >/hi</span><span >"</span><span >)
String hi(@RequestParam(</span><span >"</span><span >id</span><span >"</span><span >) String id);
}
Controller
@RestController
public class TestController {
@Autowired
ServiceAFeignClient serviceAFeignClient;
@RequestMapping(</span><span >"</span><span >/hi</span><span >"</span><span >)
</span><span >public</span><span > String hi(@RequestParam String id){
</span><span >return</span><span > serviceAFeignClient.hi(id);
}
}
2.2
运⾏后的结果应该是和ribbon的相同。
个⼈感觉使⽤feign⽐较舒服,代码⽐较简洁。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论