不多bb了,很烦就,上次写的笔记硬生生看了半小时才看懂写的啥玩意,这次要写的详细点了。
首先介绍一下情况:目前springcloud有两个模块一个是member模块,负责用户资料的查询,一个是activiti工作流程模块,负责工作流程的编写。我现在需要activiti工作流程模块调用member的接口进行查询。
1、member模块,啥也不用干,只要将nacos配置写入yml文件然后在启动类开启@EnableDiscoveryClient即可,配置文件如下:
spring:
datasource:
username: root
url: jdbc:mysql://localhost:3306/zspoa_member?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
driver-class-name: com.mysql.cj.jdbc.Driver
password: 123456
cloud:
nacos:
server-addr: 127.0.0.1:8848
application:
name: zspoa-member
jpa:
show-sql: true
hibernate:
ddl-auto: update
server:
port: 9000
2、activiti需要导入pom依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
3、创建feign文件夹,编写接口(注:这里的接口FeignClient需要写上你要调用的服务注册的名字还有对应的接口url写对:
package com.zsp.zspoaactiviti.feign;
import com.zsp.utils.R;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@Component
@FeignClient(value = "zspoa-member")
public interface MemberFeignService {
@GetMapping("member/list")
public R memberList();
@GetMapping("/add/{userName}/{userPassword}")
public R addMember(@PathVariable(value = "userName")String userName, @PathVariable(value = "userPassword") String userPassword );
}
4、到你的controller上调用该方法即可:
package com.zsp.zspoaactiviti.controller;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zsp.utils.R;
import com.zsp.zspoaactiviti.entity.ActRuTask;
import com.zsp.zspoaactiviti.feign.MemberFeignService;
import com.zsp.zspoaactiviti.service.ActRuTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
import java.util.stream.Collectors;
/**
* <p>
* 前端控制器
* </p>
*
* @author zsp
* @since 2021-05-01
*/
@RestController
@RequestMapping("acti/task")
public class ActRuTaskController {
@Autowired
ActRuTaskService actRuTaskService;
@Autowired
MemberFeignService memberFeignService;
@Autowired
StringRedisTemplate stringRedisTemplate;
@GetMapping("/list")
@GetMapping("/member/list")
@Cacheable(value = "memberList")
public R getMemberList(){
return memberFeignService.memberList();
}
@GetMapping("/member/add/{userName}/{userPassword}")
@CachePut(value = "memberList")
public R addMember(@PathVariable(value = "userName")String userName,
@PathVariable(value = "userPassword")String userPassword){
return memberFeignService.addMember(userName,userPassword);
}
}