Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 460|回复: 0

[默认分类] Spring Cloud_4_Eureka集群搭建

[复制链接]
  • TA的每日心情
    开心
    2021-12-13 21:45
  • 签到天数: 15 天

    [LV.4]偶尔看看III

    发表于 2018-6-18 11:18:14 | 显示全部楼层 |阅读模式

    Eureka集群搭建

      搭建Eureka集群
      
    1、Eureka集群搭建

      在介绍Eureka的时候,服务器实例、服务提供者实例都是只启动了一个,并没有体现高可用性
      本小节将对前面的Eureka应用进行改造,使其可以进行集群部署
      
    1.1、集群结构图

      上节改造前
      


      本案例将运行两个服务器实例、两个服务提供者实例,然后服务调用者请求服务
      本节改造后
      


      Eureka服务器A,Eureka服务器B(114电话查询平台A接线员,B接线员)
    服务提供者A,服务提供者B(多人向警察局拨打电话,人手不足,警察局也提供了A,B接线员)
    Eureka客户端服务调用者(不在乎有多少接线员,只知道可以向114平台查询到对应想要的电话号码)


      在上一节中的Eureka应用,使用的是浏览器访问Eureka的服务调用者
      改造之后,为了能看到负载均衡的效果,会编写一个HttpClient的REST客户端访问服务调用者发布的服务
      本节的开发环境只有一台电脑,操作系统为Windows,如果要构建集群,需要修改hosts文件,为其添加主机名的映射
      修改C:\Windows\System32\drivers\etc\hosts文件,添加127.0.0.1 slave1 slave2
      

    2、改造服务端

      新建Maven项目:atm_eureka_server_AB
      使用maven配置与上一节一致
      

    2.1、修改配置文件

      由于需要对同一个应用启动两次,因此需要在配置文件中使用profiles
      
    1. [code]server:
    2. port: 8761
    3. spring:
    4. application:
    5. name: first-cloud-server
    6. profiles: slave1
    7. eureka:
    8. instance:
    9. hostname: slave1
    10. client:
    11. serviceUrl:
    12. defaultZone: http://slave2:8762/eureka/ ---
    13. server:
    14. port: 8762
    15. spring:
    16. application:
    17. name: first-cloud-server
    18. profiles: slave2
    19. eureka:
    20. instance:
    21. hostname: slave2
    22. client:
    23. serviceUrl:
    24. defaultZone: http://slave1:8761/eureka/
    复制代码
    [/code]

      配置了两个profiles,分别为slave1和slave2
      在slave1中,配置了应用端口8761,主机名为slave1,当使用 slave1这个profiles启动服务器时,将会向http://slave2:8762/eureka注册自己
      使用slave2来启动服务器时,会向http://slave1:8761/eureka注册自己
      简单点说 , 就是两个服务器启动后 , 它们会互相注册 。
      
    2.2、修改启动类

      让类在启动时,读取控制台的输入,决定使用那个profiles来启动服务器
      
    1. [code]package com.atm.cloud;
    2. import java.util.Scanner;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. import org.springframework.boot.builder.SpringApplicationBuilder;
    5. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    6. @SpringBootApplication
    7. @EnableEurekaServer
    8. public class MyApplicationServer {
    9.     public static void main(String[] args) {
    10.         //读取控制台输入,决定使用哪个profiles
    11.         Scanner scanner=new Scanner(System.in);
    12.         String profiles=scanner.nextLine();
    13.         //SpringApplication.run(MyApplicationServer.class, args);
    14.         new SpringApplicationBuilder(MyApplicationServer.class).profiles(profiles).run(args);
    15.     }
    16. }
    复制代码
    [/code]

      启动类中,先读取控制台的输入,再调用profiles方法来设置启动的profiles
      启动服务器会抛出异常, 异常原因在上一节已经阐述,此处不再理会
      



















    3、改造服务提供者

      服务提供者也需要启动两个实例
      服务提供者的改造与服务端类似
      复制或者新建上一节中“服务提供者”,Maven项目:atm_eureka_provider_AB
      

    3.1、修改配置文件
    1. [code]spring:
    2. application:
    3.   name: first-cloud-provider
    4. eureka:
    5. instance:
    6.   hostname: localhost
    7. client:
    8.   serviceUrl:
    9.    defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
    复制代码
    [/code]

      注意注册的application.name为: first-cloud-provider
      
    3.2、修改控制器

      启动类使用了profiles方法来设置启动端口
      为了能看到效果,还需要改造控制器,将服务调用者请求的URL保存起来并返回
      
    1. [code]package com.atm.cloud;
    2. import javax.servlet.http.HttpServletRequest;
    3. import org.springframework.http.MediaType;
    4. import org.springframework.web.bind.annotation.PathVariable;
    5. import org.springframework.web.bind.annotation.RequestMapping;
    6. import org.springframework.web.bind.annotation.RequestMethod;
    7. import org.springframework.web.bind.annotation.RestController;
    8. @RestController
    9. public class MyController {
    10.     @RequestMapping(value = "/person/{personId}", method = RequestMethod.GET,
    11.             produces = MediaType.APPLICATION_JSON_VALUE)
    12.     public Person findPerson(@PathVariable("personId")Integer personId,HttpServletRequest request){
    13.         Person person=new Person();
    14.         person.setId(personId);
    15.         person.setAge(18);
    16.         person.setName(request.getRequestURL().toString());
    17.         return person;
    18.     }
    19. }
    复制代码
    [/code]

      控制器的findPerson方法,将请求的URL保存到Person实例的name属性中,调用服务后,可以通过name属性来查看请求的URL
      
    3.3、修改启动类

      为了避免端口冲突,启动时读取控制台输入,决定使用哪个端口来启动
      
    1. [code]package com.atm.cloud;
    2. import java.util.Scanner;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. import org.springframework.boot.builder.SpringApplicationBuilder;
    5. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    6. @SpringBootApplication
    7. @EnableEurekaClient
    8. public class MyApplicationProvider {
    9.     public static void main(String[] args) {
    10.         //读取控制台输入,避免端口冲突
    11.         Scanner scanner=new Scanner(System.in);
    12.         String port=scanner.nextLine();
    13.         new SpringApplicationBuilder(MyApplicationProvider.class).properties(
    14.                 "server.port=" + port).run(args);
    15.     }
    16. }
    复制代码
    [/code]










    4、改造服务调用者

      本案例中的服务调用只需要启动一个实例
      复制/新建上一节的“服务提供者”,Maven项目:atm_eureka_invoker_AB
      

    4.1、修改配置文件

      修改的配置,将服务调用注册到两个服务器上
      
    1. [code]server:
    2. port: 9000
    3. spring:
    4. application:
    5.   name: first-cloud-invoker
    6. eureka:
    7. instance:
    8.   hostname: localhost
    9. client:
    10.   serviceUrl:
    11.    defaultZone: http://localhost:8761/eureka/,http:// localhost:8761/eureka/
    复制代码
    [/code]
    4.2、修改控制器
    1. [code]package com.atm.cloud;
    2. import org.springframework.cloud.client.loadbalancer.LoadBalanced;
    3. import org.springframework.context.annotation.Bean;
    4. import org.springframework.context.annotation.Configuration;
    5. import org.springframework.http.MediaType;
    6. import org.springframework.web.bind.annotation.RequestMapping;
    7. import org.springframework.web.bind.annotation.RequestMethod;
    8. import org.springframework.web.bind.annotation.RestController;
    9. import org.springframework.web.client.RestTemplate;
    10. @RestController
    11. @Configuration
    12. public class InvokerController {
    13.     @Bean
    14.     @LoadBalanced
    15.     public RestTemplate getRestTemplate() {
    16.         return new RestTemplate();
    17.     }
    18.     @RequestMapping(value="/router",method=RequestMethod.GET,
    19.             produces=MediaType.APPLICATION_JSON_VALUE)
    20.     public String router() {
    21.         RestTemplate restTemplate = getRestTemplate();
    22.         // 根据应用名称调用服务
    23.         String json = restTemplate.getForObject(
    24.                 "http://first-cloud-provider/person/1", String.class);
    25.         return json;
    26.     }
    27. }
    复制代码
    [/code]

    5、REST客户端

      本案例使用的是HttpClient,HttpClient是Apache提供的一个Http工具包
      新建Maven项目:atm_eureka_REST_client
      
    5.1、引入依赖
    1. [code]<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    2.     <modelVersion>4.0.0</modelVersion>
    3.     <groupId>com.atm.cloud</groupId>
    4.     <artifactId>atm_eureka_REST_client</artifactId>
    5.     <packaging>war</packaging>
    6.     <version>0.0.1-SNAPSHOT</version>
    7.     <name>atm_eureka_REST_client Maven Webapp</name>
    8.     <url>http://maven.apache.org</url>
    9.     <dependencies>
    10.         <dependency>
    11.             <groupId>junit</groupId>
    12.             <artifactId>junit</artifactId>
    13.             <version>3.8.1</version>
    14.             <scope>test</scope>
    15.         </dependency>
    16.         <!-- HttpClient -->
    17.         <dependency>
    18.             <groupId>org.apache.httpcomponents</groupId>
    19.             <artifactId>httpclient</artifactId>
    20.             <version>4.5.2</version>
    21.         </dependency>
    22.     </dependencies>
    23.     <build>
    24.         <finalName>atm_eureka_REST_client</finalName>
    25.     </build>
    26. </project>
    复制代码
    [/code]
    5.2、新建启动类
    1. [code]package atm_eureka_REST_client;
    2. import org.apache.http.HttpResponse;
    3. import org.apache.http.client.methods.HttpGet;
    4. import org.apache.http.impl.client.CloseableHttpClient;
    5. import org.apache.http.impl.client.HttpClients;
    6. import org.apache.http.util.EntityUtils;
    7. public class MyApplicationRESTClient {
    8.     public static void main(String[] args) {
    9.         //创建默认的HttpClient
    10.         CloseableHttpClient client=HttpClients.createDefault();
    11.         //调用6次服务并输出结果
    12.         for (int i = 0; i < 6; i++) {
    13.             //调用GET方法请求服务
    14.             HttpGet httpGet=new HttpGet("http://location:9000/router");
    15.             try {
    16.                 //获取响应
    17.                 HttpResponse response=client.execute(httpGet);
    18.                 //根据响应解析出字符串
    19.                 System.out.println(EntityUtils.toString(response.getEntity()));
    20.             } catch (Exception e){
    21.                 System.out.println("Exception --->>>"+e);
    22.             }
    23.         }
    24.     }
    25. }
    复制代码
    [/code]

      在main方法,调用了6次9000端口的router服务并输出结果
      



      启动两个服务端,控制端分别输入“slave1”和“slave2”
      启动两个服务提供者,控制台分别输入“8081”和“8082”
      启动服务调用者
      运行MyApplicationRESTClient的main方法
      





      根据输出内容可知:8081与8082端口,分别被请求了3次,可见已经达成了负载均衡的目的
      关于负载均衡更详细的内容,将在后面章节中讲解
      

    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-4-25 06:58 , Processed in 0.415473 second(s), 48 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表