Springboot 内部服务调用方式
作者:Kings菜鸟 发布时间:2023-08-24 00:32:20
标签:Springboot,内部服务,调用
Eureka注册的服务之间互相调用
1.请求方
启动类添加注解,扫描Eureka 中的全部服务
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class LoginServiceApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(LoginServiceApplication.class).web(true).run(args);
}
}
pom.xml 添加包 (版本号 根据实际选择)
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
创建接口类
@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);
}
实现类注入此接口类
@Autowired
FeignVehicle feignVehicle;
使用的时候直接按照正常调用方式即可
Map<String,String> map = new HashMap<String, String>();
feignVehicle.hello(map);
跨服务调用的时候出现token信息取不到,在发送方添加 *
@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(); //当前服务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;
}
}
多模块化,服务间调用的坑
问题背景
product
服务作为服务端,提供了一个 对外通信Fegin接口 ProductClient,放在了com.imooc.product.client jar包下order
服务作为客户端,直接引用上面的jar,使用 ProductClient ,启动主类后报下图错误:
解决办法
多模块化时,应该在order主类上添加下面圈出来的注解,这样启动后就能扫描这个包。
来源:https://blog.csdn.net/purple25/article/details/88691740
0
投稿
猜你喜欢
- mapper文件使用in("str1","str2")mybatis的xxxMapper.xml文件
- 效果:原图加水印后的图片废话不多说,直接上代码代码:package com.example.demo;import java.awt.Alp
- requestFoucs();无效。requestFoucsFromTouch();无效。webview.setTouchListener;
- 最新开发新项目的时候,要做分享项目,要求分享有微信,微信朋友圈,QQ,QQ空间,新浪微博这五个,所分享内容包括,分享纯图片,纯文字,图文类型
- 如图所示的效果相信大家都不陌生,我们可以使用很多种方法去实现此效果,这里自己采用CountDownTimer定时器简单封装下此效果,方便我们
- 我们都知道单精度浮点数(Single,float,Real)由32位0或1组成,它具体是如何来的。浮点数的32位N=1符号位(Sign)+8
- 本文实例为大家分享了Android实现登录注册功能的具体代码,供大家参考,具体内容如下运行环境 Android Studio总体效果图一、
- 右击有main方法的类===> Run as===> Run Configurations ===>双击java
- 前言前阵子有同学反馈Flutter中的http请求无法通过fiddler抓包,作者喜欢使用Charles抓包工具,于是抽时间写了个小demo
- 引言在高并发的场景下,异步是一个极其重要的优化方向。前段时间,生产环境发生一次事故,笔者认为事故的场景非常具备典型性 。写这篇文章,笔者想和
- 实现方案:我们直接参考实例代码:private String pattern = "((http|ftp
- 线程状态NEW:刚创建未启动的线程RUNNABLE:正在执行状态BLOCKED:处于阻塞状态的线程WAITING:正在等待另一个线程执行特定
- 反射方式获取JPA Entity属性和值在记录日志或者调试的时候,往往需要输出数据库查询或者写入的值,或者在接 * 互的时候,可能需要将实体转
- 多数据源创建数据库CREATE DATABASE mybatis_plus_1;USE mybatis_plus_1;CREATE TABL
- 在使用springMVC框架构建web应用,客户端常会请求字符串、整型、json等格式的数据,通常使用@ResponseBody注解使 co
- Pom依赖<parent> <groupId>org.springframework.bo
- 在java.lang.Runtime.exec的使用中,我们经常会用到将重定向命令执行的输入/结果或者将错误信息读取出来.那么,在使用过程中
- 具体代码如下所示:***web.xml***<?xml version="1.0" encoding="
- 我们讲一下Criteria查询,这个对于不是太熟悉SQL语句的我们这些程序员来说是很容易上手的。 废话不多说,看一下例子:&nbs
- 简单的实现了一个树的结构,很不完善!后续参考一些其他代码的实现。试图实现叶子存在可变的节点,能够用来解析xml文件。叶子的代码:packag