springcloud配置注册和发现

配置注册中心:

eurekaserver中加入依赖

[html]  view plain  copy
  1. <parent>  
  2.   
  3.     <groupId>org.springframework.boot</groupId>  
  4.   
  5.     <artifactId>spring-boot-starter-parent</artifactId>  
  6.   
  7.     <version>1.5.9.RELEASE</version>  
  8.   
  9. </parent>  
  10.   
  11. <dependencyManagement>  
  12.   
  13.     <dependencies>  
  14.   
  15.         <dependency>  
  16.   
  17.             <groupId>org.springframework.cloud</groupId>  
  18.   
  19.             <artifactId>spring-cloud-dependencies</artifactId>  
  20.   
  21.             <version>Dalston.SR4</version>  
  22.   
  23.             <type>pom</type>  
  24.   
  25.             <scope>import</scope>  
  26.   
  27.         </dependency>  
  28.   
  29.     </dependencies>  
  30.   
  31. </dependencyManagement>  
  32.   
  33. <dependencies>  
  34.   
  35.     <dependency>  
  36.   
  37.         <groupId>org.springframework.cloud</groupId>  
  38.   
  39.         <artifactId>spring-cloud-starter-eureka-server</artifactId>  
  40.   
  41.     </dependency>  
  42.   
  43.      
  44.   
  45. </dependencies>  

注册中心的资源文件application.yml

server:

  port: 8761#eureka默认端口

 

eureka:

  instance:

hostname: localhost#主机名

  client:

    registerWithEureka: false

    fetchRegistry: false

    serviceUrl:

      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

网址:

http://cloud.spring.io/spring-cloud-static/Dalston.SR5/single/spring-cloud.html#spring-cloud-eureka-server

 

注册中心的启动类(EnableEurekaServer)

[html]  view plain  copy
  1. package cn.et;  
  2.   
  3.    
  4.   
  5. import org.springframework.boot.SpringApplication;  
  6.   
  7. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  8.   
  9. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;  
  10.   
  11.    
  12.   
  13. @SpringBootApplication  
  14.   
  15. @EnableEurekaServer  
  16.   
  17. public class Main {  
  18.   
  19. public static void main(String[] args) {  
  20.   
  21. SpringApplication.run(Main.class, args);  
  22.   
  23. }  
  24.   
  25. }  



eurekaServer的访问地址:localhost:8761  出现界面

 

eureka client 的配置(客户端)

[html]  view plain  copy
  1. spring.mail.host=smtp.163.com  
  2.   
  3. Spring.mail.password=123456  
  4.   
  5. spring.mail.port=25  
  6.   
  7. spring.mail.protocol=smtp  
  8.   
  9. spring.mail.username=raya_l@163.com  
  10.   
  11. eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/  
  12.   
  13. spring.application.name=email  
  14.   
  15. server.port=8081  
  16.   
  17.    
  18.   
  19. 在注册中的main方法上加@EnableEurekaClient启动自动注册  


 

 

Eureka客户端启动类

[html]  view plain  copy
  1. package cn.et;  
  2.   
  3.    
  4.   
  5. import org.springframework.boot.SpringApplication;  
  6.   
  7. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  8.   
  9. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;  
  10.   
  11. @EnableEurekaClient  
  12.   
  13. @SpringBootApplication  
  14.   
  15. public class Main {  
  16.   
  17. public static void main(String[] args) {  
  18.   
  19. SpringApplication.run(Main.class, args);  
  20.   
  21. }  
  22.   
  23. }  



Ribbon

  

[html]  view plain  copy
  1. <dependency>  
  2.   
  3.         <groupId>org.springframework.cloud</groupId>  
  4.   
  5.         <artifactId>spring-cloud-starter-ribbon</artifactId>  
  6.   
  7. </dependency>  

 

@EnableEurekaClient@EnableDiscoveryClient是一样的

Ribbonmain方法

