SpringCloud -Eurelka的feign简单应用

本文介绍如何利用Feign实现微服务之间的邮件发送功能。从引入依赖开始,详细讲解了配置文件的设置、定义接口进行服务调用、创建发送邮件页面及Controller层的实现,最后演示了如何启动服务并进行测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


以发送邮件为例

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,发送邮件测试

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值