spring-cloud调用服务有两种方式,一种是Ribbon+RestTemplate, 另外一种是Feign。
Ribbon是一个基于HTTP和TCP客户端的负载均衡器,其实feign也使用了ribbon, 只要使用@FeignClient时,ribbon就会自动使用。
1.1
新建模块client-a
pom文件
<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-cloudartifactId>
<groupId>com.fenggroupId>
<version>0.0.1version>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>client-aartifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-ribbonartifactId>
dependency>
dependencies>
project>
新建bootstrap.yml
server:
port: 8910
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8010/eureka/
spring:
application:
name: client-a
ClientApplication, 这里我们需要注册一个RestTemplate,并且使用@LoadBalanced开启负载功能
@SpringBootApplication
@EnableDiscoveryClient
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
}
测试用的controller
@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);
}
}
1.2
为了测试负载功能,这里要再新建一个模块service-b, 和上一篇的service-a的代码基本相同,只把端口修改了就可以。
把client-a和service-b都启动成功后,打开eureka中心应该看到:
1.3
打开http://localhost:8910/hi?id=123
可以看到服务已经成功调用。
2.1
新建模块client-b
pom文件
<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-cloudartifactId>
<groupId>com.fenggroupId>
<version>0.0.1version>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>client-bartifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-feignartifactId>
dependency>
dependencies>
project>
bootstrap.yml
server:
port: 8911
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8010/eureka/
spring:
application:
name: client-b
ClientApplication, 使用@EnableFeignClients开启feiginClient功能
@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.2
运行后的结果应该是和ribbon的相同。
个人感觉使用feign比较舒服,代码比较简洁。