SpringCloud 学习(3):eureka集群
*这篇文章基于上篇文章进行修改,黄色背景为修改的部分。
1.介绍:Eureka集群就是n个Eureka服务器+n个Eureka客户端,目的是实现负载均衡,减轻单个服务器的压力。
2.原理:
3.代码实现
- Eureka服务器(建立项目consultingPlatform_server):
1).pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
2)
.application.properties
(我们通过profiles指定不同的配置文件,模拟不同的服务器)|
spring.profiles.active=server1
#spring.profiles.active=server2
.application-server1.properties
server.port=1001
#服务名称
spring.application.name=server
#eureka服务器路径
eureka.client.service-url.defaultZone=http://localhost:1002/eureka/
.application-server2.properties
server.port=1002
#服务名称
spring.application.name=server
#eureka服务器路径
eureka.client.service-url.defaultZone=http://localhost:1001/eureka/
在这里两台Eureka服务器互相注册。。ok,到这里Eureka集群服务器已经搭建完毕,通过修改profiles分别启动两个Eureka服务器。
3).Application.java(启动类)
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
- 服务提供者(创建项目police_client)
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
2)
.application.properties(我们通过profiles指定不同的配置文件,模拟不同的客户端)
spring.profiles.active=client1
#spring.profiles.active=client2
.application-client1.propertiesserver.port=2001
#服务名称
spring.application.name=police
#eureka服务器路径
eureka.client.service-url.defaultZone=http://localhost:1001/eureka/,http://localhost:1002/eureka/
.application-client2.propertiesserver.port=2002
#服务名称
spring.application.name=police
#eureka服务器路径
eureka.client.service-url.defaultZone=http://localhost:1001/eureka/,http://localhost/eureka/
3 ).Application.java
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @author: lqh
* @description:
* @program: police_client
* @create: 2018-06-06 11:50
**/
@SpringBootApplication
@EnableEurekaClient
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
4 ).
PoliceController.java
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author:
* @description:
* @program: police_client
* @create: 2018-06-06 11:59
**/
@RestController
public class PoliceController {
@RequestMapping("call")
public String call(HttpServletRequest request) {
//这里返回url,便于查看服务提供者。
return "call:" + request.getRequestURL().toString();
}
}
- 服务调用者(创建项目people_caller):
- 1).pom.xml
2.application.properties<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.13.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Edgware.SR3</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> </dependencies>
3.Applicationserver.port=6003 #服务名称 spring.application.name=people #注册路径 eureka.client.service-url.defaultZone=http://localhost:6001/eureka/
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class Application { public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); } }
4.PeopleControllerimport org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @Configuration @RestController public class PeopleController { @Bean @LoadBalanced RestTemplate getRestTemplate() { return new RestTemplate(); } @RequestMapping("call") public String call() { return getRestTemplate().getForObject("http://police/call",String.class); } }
OK,跑起来。。
- #图一:可以看到有2个服务器,2个服务提供者,1个服务调用者
- #图二:可以看到有2个服务器,2个服务提供者,1个服务调用者
- #图三:服务调用者,可以看到端口一直在2001\2002之间切换,说明一直在调用不同的服务提供者。