以发送邮件为例
1.引用feign 需要先引用feign的包
<!-- 继承spring boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<!-- 引用springcloud -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- springboot每一个框架的集成都是一个starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 添加feign架包 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<!--发送到注册中心 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
2.在src/main/resources创建一个application.properties的文件
#配置访问注册中心的路径
eureka.client.serviceUrl.defaultZone= http://localhost:8761/eureka/
#定义唯一的名称
spring.application.name=SENDFeignClient
#定义端口
server.port=812
#配置提供服务的负载均衡的算法
SENDMAIL.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule
3.feign的调用本质是面向接口编程 ,所有需要定义一个接口,接口里面的方法名要和被调用微服务的方法的名称一致,否则调用不了
package cn.et;
import java.util.Map;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
//调用微服务的名字
@FeignClient("SENDMAIL")
public interface ISendMail {
@PostMapping("/sends")
public String sends(@RequestBody Map map);
}
4.创建一个页面用来发送邮件
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="sendclient">
接收人 :<input type="text" name="emailto"/><br/>
主题 :<input type="text" name="emailsubject"/><br/>
内容 :<textarea rows="20" cols="20" name="emailcenter"></textarea><br/>
<input type="submit" value="发送"/>
</form>
</body>
</html>
5.创建一个controller层用来调用实现接口里面的方法。
package cn.et;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class SendController {
//调用feign定义的接口
@Autowired
ISendMail isd;
@GetMapping("/sendclient")
public String send(String emailto,String emailsubject,String emailcenter){
//调用sendmain服务
String controller ="/sends";
//通过注册中心客户端负载均衡 获取一台主机来调用
try{
//创建一个map来保存传过来的值
Map map=new HashMap();
map.put("emailto", emailto);
map.put("emailsubject", emailsubject);
map.put("emailcenter", emailcenter);
//发送到微服务
isd.sends(map);
return "redirect:/suc.html";
}catch(Exception e){
e.printStackTrace();
return "redirect:/error.html";
}
}
}
5.在 服务方取出传过来的值
package cn.et;
import java.util.Map;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MailController {
@Autowired
JavaMailSender jms;
@PostMapping("/sends")
public String sends(@RequestBody Map map){
SimpleMailMessage smm= new SimpleMailMessage();
//设置发送方
smm.setFrom("zmw960221@126.com");
//设置收件方
smm.setTo(map.get("emailto").toString());
//设置邮件标题
smm.setSubject(map.get("emailsubject").toString());
//设置邮件内容
smm.setText(map.get("emailcenter").toString());
jms.send(smm);
return "1";
}
}
7.创建main方法的类
package cn.et;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
//发送到注册中心
@EnableDiscoveryClient
@SpringBootApplication
//启用feign的客户端
@EnableFeignClients
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
8.启动服务
1.先启动注册中心
2.启动提供服务方
3.启动feign,发送邮件测试