javascript
feign和ajax,SpringCloud-feign 声明式服务调用
以前學習java,一般就一個后端,都要學習如何在容器中運行,如tomcat,weblogic,現在微服務顛覆了這一切,一個系統要被拆分成多個服務,服務與服務間需要通信,讓我想到了前端的ajax,java里可沒js那樣方便,一般使用resttemplate,httpclient?,F在springcloud又帶來了一種新的服務調用方式--feign。
下面,我們創建一個工程測試feign,先啟動前面講的注冊中心,feign客戶端作為一個消費端,還需要一個提供端。
創建消費端,工程依賴如下(這里使用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')
}
接著配置端口并注冊到注冊中心,如下:
spring.application.name=feign-consumer
# 單機
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
server.port=8083
啟動類加上注解,一個用于服務發現,一個用于feign客戶端,可通過EnableFeignClients調用其他服務的api,如下:
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class CloudApplication {
public static void main(String[] args) {
SpringApplication.run(CloudApplication.class, args);
}
}
接著編寫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);
}
上面定義一個feign客戶端,它指定了要消費的服務名以及降級的處理類,若調用service.hello()則會發起對應請求:http://HELLO-SERVICE/hello
降級處理類也很簡單,只需實現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";
}
}
其實上面的方法,實現比較繁瑣,我們可以用更簡單的方式,如下,
@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);
}
上面我們重構了service接口,將所有requestMapping寫入,其實與上面的變化也不大,最主要的區別是它可以被多模塊共享,可以以最簡方式創建feignClient,下面看下feignClient的實現,如下:
@FeignClient(value = "HELLO-SERVICE")
public interface RefactorHelloService extends HelloService {
}
這樣是不是很簡單呢
下面我們編寫controller,只需注入上面的服務即可。
@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();
}
}
上面主要講了消費服務的創建,提供服務的創建請參考另一篇文章 SpringCloud-service 服務提供
學習交流,請加群:64691032
總結
以上是生活随笔為你收集整理的feign和ajax,SpringCloud-feign 声明式服务调用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: wdcp服务器/虚拟主机管理系统,wdc
- 下一篇: exchange服务器维护模式命令,Ex