文章第一篇: 服务的注册与发现(Eureka) 介绍了服务注册与发现,其中服务注册中心Eureka Server,是一个实例,当成千上万个服务向它注册的时候,它的负载是非常高的,这在生产环境上是不太合适的,这篇文章主要介绍怎么将Eureka Server集群化。
一、准备工作
Eureka通过运行多个实例,使其更具有高可用性。事实上,这是它默认的属性,你需要做的就是给对等的实例一个合法的关联serviceurl。这篇文章主要是对第一篇的文章进行改造。
修改hosts文件,进入到目录C:\Windows\System32\drivers\etc下,编辑hosts文件,添加两个域名,如果不添加这两个,就不能模拟单机多个节点了。
二、改造工作
1、新建工程SpringCloud_Server_8761
pom.xml内容如下:
<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>
<artifactId>SpringCloud_Server_8761</artifactId>
<packaging>jar</packaging>
<name>SpringCloud_Server_8761</name>
<parent>
<groupId>com</groupId>
<artifactId>SpringCloud_Common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
application.properties内容如下:
server.port=8761
eureka.instance.hostname=peer1
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
eureka.client.serviceUrl.defaultZone=http://peer2:8769/eureka/
EurekaServerApplication内容如下:
package com.zit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/*启动一个服务注册中心*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
2、新建工程SpringCloud_Server_8769
pom.xml内容如下:
<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>
<artifactId>SpringCloud_Server_8769</artifactId>
<packaging>jar</packaging>
<name>SpringCloud_Server_8769</name>
<parent>
<groupId>com</groupId>
<artifactId>SpringCloud_Common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
application.properties内容如下:
server.port=8769
eureka.instance.hostname=peer2
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
eureka.client.serviceUrl.defaultZone=http://peer1:8761/eureka/
EurekaServerApplication内容如下:
package com.zit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/*启动一个服务注册中心*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
3、新建工程SpringCloud_Client
pom内容如下:
<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>
<artifactId>SpringCloud_Client</artifactId>
<packaging>jar</packaging>
<name>SpringCloud_Client</name>
<parent>
<groupId>com</groupId>
<artifactId>SpringCloud_Common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
application.properties内容如下:
server.port=8762
eureka.client.serviceUrl.defaultZone=http://peer1:8761/eureka/
spring.application.name=service-hi
EurekaClientApplicationOne内容如下:
package com.zit;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/*@EnableEurekaClient表明自己是个eureka客户端*/
@SpringBootApplication
@EnableEurekaClient
@RestController
public class EurekaClientApplicationOne {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplicationOne.class, args);
}
@Value("${server.port}")
String port;
@RequestMapping("/hi")
public String hi(@RequestParam String name) {
return "hi"+name+",i am from port:"+port;
}
}
三、启动工程
依次启动SpringCloud_Server_8761、SpringCloud_Server_8769和SpringCloud_Client这三个工程
在浏览器输入http://localhost:8761
在浏览器输入http://localhost:8769
我们在SpringCloud_Client的applicaiton.properties里面只配置了一个注册地址,但是两个节点都存在服务
eureka.client.serviceUrl.defaultZone=http://peer1:8761/eureka/