1. Discovering the services that call the registry requires a consumer with a relative service, first creating a MAVEN project locally, introducing the POM, and the consumer submitting the service to the registry. To send a message as an example
<parent> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-starter-par ent</artifactid> <version>1.5.9.RELEASE</version> </parent> <dependencyManagement> &
Lt;dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>dalston.sr4</version>
; <type>pom</type> <scope>import</scope> </dependency> </dependenci Es> </dependencyManagement> <dependencies> <!--springboot The integration of each frame is a starter--and Lt;dependency> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-st Arter-web</artifactid> </dependency> <!--add Ribbon rack packs--<dependency> &L t;groupid≫org.springframework.cloud</groupid> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> <!--Send to registration center--<dependency> <groupid>org.springframework.cloud
</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependencies>
2. Create a application.properties file in src/main/resources, configure conditions, name and port can be defined by themselves but must be unique
#配置访问注册中心的路径
eureka.client.serviceurl.defaultzone= http://localhost:8761/eureka/
#定义唯一的名称
Spring.application.name=sendclient
#定义端口
server.port=815
3. Create a class with the main method, the value of which is the unique name that provides the service party
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;
Send to registry
@EnableDiscoveryClient
@SpringBootApplication
//Indicates the service name of the publisher using the Ribbon client 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: Create an HTML
<! DOCTYPE html>
5. Create a controller layer
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 {//Call 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");//Select one from two idserver here involves the selection algorithm su.append (ss.get
Uri (). toString () + "<br/>"); } return Su.tosTring ();
} @GetMapping ("/SENDC") public String sends (String emailto,string emailsubject,string emailcenter) {//Invoke Sendmain service
String controller = "/send"; Get a host from the registry client load Balancer to invoke 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) {//Call Sendmain
Service String controller = "/sends";
Obtain a host from the registry client load Balancer to invoke 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. Take out the value of the consumer passed in the provision of the Mail party controller layer
@PostMapping ("/sends") public
String sends (@RequestBody map map) {
simplemailmessage smm= new Simplemailmessage ();
Set sender
smm.setfrom ("zmw960221@126.com");
Set
the recipient Smm.setto (Map.get ("Emailto"). toString ());
Set the message header
Smm.setsubject (Map.get ("EmailSubject"). toString ());
Set the message content
Smm.settext (map.get ("Emailcenter"). toString ());
Jms.send (SMM);
return "1";
}
7. Test whether you can call
1. Run the registration center first
2. Run the submission service party
3. Run the consumer