springcloud-openfeign

服务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");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值