欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringCloud-Eureka 发现调用注册中心的服务

程序员文章站 2022-06-13 12:57:51
...

1. 发现调用注册中心的服务,需要一个相对服务来说的消费方 ,首先在本地创建maven项目,引入pom,消费方也要把服务提交到注册中心 。以发送邮件为例

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
</parent>
<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>
    
    <!-- 添加ribbon架包 -->
     <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-ribbon</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=SENDClient
#定义端口
server.port=815

3. 创建带main方法的类,value的值是提供服务方的唯一名称

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.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
//发送到注册中心
@EnableDiscoveryClient
@SpringBootApplication
//表示使用ribbon客户端 value表示发布方的服务名称
@RibbonClient(value="SENDMAIL")
public class Main {
	
	 @LoadBalanced
	    @Bean
	    RestTemplate restTemplate() {
	        return new RestTemplate();
	    }
	
	public static void main(String[] args) {
		SpringApplication.run(Main.class, args);
	}
}


4..创建一个html 

<!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.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

@Controller
public class SendController {
	
	//调用sendmail的controller
	@Autowired
	RestTemplate restTemplate;
	
	@Autowired
	LoadBalancerClient load;
	
	@ResponseBody
	@GetMapping("/choose")
	public String Choose(){
		StringBuffer su= new StringBuffer();
		for(int i=0;i<=10;i++){
			ServiceInstance ss= load.choose("SENDMAIL");//从两个IDserver中选择一个 这里涉及到选择算法
			su.append(ss.getUri().toString()+"<br/>");
		}
		return su.toString();
	}
	
	@GetMapping("/sendc")
	public String sends(String emailto,String emailsubject,String emailcenter){
		//调用sendmain服务
		String controller ="/send";
		//通过注册中心客户端负载均衡 获取一台主机来调用
		try{
			controller+="?emailto="+emailto+"&emailsubject="+emailsubject+"&emailcenter="+emailcenter;
			String result=restTemplate.getForObject("http://SENDMAIL"+controller, String.class);
		}catch(Exception e){
			return "redirect:/error.html";
		}
		return "redirect:/suc.html";
	}
	
	@GetMapping("/sendclient")
	public String send(String emailto,String emailsubject,String emailcenter){
		//调用sendmain服务
		String  controller ="/sends";
		//通过注册中心客户端负载均衡 获取一台主机来调用
		try{
			HttpHeaders headers = new HttpHeaders();
			Map map=new HashMap();    
			map.put("emailto", emailto);  
			map.put("emailsubject", emailsubject);  
			map.put("emailcenter", emailcenter);  
			HttpEntity<Map> request=new HttpEntity<Map>(map, headers);  
			restTemplate.postForObject("http://SENDMAIL"+controller,request,String.class);
		
		}catch(Exception e){
			return "redirect:/error.html";
		}
		
		return "redirect:/suc.html";
	}

	
}


6.在提供邮件方controller层取出消费方传过去的值

@PostMapping("/sends")
	public String sends(@RequestBody Map map){
		
		SimpleMailMessage smm= new SimpleMailMessage();
		//设置发送方
		smm.setFrom("aaa@qq.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.测试是否能够调用 

1.先运行注册中心 

2.运行提交服务方

3.运行消费方

SpringCloud-Eureka 发现调用注册中心的服务