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)文章
在IntelliJ IDEA中使用gulp的方法步驟(圖文)
這篇文章主要介紹了在IntelliJ IDEA中使用gulp的方法步驟(圖文),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01java final 和instanceof 關(guān)鍵字的區(qū)別
這篇文章介紹了java final 和instanceof 關(guān)鍵字的區(qū)別,有需要的朋友可以參考一下2013-09-09教你創(chuàng)建springcloud微服務(wù)的基礎(chǔ)子服務(wù)的超詳細(xì)過程
這篇文章主要介紹了創(chuàng)建springcloud微服務(wù)的基礎(chǔ)子服務(wù),主要是創(chuàng)建兩個springboot服務(wù),在教程中增加springcloud相關(guān)組件,本文分步驟給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04詳解mybatis-plus的 mapper.xml 路徑配置的坑
這篇文章主要介紹了詳解mybatis-plus的 mapper.xml 路徑配置的坑,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08SpringCloud之Zuul網(wǎng)關(guān)原理及其配置講解
這篇文章主要介紹了SpringCloud之Zuul網(wǎng)關(guān)原理及其配置講解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03