[html]  view plain  copy
  1. @SpringBootApplication  
  2.   
  3. @EnableEurekaClient  
  4.   
  5. @RibbonClient(value="EMAIL")  
  6.   
  7. /**  
  8.   
  9.  * 表示当前这个服务需要调用其他的服务的名称  
  10.   
  11.  * @author Administrator  
  12.   
  13.  *  
  14.   
  15.  */  
  16.   
  17. @Configuration  
  18.   
  19. public class Main {  
  20.   
  21.   @LoadBalanced  
  22.   
  23.     @Bean  
  24.   
  25.     RestTemplate restTemplate() {  
  26.   
  27.         return new RestTemplate();  
  28.   
  29.     }  
  30.   
  31. public static void main(String[] args) {  
  32.   
  33. SpringApplication.run(Main.class, args);  
  34.   
  35. }  


Ribbonget发送和post发送

 

[html]  view plain  copy
  1. package cn.et;  
  2.   
  3.    
  4.   
  5. import java.util.HashMap;  
  6.   
  7. import java.util.Map;  
  8.   
  9.    
  10.   
  11. import org.springframework.beans.factory.annotation.Autowired;  
  12.   
  13. import org.springframework.cloud.client.ServiceInstance;  
  14.   
  15. import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;  
  16.   
  17. import org.springframework.http.HttpEntity;  
  18.   
  19. import org.springframework.http.HttpHeaders;  
  20.   
  21. import org.springframework.stereotype.Controller;  
  22.   
  23. import org.springframework.web.bind.annotation.GetMapping;  
  24.   
  25. import org.springframework.web.bind.annotation.PostMapping;  
  26.   
  27. import org.springframework.web.bind.annotation.RequestMapping;  
  28.   
  29. import org.springframework.web.bind.annotation.ResponseBody;  
  30.   
  31. import org.springframework.web.client.RestClientException;  
  32.   
  33. import org.springframework.web.client.RestTemplate;  
  34.   
  35.    
  36.   
  37.    
  38.   
  39. @Controller  
  40.   
  41. public class SendController {  
  42.   
  43. @Autowired  
  44.   
  45. private RestTemplate restTemplate;//通过restTemplate来调用ribbon  
  46.   
  47. @Autowired    
  48.   
  49.     private LoadBalancerClient loadBalancer;  
  50.   
  51. /**    
  52.   
  53.      * 启动多个发布者 端口不一致 程序名相同     
  54.   
  55.      * 使用    
  56.   
  57.      * @LoadBalanced必须添加    
  58.   
  59.      * @return    
  60.   
  61.      */    
  62.   
  63. @ResponseBody  
  64.   
  65.     @RequestMapping("/choosePub")    
  66.   
  67.     public String choosePub() {    
  68.   
  69.         StringBuffer sb=new StringBuffer();    
  70.   
  71.         for(int i=0;i<=10;i++) {    
  72.   
  73.             ServiceInstance ss=loadBalancer.choose("EMAILSERVER");//从两个idserver中选择一个 这里涉及到选择算法    
  74.   
  75.             sb.append(ss.getUri().toString()+"<br/>");  
  76.   
  77.         }    
  78.   
  79.         return sb.toString();    
  80.   
  81.     }      
  82.   
  83. @GetMapping("/sendClient")  
  84.   
  85. public String send(String email_to,String email_subject ,String email_content){  
  86.   
  87. //调用email服务  
  88.   
  89. String controller="/send";  
  90.   
  91. //通过注册中心客户端负载均衡  获取一台主机来调用  
  92.   
  93. try {  
  94.   
  95. controller += "?email_to="+email_to+"&email_subject="+email_subject+"&email_content="+email_content;  
  96.   
  97. String result=restTemplate.getForObject("http://EMAILSERVER"+controller, String.class);  
  98.   
  99. } catch (RestClientException e) {  
  100.   
  101. // TODO Auto-generated catch block  
  102.   
  103. e.printStackTrace();  
  104.   
  105. return "redirect:/error.html";  
  106.   
  107. }  
  108.   
  109.    
  110.   
  111. //通过redirect返回string类型跳转,不允许spring控制器用@ResponseBody  
  112.   
  113. return "redirect:/suc.html";  
  114.   
  115. }  
  116.   
  117. /**  
  118.   
  119.  * 演示post  
  120.   
  121.  * @param email_to  
  122.   
  123.  * @param email_subject  
  124.   
  125.  * @param email_content  
  126.   
  127.  * @return  
  128.   
  129.  */  
  130.   
  131. @PostMapping("/sendClientpost")  
  132.   
  133. public String postsend(String email_to,String email_subject ,String email_content){  
  134.   
  135. try {  
  136.   
  137. //  
  138.   
  139. HttpHeaders headers=new HttpHeaders();  
  140.   
  141. Map<String,Object> map=new HashMap<String,Object>();  
  142.   
  143. map.put("email_to",email_to );  
  144.   
  145. map.put("email_subject", email_subject);  
  146.   
  147. map.put("email_content", email_content);  
  148.   
  149. HttpEntity<Map> request=new HttpEntity<Map>(map, headers);  
  150.   
  151. String result=restTemplate.postForObject("http://EMAILSERVER/send",request, String.class);  
  152.   
  153. } catch (RestClientException e) {  
  154.   
  155. // TODO Auto-generated catch block  
  156.   
  157. e.printStackTrace();  
  158.   
  159. return "redirect:/error.html";  
  160.   
  161. }  
  162.   
  163. return "redirect:/suc.html";  
  164.   
  165. }  
  166.   
  167. /**  
  168.   
  169.  * 演示调用sendmail的/user/这个请求  
  170.   
  171.  * @return  
  172.   
  173.  */  
  174.   
  175. @ResponseBody  
  176.   
  177. @GetMapping("/invokeUser")  
  178.   
  179. public String invokeUser(String id){  
  180.   
  181. String resul=restTemplate.getForObject("http://EMAILSERVER/user/{id}",String.class,id);  
  182.   
  183. return resul;  
  184.   
  185. }  
  186.   
  187. }  


 

 

 

 

发送邮件的控制类

 

[html]  view plain  copy
  1. package cn.et;  
  2.   
  3.    
  4.   
  5. import java.util.HashMap;  
  6.   
  7. import java.util.Map;  
  8.   
  9.    
  10.   
  11. import org.springframework.beans.factory.annotation.Autowired;  
  12.   
  13. import org.springframework.mail.SimpleMailMessage;  
  14.   
  15. import org.springframework.mail.javamail.JavaMailSender;  
  16.   
  17. import org.springframework.web.bind.annotation.GetMapping;  
  18.   
  19. import org.springframework.web.bind.annotation.PathVariable;  
  20.   
  21. import org.springframework.web.bind.annotation.PostMapping;  
  22.   
  23. import org.springframework.web.bind.annotation.RequestBody;  
  24.   
  25. import org.springframework.web.bind.annotation.RestController;  
  26.   
  27.    
  28.   
  29. @RestController  
  30.   
  31. public class MailController {  
  32.   
  33. @Autowired  
  34.   
  35. private JavaMailSender jms;  
  36.   
  37. @PostMapping("/send")  
  38.   
  39. public String send(@RequestBody Map<String,Object> map){  
  40.   
  41. SimpleMailMessage mailMessage=new SimpleMailMessage();  
  42.   
  43. mailMessage.setFrom("[email protected]");  
  44.   
  45. mailMessage.setTo(map.get("email_to").toString());  
  46.   
  47. mailMessage.setSubject(map.get("email_subject").toString());  
  48.   
  49. mailMessage.setText(map.get("email_content").toString());  
  50.   
  51. jms.send(mailMessage);  
  52.   
  53. return "1";  
  54.   
  55. }  
  56.   
  57. @GetMapping("/a")  
  58.   
  59. public String a(){  
  60.   
  61. return "2";  
  62.   
  63. }  
  64.   
  65. @GetMapping("/user/{userId}")  
  66.   
  67. public Map getUser(@PathVariable String userId){  
  68.   
  69. Map map=new HashMap();  
  70.   
  71. map.put("id", userId);  
  72.   
  73. map.put("name", "zs_"+userId);  
  74.   
  75. return map;  
  76.   
  77. }  
  78.   
  79. }  


 

 

启动MailController

[html]  view plain  copy
  1. package cn.et;  
  2.   
  3.    
  4.   
  5. import org.springframework.boot.SpringApplication;  
  6.   
  7. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  8.   
  9. import org.springframework.cloud.client.loadbalancer.LoadBalanced;  
  10.   
  11. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;  
  12.   
  13. import org.springframework.cloud.netflix.ribbon.RibbonClient;  
  14.   
  15. import org.springframework.context.annotation.Bean;  
  16.   
  17. import org.springframework.context.annotation.Configuration;  
  18.   
  19. import org.springframework.web.client.RestTemplate;  
  20.   
  21.    
  22.   
  23. @SpringBootApplication  
  24.   
  25. @EnableEurekaClient  
  26.   
  27. @RibbonClient(value = "EMAILSERVER")  
  28.   
  29. /**  
  30.   
  31.  * 表示当前这个服务需要调用其他的服务的名称  
  32.   
  33.  *  
  34.   
  35.  * @author Administrator  
  36.   
  37.  *  
  38.   
  39.  */  
  40.   
  41. @Configuration  
  42.   
  43. public class Main {  
  44.   
  45. @LoadBalanced//启动负载均衡  
  46.   
  47. @Bean  
  48.   
  49. RestTemplate restTemplate() {  
  50.   
  51. return new RestTemplate();  
  52.   
  53. }  
  54.   
  55.    
  56.   
  57. public static void main(String[] args) {  
  58.   
  59. SpringApplication.run(Main.class, args);  
  60.   
  61. }  
  62.   
  63.    
  64.   
  65. }  


 

定义LoadBalancerClient 可以用于测试选择的服务器的算法 负载的算法的类需要实现 IRule 接口 以下列表摘自网络

策略名

策略描述

BestAvailableRule

选择一个最小的并发请求的server

AvailabilityFilteringRule

过滤掉那些因为一直连接失败的被标记为circuit tripped的后端server,并过滤掉那些高并发的的后端server(active connections 超过配置的阈值)

WeightedResponseTimeRule

根据相应时间分配一个weight,相应时间越长,weight越小,被选中的可能性越低。

RetryRule

对选定的负载均衡策略机上重试机制。

RoundRobinRule

roundRobin方式轮询选择server

RandomRule

随机选择一个server

ZoneAvoidanceRule

复合判断server所在区域的性能和server的可用性选择server

ribbon默认的配置类为 RibbonClientConfiguration 其中配置IRule的bean为

 

负载均衡的规则配置:

EMAILSERVER:用户名

EMAILSERVER.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule

 

Feign(面向接口式)

加入依赖:

[html]  view plain  copy
  1. <dependency>  
  2.   
  3.         <groupId>org.springframework.cloud</groupId>  
  4.   
  5.         <artifactId>spring-cloud-starter-feign</artifactId>  
  6.   
  7. </dependency>  


 

配置文件:

[html]  view plain  copy
  1. spring.application.name=EMAIL  
  2.   
  3. server.port=8888  
  4.   
  5. eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/  
  6.   
  7. #服务器的算法(随机)  
  8.   
  9. EMAILSERVER.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule  


 

接口

[html]  view plain  copy
  1. package cn.et;  
  2.   
  3.    
  4.   
  5. import java.util.Map;  
  6.   
  7.    
  8.   
  9. import org.springframework.cloud.netflix.feign.FeignClient;  
  10.   
  11. import org.springframework.web.bind.annotation.GetMapping;  
  12.   
  13. import org.springframework.web.bind.annotation.PathVariable;  
  14.   
  15. import org.springframework.web.bind.annotation.PostMapping;  
  16.   
  17. import org.springframework.web.bind.annotation.RequestBody;  
  18.   
  19.    
  20.   
  21. @FeignClient("EMAILSERVER")  
  22.   
  23. public interface IsendMail {  
  24.   
  25. @GetMapping("/user/{userId}")  
  26.   
  27. public Map getUser(@PathVariable("userId") String userId);  
  28.   
  29. @PostMapping("/send")  
  30.   
  31. public String send(@RequestBody Map<String,Object> map);  
  32.   
  33. }  


控制类

[html]  view plain  copy
  1. package cn.et;  
  2.   
  3.   
  4.   
  5. import java.util.HashMap;  
  6.   
  7. import java.util.Map;  
  8.   
  9.    
  10.   
  11. import org.springframework.beans.factory.annotation.Autowired;  
  12.   
  13. import org.springframework.http.HttpEntity;  
  14.   
  15. import org.springframework.http.HttpHeaders;  
  16.   
  17. import org.springframework.stereotype.Controller;  
  18.   
  19. import org.springframework.web.bind.annotation.GetMapping;  
  20.   
  21. import org.springframework.web.bind.annotation.PostMapping;  
  22.   
  23. import org.springframework.web.bind.annotation.ResponseBody;  
  24.   
  25. import org.springframework.web.client.RestClientException;  
  26.   
  27.    
  28.   
  29.    
  30.   
  31. @Controller  
  32.   
  33. public class SendController {  
  34.   
  35.    
  36.   
  37. @Autowired  
  38.   
  39. private IsendMail sendMail;  
  40.   
  41. @GetMapping("/sendClient")  
  42.   
  43. public String send(String email_to,String email_subject ,String email_content){  
  44.   
  45. //调用email服务  
  46.   
  47. String controller="/send";  
  48.   
  49. //通过注册中心客户端负载均衡  获取一台主机来调用  
  50.   
  51. try {  
  52.   
  53. controller += "?email_to="+email_to+"&email_subject="+email_subject+"&email_content="+email_content;  
  54.   
  55. //String result=restTemplate.getForObject("http://EMAILSERVER"+controller, String.class);  
  56.   
  57. } catch (RestClientException e) {  
  58.   
  59. // TODO Auto-generated catch block  
  60.   
  61. e.printStackTrace();  
  62.   
  63. return "redirect:/error.html";  
  64.   
  65. }  
  66.   
  67. return "redirect:/suc.html";  
  68.   
  69. }  
  70.   
  71. /**  
  72.   
  73.  * 演示post  
  74.   
  75.  * @param email_to  
  76.   
  77.  * @param email_subject  
  78.   
  79.  * @param email_content  
  80.   
  81.  * @return  
  82.   
  83.  */  
  84.   
  85. @PostMapping("/sendClientpost")  
  86.   
  87. public String postsend(String email_to,String email_subject ,String email_content){  
  88.   
  89. try {  
  90.   
  91. Map<String,Object> map=new HashMap<String,Object>();  
  92.   
  93. map.put("email_to",email_to );  
  94.   
  95. map.put("email_subject", email_subject);  
  96.   
  97. map.put("email_content", email_content);  
  98.   
  99. sendMail.send(map);  
  100.   
  101. } catch (RestClientException e) {  
  102.   
  103. // TODO Auto-generated catch block  
  104.   
  105. e.printStackTrace();  
  106.   
  107. return "redirect:/error.html";  
  108.   
  109. }  
  110.   
  111. return "redirect:/suc.html";  
  112.   
  113. }  
  114.   
  115. /**  
  116.   
  117.  * 演示调用sendmail的/user/这个请求  
  118.   
  119.  * @return  
  120.   
  121.  */  
  122.   
  123. @ResponseBody  
  124.   
  125. @GetMapping("/invokeUser")  
  126.   
  127. public String invokeUser(String id){  
  128.   
  129. Map map=sendMail.getUser(id);  
  130.   
  131. return map.get("name").toString();  
  132.   
  133. }  
  134.   
  135. }  
  136.   
  137. 启动类  
  138.   
  139. package cn.et;  
  140.   
  141.    
  142.   
  143. import java.util.Map;  
  144.   
  145.    
  146.   
  147. import org.springframework.cloud.netflix.feign.FeignClient;  
  148.   
  149. import org.springframework.web.bind.annotation.GetMapping;  
  150.   
  151. import org.springframework.web.bind.annotation.PathVariable;  
  152.   
  153. import org.springframework.web.bind.annotation.PostMapping;  
  154.   
  155. import org.springframework.web.bind.annotation.RequestBody;  
  156.   
  157.    
  158.   
  159. @FeignClient("EMAILSERVER")  
  160.   
  161. public interface IsendMail {  
  162.   
  163. @GetMapping("/user/{userId}")  
  164.   
  165. public Map getUser(@PathVariable("userId") String userId);  
  166.   
  167. @PostMapping("/send")  
  168.   
  169. public String send(@RequestBody Map<String,Object> map);  
  170.   
  171. }  

猜你喜欢

转载自blog.csdn.net/qq_37928350/article/details/78997245
今日推荐