服务A和服务B需要注册到注册中心(nacos或eureka)
1,服务A接口
2,openfeign
3,服务B接口
代码一(服务A接口)
import org.springblade.core.tool.api.R; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author Administrator */ @RestController @RequestMapping("/client2") public class MailController2 { @Autowired private ClientService clientService; @GetMapping("/send-email2") public R sendMail(){ return this.clientService.sendEmail("", ""); } }
代码二(openfeign)(服务A接口)
import com.xc.device.feign.impl.DeviceClientFallBack; import org.springblade.core.tool.api.R; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; /** * @author Administrator */ @FeignClient(value = "调用服务注册名称",fallback = ClientFallBack.class) public interface ClientService { String PREFIX = "/client"; String SEND_EMAIL = PREFIX + "/send-email"; /** * * @param email * @param code * @return */ @GetMapping(SEND_EMAIL) R sendEmail(@RequestParam String email, @RequestParam String code); }
代码三(服务A接口)
import com.xc.device.feign.DeviceClientService; import org.springblade.core.tool.api.R; import org.springframework.stereotype.Service; /** * @author Administrator */ @Service public class ClientFallBack implements ClientService { @Override public R sendEmail(String email,String code){ return R.fail("失败"); } }
代码四(服务B接口)
import com.xc.device.common.exction.Constants; import org.springblade.core.tool.api.R; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author Administrator */ @RestController @RequestMapping("/client") public class MailController { @Autowired private JavaMailSender javaMailSender; @GetMapping("/send-email") public R sendMail(){ SimpleMailMessage message = new SimpleMailMessage(); //邮件发送 message.setFrom(Constants.XCMail); //邮件接收,多个帐号用逗号分割 message.setTo("123456@qq.com"); //邮件主题 message.setSubject("物联网设备管理平台告警通知"); //邮件内容 message.setText("test--test--test--test--test--test--test--test!"); javaMailSender.send(message); return R.success("ok"); } }