SpringCloud第十篇:高可用的服务注册中心(Finchley版本)

       文章第一篇: 服务的注册与发现(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_8761SpringCloud_Server_8769SpringCloud_Client这三个工程

       在浏览器输入http://localhost:8761

       在浏览器输入http://localhost:8769

       我们在SpringCloud_Clientapplicaiton.properties里面只配置了一个注册地址,但是两个节点都存在服务

eureka.client.serviceUrl.defaultZone=http://peer1:8761/eureka/

        

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

快乐的小三菊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值