本网站(662p.com)打包出售,且带程序代码数据,662p.com域名,程序内核采用TP框架开发,需要联系扣扣:2360248666 /wx:lianweikj
精品域名一口价出售:1y1m.com(350元) ,6b7b.com(400元) , 5k5j.com(380元) , yayj.com(1800元), jiongzhun.com(1000元) , niuzen.com(2800元) , zennei.com(5000元)
需要联系扣扣:2360248666 /wx:lianweikj
Springboot 内部服务调用方式
talkchan · 230浏览 · 发布于2022-03-15 +关注

这篇文章主要介绍了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主类上添加下面圈出来的注解,这样启动后就能扫描这个包。

 


相关推荐

PHP实现部分字符隐藏

沙雕mars · 1327浏览 · 2019-04-28 09:47:56
Java中ArrayList和LinkedList区别

kenrry1992 · 912浏览 · 2019-05-08 21:14:54
Tomcat 下载及安装配置

manongba · 976浏览 · 2019-05-13 21:03:56
JAVA变量介绍

manongba · 965浏览 · 2019-05-13 21:05:52
什么是SpringBoot

iamitnan · 1092浏览 · 2019-05-14 22:20:36
加载中

0评论

评论
大家好,我是一名专注技术开发的技术屌丝,有什么问题可以互相交流,一起学习进步,谢谢。
分类专栏
小鸟云服务器
扫码进入手机网页