以前學(xué)習(xí)java,一般就一個(gè)后端耐量,都要學(xué)習(xí)如何在容器中運(yùn)行飞蚓,如tomcat,weblogic廊蜒,現(xiàn)在微服務(wù)顛覆了這一切趴拧,一個(gè)系統(tǒng)要被拆分成多個(gè)服務(wù)溅漾,服務(wù)與服務(wù)間需要通信,讓我想到了前端的ajax著榴,java里可沒(méi)js那樣方便添履,一般使用resttemplate,httpclient∧杂郑現(xiàn)在springcloud又帶來(lái)了一種新的服務(wù)調(diào)用方式--feign暮胧。
下面,我們創(chuàng)建一個(gè)工程測(cè)試feign问麸,先啟動(dòng)前面講的注冊(cè)中心往衷,feign客戶(hù)端作為一個(gè)消費(fèi)端,還需要一個(gè)提供端严卖。
創(chuàng)建消費(fèi)端席舍,工程依賴(lài)如下(這里使用boot1.5.x):
dependencyManagement {
imports {
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Edgware.SR4'
}
}
dependencies {
compile('org.springframework.boot:spring-boot-starter')
compile('org.springframework.boot:spring-boot-starter-web')
compile 'org.slf4j:slf4j-api:1.7.14'
compile('org.springframework.cloud:spring-cloud-starter-eureka')
compile('org.springframework.cloud:spring-cloud-starter-feign')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
接著配置端口并注冊(cè)到注冊(cè)中心,如下:
spring.application.name=feign-consumer
# 單機(jī)
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
server.port=8083
啟動(dòng)類(lèi)加上注解妄田,一個(gè)用于服務(wù)發(fā)現(xiàn),一個(gè)用于feign客戶(hù)端驮捍,可通過(guò)EnableFeignClients調(diào)用其他服務(wù)的api疟呐,如下:
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class CloudApplication {
public static void main(String[] args) {
SpringApplication.run(CloudApplication.class, args);
}
}
接著編寫(xiě)service,如下:
@FeignClient(name="HELLO-SERVICE", fallback = HelloServiceFallback.class)
public interface HelloService {
@RequestMapping("/hello")
String hello();
@RequestMapping(value = "/hello1", method = RequestMethod.GET)
String hello(@RequestParam("name") String name) ;
@RequestMapping(value = "/hello2", method = RequestMethod.GET)
User hello(@RequestHeader("name") String name, @RequestHeader("age") Integer age);
@RequestMapping(value = "/hello3", method = RequestMethod.POST)
String hello(@RequestBody User user);
}
上面定義一個(gè)feign客戶(hù)端东且,它指定了要消費(fèi)的服務(wù)名以及降級(jí)的處理類(lèi)启具,若調(diào)用service.hello()則會(huì)發(fā)起對(duì)應(yīng)請(qǐng)求:http://HELLO-SERVICE/hello
降級(jí)處理類(lèi)也很簡(jiǎn)單,只需實(shí)現(xiàn)service接口即可珊泳。
@Component
public class HelloServiceFallback implements HelloService {
@Override
public String hello() {
return "error";
}
@Override
public String hello(@RequestParam("name") String name) {
return "error";
}
@Override
public User hello(@RequestHeader("name") String name, @RequestHeader("age") Integer age) {
return new User("nothing", 0);
}
@Override
public String hello(@RequestBody User user) {
return "error";
}
}
其實(shí)上面的方法鲁冯,實(shí)現(xiàn)比較繁瑣,我們可以用更簡(jiǎn)單的方式色查,如下薯演,
@RequestMapping("/refactor")
public interface HelloService {
@RequestMapping(value = "/hello1", method = RequestMethod.GET)
String hello(@RequestParam("name") String name) ;
@RequestMapping(value = "/hello2", method = RequestMethod.GET)
User hello(@RequestHeader("name") String name, @RequestHeader("age") Integer age);
@RequestMapping(value = "/hello3", method = RequestMethod.POST)
String hello(@RequestBody User user);
}
上面我們重構(gòu)了service接口,將所有requestMapping寫(xiě)入秧了,其實(shí)與上面的變化也不大跨扮,最主要的區(qū)別是它可以被多模塊共享,可以以最簡(jiǎn)方式創(chuàng)建feignClient验毡,下面看下feignClient的實(shí)現(xiàn)衡创,如下:
@FeignClient(value = "HELLO-SERVICE")
public interface RefactorHelloService extends HelloService {
}
這樣是不是很簡(jiǎn)單呢
下面我們編寫(xiě)controller,只需注入上面的服務(wù)即可晶通。
@RestController
public class ConsumerController {
@Autowired
HelloService helloService;
@Autowired
RefactorHelloService refactorHelloService;
@RequestMapping(value = "/feign-consumer", method = RequestMethod.GET)
public String helloConsumer() {
return helloService.hello();
}
@RequestMapping(value = "/feign-consumer2", method = RequestMethod.GET)
public String helloConsumer2() {
StringBuilder sb = new StringBuilder();
sb.append(helloService.hello()).append("\n");
sb.append(helloService.hello("DIDI")).append("\n");
sb.append(helloService.hello("DIDI", 30)).append("\n");
sb.append(helloService.hello(new User("DIDI", 30))).append("\n");
return sb.toString();
}
@RequestMapping(value = "/feign-consumer3", method = RequestMethod.GET)
public String helloConsumer3() {
StringBuilder sb = new StringBuilder();
sb.append(refactorHelloService.hello("MIMI")).append("\n");
sb.append(refactorHelloService.hello("MIMI", 20)).append("\n");
sb.append(refactorHelloService.hello(new User("MIMI", 20))).append("\n");
return sb.toString();
}
}
上面主要講了消費(fèi)服務(wù)的創(chuàng)建璃氢,提供服務(wù)的創(chuàng)建請(qǐng)參考另一篇文章 SpringCloud-service 服務(wù)提供
學(xué)習(xí)交流,請(qǐng)加群:64691032