原文地址为:Spring Cloud_4_Eureka集群搭建
转载请注明本文地址:Spring Cloud_4_Eureka集群搭建
Eureka集群搭建
- 搭建Eureka集群
1、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
server: port: 8761 spring: application: name: first-cloud-server profiles: slave1 eureka: instance: hostname: slave1 client: serviceUrl: defaultZone: http://slave2:8762/eureka/ --- server: port: 8762 spring: application: name: first-cloud-server profiles: slave2 eureka: instance: hostname: slave2 client: serviceUrl: defaultZone: http://slave1:8761/eureka/
- 配置了两个profiles,分别为slave1和slave2
- 在slave1中,配置了应用端口8761,主机名为slave1,当使用 slave1这个profiles启动服务器时,将会向http://slave2:8762/eureka注册自己
- 使用slave2来启动服务器时,会向http://slave1:8761/eureka注册自己
- 简单点说 , 就是两个服务器启动后 , 它们会互相注册 。
2.2、修改启动类
- 让类在启动时,读取控制台的输入,决定使用那个profiles来启动服务器
package com.atm.cloud; import java.util.Scanner; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class MyApplicationServer { public static void main(String[] args) { //读取控制台输入,决定使用哪个profiles Scanner scanner=new Scanner(System.in); String profiles=scanner.nextLine(); //SpringApplication.run(MyApplicationServer.class, args); new SpringApplicationBuilder(MyApplicationServer.class).profiles(profiles).run(args); } }
- 启动类中,先读取控制台的输入,再调用profiles方法来设置启动的profiles
- 启动服务器会抛出异常, 异常原因在上一节已经阐述,此处不再理会







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

3.1、修改配置文件
spring: application: name: first-cloud-provider eureka: instance: hostname: localhost client: serviceUrl: defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
- 注意注册的application.name为: first-cloud-provider
3.2、修改控制器
- 启动类使用了profiles方法来设置启动端口
- 为了能看到效果,还需要改造控制器,将服务调用者请求的URL保存起来并返回
package com.atm.cloud; import javax.servlet.http.HttpServletRequest; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class MyController { @RequestMapping(value = "/person/{personId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public Person findPerson(@PathVariable("personId")Integer personId,HttpServletRequest request){ Person person=new Person(); person.setId(personId); person.setAge(18); person.setName(request.getRequestURL().toString()); return person; } }
- 控制器的findPerson方法,将请求的URL保存到Person实例的name属性中,调用服务后,可以通过name属性来查看请求的URL
3.3、修改启动类
- 为了避免端口冲突,启动时读取控制台输入,决定使用哪个端口来启动
package com.atm.cloud; import java.util.Scanner; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class MyApplicationProvider { public static void main(String[] args) { //读取控制台输入,避免端口冲突 Scanner scanner=new Scanner(System.in); String port=scanner.nextLine(); new SpringApplicationBuilder(MyApplicationProvider.class).properties( "server.port=" + port).run(args); } }




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

4.1、修改配置文件
- 修改的配置,将服务调用注册到两个服务器上
server: port: 9000 spring: application: name: first-cloud-invoker eureka: instance: hostname: localhost client: serviceUrl: defaultZone: http://localhost:8761/eureka/,http:// localhost:8761/eureka/
4.2、修改控制器
package com.atm.cloud; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController @Configuration public class InvokerController { @Bean @LoadBalanced public RestTemplate getRestTemplate() { return new RestTemplate(); } @RequestMapping(value="/router",method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE) public String router() { RestTemplate restTemplate = getRestTemplate(); // 根据应用名称调用服务 String json = restTemplate.getForObject( "http://first-cloud-provider/person/1", String.class); return json; } }

5、REST客户端
- 本案例使用的是HttpClient,HttpClient是Apache提供的一个Http工具包
- 新建Maven项目:atm_eureka_REST_client
5.1、引入依赖
<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"> <modelVersion>4.0.0</modelVersion> <groupId>com.atm.cloud</groupId> <artifactId>atm_eureka_REST_client</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>atm_eureka_REST_client Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- HttpClient --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.2</version> </dependency> </dependencies> <build> <finalName>atm_eureka_REST_client</finalName> </build> </project>
5.2、新建启动类
package atm_eureka_REST_client; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; public class MyApplicationRESTClient { public static void main(String[] args) { //创建默认的HttpClient CloseableHttpClient client=HttpClients.createDefault(); //调用6次服务并输出结果 for (int i = 0; i < 6; i++) { //调用GET方法请求服务 HttpGet httpGet=new HttpGet("http://location:9000/router"); try { //获取响应 HttpResponse response=client.execute(httpGet); //根据响应解析出字符串 System.out.println(EntityUtils.toString(response.getEntity())); } catch (Exception e){ System.out.println("Exception --->>>"+e); } } } }
- 在main方法,调用了6次9000端口的router服务并输出结果
- 启动两个服务端,控制端分别输入“slave1”和“slave2”
- 启动两个服务提供者,控制台分别输入“8081”和“8082”
- 启动服务调用者
- 运行MyApplicationRESTClient的main方法


- 根据输出内容可知:8081与8082端口,分别被请求了3次,可见已经达成了负载均衡的目的
- 关于负载均衡更详细的内容,将在后面章节中讲解
转载请注明本文地址:Spring Cloud_4_Eureka集群搭建