SpringCloud搭建Eureka集群

第一部分:搭建Eureka Server集群

Step1:新建工程,引入依赖

依赖文件pom.xml如下

 <?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.fangxing.springcloud</groupId>
<artifactId>euruka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>euruka-server</name>
<description>Demo project for Eureka Server</description> <!--基于Springboot-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
<relativePath/>
</parent> <properties>
<!--设置字符编码及java版本-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<!--增加eureka-server的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<!--用于测试的,本例可省略-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <!--依赖管理,用于管理spring-cloud的依赖,其中Camden.SR3是版本号-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

 

Step2:开启Eureka的注册中心

 /**
* 开启Eureka的注册中心,只需要添加这个注解即可
*/
@EnableEurekaServer
@SpringBootApplication
public class EurukaServerApplication { public static void main(String[] args) {
SpringApplication.run(EurukaServerApplication.class, args);
}
}

Step3:添加多实例的配置文件

既然是集群,1个实例就太寒碜了,所以我们通过配置文件制定了多个实例。

application-peer1.properties文件内容如下:

 spring.application.name=eureka-server
server.port=1111 eureka.instance.hostname=peer1
eureka.client.serviceUrl.defaultZone=http://peer2:2222/eureka/

application-peer2.properties文件内容如下:

 spring.application.name=eureka-server
server.port=2222 eureka.instance.hostname=peer2
eureka.client.serviceUrl.defaultZone=http://peer1:1111/eureka/

其中:eureka.client.serviceUrl.defaultZone指的是注册中心的地址,所以两个实例互相指向。

PS:这里的peer1、peer2需要再host中配置一番,如下所示:

 127.0.0.1 peer1
127.0.0.1 peer2

Step4:启动注册中心的多实例

再idea中配置多实例的启动参数,如下所示:

SpringCloud搭建Eureka集群

配置好了,依次启动peer1和peer2

SpringCloud搭建Eureka集群

 Step5:Eureka的注册中心去验证

SpringCloud搭建Eureka集群

第二部分:搭建Eureka Client集群

 Step1:新建工程,引入配置文件

我测试时的pom.xml如下:

 <?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.fangxing.springcloud</groupId>
<artifactId>euruka-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>euruka-server</name>
<description>Demo project for Eureka Server</description> <!--基于Springboot-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
<relativePath/>
</parent> <properties>
<!--设置字符编码及java版本-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<!--增加eureka-server的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!--用于测试的,本例可省略-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <!--依赖管理,用于管理spring-cloud的依赖,其中Camden.SR3是版本号-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

Eureka Client的pom.xml

Step2:启动发现服务

 @EnableDiscoveryClient
@SpringBootApplication
public class EurekalclientApplication { public static void main(String[] args) {
SpringApplication.run(EurekalclientApplication.class, args);
}
}

Step3:配置Client的应用

 spring.application.name=eureka-client

 eureka.client.serviceUrl.defaultZone=http://peer1:1111/eureka/,http://peer2:2222/eureka/

提供一个测试的Controller。

 @RestController
public class HelloController { @Autowired
private DiscoveryClient client; @RequestMapping(value = "/hello",method = RequestMethod.GET)
public String index(){
ServiceInstance instance=client.getLocalServiceInstance();
System.out.println("/hello,host:"+instance.getHost()+",service_id:"+instance.getServiceId());
return "hello world!";
} }

Step4:启动多个实例

因为这里的多个实例,我们需要不同的端口,所以,我们还是采用前面的方法来启动多个实例。

SpringCloud搭建Eureka集群

启动后,我们可以在Eureka的注册中心中可以看到(Peer1和Peer2都可以看到)

SpringCloud搭建Eureka集群

PS:这里localhost:eureka-client这个是测试时残留的,这里没有清除掉,可以不用管它。

到这里,我们的服务注册中心和服务提供者都写好了。

第三部分:消费Eureka Client的消息

Step1:新建工程,引入依赖

注意,这里我们要引入新的依赖ribbon。

 <?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.fangxing.springcloud</groupId>
<artifactId>euruka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>euruka-server</name>
<description>Demo project for Eureka Server</description> <!--基于Springboot-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
<relativePath/>
</parent> <properties>
<!--设置字符编码及java版本-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<!--增加eureka-server的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!--增加ribbon的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<!--用于测试的,本例可省略-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <!--依赖管理,用于管理spring-cloud的依赖,其中Camden.SR3是版本号-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

Eureka消费者的pom.xml

 Step2:启用发现服务并开启客户端负载均衡

 @EnableDiscoveryClient
@SpringBootApplication
public class RibbonConsumerApplication { public static void main(String[] args) {
SpringApplication.run(RibbonConsumerApplication.class, args);
} //开启客户端负载均衡
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
}

Step3:配置消费者(跟生产者类似)

 spring.application.name=ribbon-consumer
server.port=9000 eureka.client.serviceUrl.defaultZone=http://peer1:1111/eureka/,http://peer2:2222/eureka/

Step4:编写消费者实例

 @RestController
public class ConsumerController { @Autowired
RestTemplate restTemplate; @RequestMapping(value = "/ribbon-consumer",method = RequestMethod.GET)
public String helloConsumer(){
return restTemplate.getForEntity("http://eureka-client/hello",String.class).getBody();
} }

Step5:验证Eureka注册中心

SpringCloud搭建Eureka集群

 Step6:验证请求

SpringCloud搭建Eureka集群

参考文档:

《SpringCloud 微服务实践》

源码:https://github.com/iprometheus/spring-cloud-eureka-test

上一篇:Laravel 学习笔记之数据库操作——Eloquent ORM


下一篇:spring框架学习之--数据库操作增删改查