亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Springboot?內(nèi)部服務(wù)調(diào)用方式

 更新時間:2022年03月14日 17:28:10   作者:Kings菜鳥  
這篇文章主要介紹了Springboot?內(nèi)部服務(wù)調(diào)用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

Eureka注冊的服務(wù)之間互相調(diào)用

1.請求方

啟動類添加注解,掃描Eureka 中的全部服務(wù)

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class LoginServiceApplication {?? ?
? ? public static void main(String[] args) {
? ? ? ? new SpringApplicationBuilder(LoginServiceApplication.class).web(true).run(args);
? ? }? ??
}

pom.xml 添加包 (版本號 根據(jù)實際選擇)

<dependency>
? ? <groupId>org.springframework.cloud</groupId>
? ? <artifactId>spring-cloud-starter-feign</artifactId>
?? ?<version>1.4.6.RELEASE</version>
</dependency>

創(chuàng)建接口類

@FeignClient(name="hello-service") //spring service name
public interface FeignVehicle {
?? ?
?? ?@RequestMapping(value="/hello", method = RequestMethod.GET)
?? ?@ResponseBody
?? ?public List<Map> hello(@RequestParam Map<String,String> params);
}

實現(xiàn)類注入此接口類

@Autowired
FeignVehicle feignVehicle;

使用的時候直接按照正常調(diào)用方式即可

Map<String,String> map = new HashMap<String, String>();
feignVehicle.hello(map);

跨服務(wù)調(diào)用的時候出現(xiàn)token信息取不到,在發(fā)送方添加攔截器

@Configuration
public class FeignConfiguration {
?
? ? @Bean
? ? public RequestInterceptor requestInterceptor() {
? ? ? ? return new RequestInterceptor() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void apply(RequestTemplate template) {?
? ? ? ? ? ? ? ? ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
? ? ? ? ? ? ? ? ? ? ? ? .getRequestAttributes();
? ? ? ? ? ? ? ? HttpServletRequest request = attributes.getRequest(); ?//當(dāng)前服務(wù)token
?
? ? ? ? ? ? ? ? template.header("Authorization","Bearer " + request.getSession().getId()); //template 接收請求方token
? ? ? ? ? ? }?
? ? ? ? };
? ? }
}

2.接收方

請求 啟動類

@SpringBootApplication
@EnableEurekaClient
public class HelloServiceApplication {?? ?
? ? public static void main(String[] args) {
? ? ? ? new SpringApplicationBuilder(HelloServiceApplication.class).web(true).run(args);
? ? }? ??
}

請求Controller

@Controller
@RequestMapping("/hello")
public class HelloController {?? ?
? ? @RequestMapping(value="/hello",method = RequestMethod.GET)
? ? @ResponseBody
? ? public List<Map> hello(@RequestParam Map<String, String> queryParam) {
? ? ? ? return null; ?
? ? }
}

多模塊化,服務(wù)間調(diào)用的坑

問題背景

  • product 服務(wù)作為服務(wù)端,提供了一個 對外通信Fegin接口 ProductClient,放在了com.imooc.product.client jar包下
  • order 服務(wù)作為客戶端,直接引用上面的jar,使用 ProductClient ,啟動主類后報下圖錯誤:

解決辦法

多模塊化時,應(yīng)該在order主類上添加下面圈出來的注解,這樣啟動后就能掃描這個包。

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論