spring-cloud調用服務有兩種方式,一種是Ribbon+RestTemplate, 另一種是Feign。
Ribbon是一個基於HTTP和TCP客戶端的負載均衡器,其實feign也使用了ribbon, 只要使用@FeignClient時,ribbon就會自動使用。spring
1.1
新建模塊client-a
pom文件apache
<?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.ymlbootstrap
server: port: 8910 eureka: client: serviceUrl: defaultZone: http://localhost:8010/eureka/ spring: application: name: client-a
ClientApplication, 這裏咱們須要註冊一個RestTemplate,而且使用@LoadBalanced開啓負載功能app
/** * @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); } }
1.2
爲了測試負載功能,這裏要再新建一個模塊service-b, 和上一篇的service-a的代碼基本相同,只把端口修改了就能夠。
把client-a和service-b都啓動成功後,打開eureka中心應該看到:
maven
1.3
打開http://localhost:8910/hi?id=123
能夠看到服務已經成功調用。測試
而後刷新頁面
看到端口已經改變,說明負載功能成功實現rest
2.1
新建模塊client-b
pom文件code
<?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.ymlserver
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.2
運行後的結果應該是和ribbon的相同。
我的感受使用feign比較舒服,代碼比較簡潔。