日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

feign和ajax,SpringCloud-feign 声明式服务调用

發布時間:2023/12/4 javascript 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 feign和ajax,SpringCloud-feign 声明式服务调用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

以前學習java,一般就一個后端,都要學習如何在容器中運行,如tomcat,weblogic,現在微服務顛覆了這一切,一個系統要被拆分成多個服務,服務與服務間需要通信,讓我想到了前端的ajax,java里可沒js那樣方便,一般使用resttemplate,httpclient。現在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 声明式服务调用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。