转载请注明出处 http://www.paraller.com
原文排版地址 点击跳转

spring:
application:

name: eureka-server-clustered3

profiles: peer3
server:
port: 9763
host: serverA
eureka:
instance:

hostname: eureka-peer3
preferIpAddress: true
instance-id: ${spring.cloud.client.ipAddress}:${server.port}:${spring.application.name}

client:

registerWithEureka: true
fetchRegistry: true
serviceUrl:defaultZone: http://${server.host}:9761/eureka/,http://${server.host}:9762/eureka/

##### 配置2: application.propertiesapplication-peer1.properties

eureka.client.fetchRegistry=true
eureka.client.registerWithEureka=true
eureka.client.serviceUrl.defaultZone=http://serverA:9762/eureka/,http://serverA:9763/eureka/
eureka.instance.hostname=serverA
eureka.instance.metadataMap.instanceId=${spring.application.name}\:${spring.application.instance_id:${random.value}}
eureka.server.waitTimeInMsWhenSyncEmpty=0
server.port=9761
spring.application.name=server_peer1

application-peer2.properties

eureka.client.fetchRegistry=true
eureka.client.registerWithEureka=true
eureka.client.serviceUrl.defaultZone=http://serverA:9761/eureka/,http://serverA:9763/eureka/
eureka.instance.hostname=serverA
eureka.server.waitTimeInMsWhenSyncEmpty=0
server.port=9762
eureka.instance.metadataMap.instanceId=${spring.application.name}\:${spring.application.instance_id:${random.value}}
spring.application.name=server_peer2


application-peer3.properties

eureka.client.fetchRegistry=true
eureka.client.registerWithEureka=true
eureka.client.serviceUrl.defaultZone=http://serverA:9761/eureka/,http://serverA:9762/eureka/
eureka.instance.hostname=serverA
eureka.server.waitTimeInMsWhenSyncEmpty=0
server.port=9763
eureka.instance.metadataMap.instanceId=${spring.application.name}\:${spring.application.instance_id:${random.value}}
spring.application.name=server_peer3


##### 本地跑测试的时候,需要编辑 etc/hosts

127.0.0.1 serverA


##### Dockerfile 文件

FROM java:7
VOLUME /tmp
ADD eureka-0.0.1-SNAPSHOT.jar /app.jar
RUN bash -c 'touch /app.jar'
EXPOSE 9762
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]


##### 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>org.demo</groupId>
<artifactId>eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging><name>Eureka Server</name>
<description>Eureka Server demo project</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.4.3.RELEASE</version><relativePath />
</parent>
<properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><!-- springBoot 程序的主入口 --><start-class>eurekademo.EurekaApplication</start-class> <java.version>1.7</java.version><docker.registry>docker.umiit.cn:5043</docker.registry></properties><profiles><profile><id>dev</id><properties><env>dev</env></properties><activation><activeByDefault>true</activeByDefault></activation></profile><profile><id>peer1</id><properties><env>peer1</env><docker>peer1</docker></properties></profile><profile><id>peer2</id><properties><env>peer2</env><docker>peer2</docker></properties></profile><profile><id>peer3</id><properties><env>peer3</env><docker>peer3</docker></properties></profile>
</profiles><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</artifactId><version>1.3.0.RELEASE</version></dependency><!-- <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>
</dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Camden.SR6</version><type>pom</type><scope>import</scope></dependency></dependencies>
</dependencyManagement>
<build><plugins><plugin><groupId>com.spotify</groupId><artifactId>docker-maven-plugin</artifactId><version>0.2.11</version><configuration><pushImage>true</pushImage><imageName>${docker.registry}/v3/${project.artifactId}:${docker}</imageName><dockerDirectory>src/main/resources_${env}/docker</dockerDirectory><resources><resource><targetPath>/</targetPath><directory>${project.build.directory}</directory><include>${project.build.finalName}.jar</include></resource></resources></configuration></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><!-- defined in spring-cloud-starter-parent pom (as documentation hint), but needs to be repeated here --><configuration><requiresUnpack><dependency><groupId>com.netflix.eureka</groupId><artifactId>eureka-core</artifactId></dependency><dependency><groupId>com.netflix.eureka</groupId><artifactId>eureka-client</artifactId></dependency></requiresUnpack></configuration></plugin><plugin><groupId>pl.project13.maven</groupId><artifactId>git-commit-id-plugin</artifactId><configuration><failOnNoGitDirectory>false</failOnNoGitDirectory></configuration></plugin><plugin><!--skip deploy (this is just a test module) --><artifactId>maven-deploy-plugin</artifactId><configuration><skip>true</skip></configuration></plugin><plugin><groupId>org.codehaus.mojo</groupId><artifactId>build-helper-maven-plugin</artifactId><executions><execution><id>add-resource</id><phase>initialize</phase><goals><goal>add-resource</goal></goals><configuration><resources><resource><directory>src/main/resources_${env}</directory></resource></resources></configuration></execution></executions></plugin></plugins>
</build><repositories><repository><id>spring-snapshots</id><name>Spring Snapshots</name><url>https://repo.spring.io/libs-snapshot-local</url><snapshots><enabled>true</enabled></snapshots></repository><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/libs-milestone-local</url><snapshots><enabled>false</enabled></snapshots></repository><repository><id>spring-releases</id><name>Spring Releases</name><url>https://repo.spring.io/libs-release-local</url><snapshots><enabled>false</enabled></snapshots></repository>
</repositories>
<pluginRepositories><pluginRepository><id>spring-snapshots</id><name>Spring Snapshots</name><url>https://repo.spring.io/libs-snapshot-local</url><snapshots><enabled>true</enabled></snapshots></pluginRepository><pluginRepository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/libs-milestone-local</url><snapshots><enabled>false</enabled></snapshots></pluginRepository>
</pluginRepositories>

</project>


##### .gitlab-ci.yml

image: docker.umiit.cn:5043/maven_docker:latest

stages:

  • build

run_build_peer1:
stage: build
tags:

- docker

only:

- develop

script:

- mvn clean package docker:build -Ppeer1

run_build_peer2:
stage: build
tags:

- docker

only:

- develop

script:

- mvn clean package docker:build -Ppeer2

run_build_peer3:
stage: build
tags:

- docker

only:

- develop

script:

- mvn clean package docker:build -Ppeer3

##### docker-compose.yml 

eureka_1:
image: docker.umiit.cn:5043/v3/eureka:peer1
volumes:

- /etc/localtime:/etc/localtime:ro
- /etc/timezone:/etc/timezone:ro
- /mnt/docker-data/logstash/eureka_1:/usr/local/tomcat/logs

environment:

spring.profiles.active: peer1

ports:

- "公网IP:9761:9761"

eureka_2:
image: docker.umiit.cn:5043/v3/eureka:peer2
volumes:

- /etc/localtime:/etc/localtime:ro
- /etc/timezone:/etc/timezone:ro
- /mnt/docker-data/logstash/eureka_2:/usr/local/tomcat/logs

environment:

spring.profiles.active: peer2

ports:

- "公网IP:9762:9762"

eureka_3:
image: docker.umiit.cn:5043/v3/eureka:peer3
volumes:

- /etc/localtime:/etc/localtime:ro
- /etc/timezone:/etc/timezone:ro
- /mnt/docker-data/logstash/eureka_2:/usr/local/tomcat/logs

environment:

spring.profiles.active: peer3

ports:

- "公网IP:9763:9763"

##### 项目结构

|-- src/main/resources_peer1
|-- src/main/resources_peer2
|-- src/main/resources_peer3

放置 Dockerfile & application.yml


##### 本地测试

mvn package

java -jar eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
java -jar eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2
java -jar eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer3

### 客户端配置##### application.yml

server:
port: ${vcap.application.port:9099}

eureka:
instance:

preferIpAddress: true

client:

serviceUrl:defaultZone: http://${serverA}:9761/eureka/,http://${serverA}:9762/eureka/,http://${serverA}:9763/eureka/
registerWithEureka: true
fetchRegistry: true

##### docker-compose.yml

ticketservice:
image: docker.umiit.cn:5043/v3/ticket-service:prep
volumes:

- /etc/localtime:/etc/localtime:ro
- /etc/timezone:/etc/timezone:ro
- /mnt/docker-data/logstash/ticketservice:/logs

external_links:

- common_mysql_1:mysqlDb
- common_redis_1:redisDb
- common_mongo_1:mongoDb

environment:

serverA: localhost
JVM_ARGS: -Xmx1024m

ports:

- "0.0.0.0:9099:9099"

yeaservice:
image: docker.umiit.cn:5043/v3/yea-service:ipc
volumes:

- /etc/localtime:/etc/localtime:ro
- /etc/timezone:/etc/timezone:ro
- /mnt/docker-data/logstash/yeaservice:/logs

external_links:

- common_mysql_1:mysqlDb
- common_redis_1:redisDb
- common_mongo_1:mongoDb

environment:

serverA: localhost
JVM_ARGS: -Xmx1024m

ports:

- "0.0.0.0:8081:8081"

hystrix-dashboard:
image: kennedyoliveira/hystrix-dashboard
ports:

- "0.0.0.0:7979:7979"

environment:

JVM_ARGS: -Xmx512m

##### yeaservice - Application.java

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableDiscoveryClient
@EnableCircuitBreaker
@EnableHystrixDashboard
@RibbonClient(name = "yea-ribbon", configuration = YeaRibbonConfiguration.class)
@ComponentScan(basePackages = { "com.**" })
public class Application {

public static void main(String[] args) {SpringApplication.run(Application.class, args);
}

}

##### yeaservice - 调用 ticketservice

@FeignClient(value = "ticket-service")
interface TicketClient {

}


这里使用了Feign的框架演示。### 查看结果

curl -i IP:9761/ # 在浏览器中输入网址


## 官方知识点### Authenticating with the Eureka ServerHTTP basic authentication will be automatically added to your eureka client if one of the eureka.client.serviceUrl.defaultZone URLs has credentials embedded in it (curl style, like http://user:password@localhost:8761/eureka). For more complex needs you can create a @Bean of type DiscoveryClientOptionalArgs and inject ClientFilter instances into it, all of which will be applied to the calls from the client to the server.### Zones希望在不同的Zone中配置你的客户端,首先要确认你的Eureka servers 部署在不同的Zone中,并且互相连接了,接下来要告诉Server你的服务实例在哪里,你可以使用`metadataMap`属性,例如像下面这样配置:##### Service 1 in Zone 1

eureka.instance.metadataMap.zone = zone1
eureka.client.preferSameZoneEureka = true


##### Service 1 in Zone 2

eureka.instance.metadataMap.zone = zone2
eureka.client.preferSameZoneEureka = true

NOTE
Because of a limitation in Eureka it isn’t possible to support per-server basic auth credentials, so only the first set that are found will be used.### 单例模式如果是单例模式的话,不必开启下面的选项,因为心跳检测会不断的进行,会报错连接不上。

eureka:
instance:

hostname: localhost

client:

registerWithEureka: false            ## this
fetchRegistry: false                ## this
serviceUrl:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

## 参考链接[如何实现微服务架构中的服务发现](http://www.jiagoushuo.com/article/1000415.html)[Microservice Registration and Discovery with Spring Cloud and Netflix's Eureka](https://spring.io/blog/2015/01/20/microservice-registration-and-discovery-with-spring-cloud-and-netflix-s-eureka)[Spring Cloud构建微服务架构(一)服务注册与发现](http://blog.didispace.com/springcloud1/)[Spring Cloud Netflix](http://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/1.2.7.RELEASE/)## QA **使用serverA 的域名配置,修改宿主机的  /etc/hosts ,不能集群连接**需要在docker容器中配置 /etc/hosts, 所以 vim Dockerfile 

RUN echo '10.45.189.178 serverA' >> /etc/hosts

重新启动,发现还是无效,cat /etc/hosts 没有写进serverA 的映射,后面推测应该是容器的/etc/hosts是动态生成的,所以这个语句无效。
从环境变量开始入手
docker-compose.yml 

environment:
serverA: 内网IP

高可用eureka服务发现实例相关推荐

  1. spring-cloud开发微服务笔记(二):高可用Eureka注册中心的搭建与RestTemplate和Fengin客户端调用微服务示例

    引言:这一篇博客是将上一篇spring-cloud-eureka-server的单机模式改为集群模式,体现eureka的高可用特性.生产环境无论是Eureka注册中心还是Client客户端大多是部署在 ...

  2. 浅谈SpringCloud (二) Eureka服务发现组件

    上面学习到了如何由一个程序访问另一个程序,那么如果使用SpringCloud来进行访问,该如何访问呐? 可以借助Eureka服务发现组件进行访问. 可以借助官方文档:https://spring.io ...

  3. 高可用 Redis 服务架构分析与搭建

    基于内存的Redis应该是目前各种web开发业务中最为常用的key-value数据库了,我们经常在业务中用其存储用户登陆态(Session存储),加速一些热数据的查询(相比较mysql而言,速度有数量 ...

  4. 如何搭建高可用Redis服务

    作者:漫步CODE人生 来自:cnblogs.com/scode2/p/8670980.html 题记 基于内存的Redis应该是目前各种web开发业务中最为常用的key-value数据库了,我们经常 ...

  5. 高可用Eureka注册中心配置说明(双机部署)

    目  录 1. 高可用EureKa注册中心示意图 2. Eureka实例相互注册配置 3. 微服务注册到Eureka配置 4. 启动步骤及配置成功检查 5. 说明事项 1. 高可用EureKa注册中心 ...

  6. 高可用Redis服务架构分析与搭建

    作者:HorstXu 原文:https://www.cnblogs.com/xuning/p/8464625.html 基于内存的Redis应该是目前各种Web开发业务中最为常用的Key-Value数 ...

  7. php redis 投票_高可用Redis服务架构分析与搭建

    HorstXuhttps://www.cnblogs.com/xuning/p/8464625.html 基于内存的Redis应该是目前各种web开发业务中最为常用的key-value数据库了,我们经 ...

  8. MySQL之MHA高可用配置及故障切换实例

    MySQL之MHA高可用配置及故障切换实例 前言 一.MHA概述 (1)MHA是什么 (2)MHA的组成 (3)MHA的特点 二.部署MHA (1)配置主从服务 ①环境初始化 ③修改master.sl ...

  9. 设计高可用Web服务

    转载请注明出处:设计高可用Web服务 高可用的设计可以说是web服务架构的目标,如果服务达不到高可用,万一出现故障将会对产品带来重大的负面影响.高可用的架构就是能够让服务在任何情况下都能正常响应,比如 ...

  10. 轻松实现基于Heartbeat的高可用web服务集群

    高可用集群就是为了保证某项服务能够时时在线,我们可以通过几个9来衡量一个高可用集群提供服务的稳定性,例如5个9的高可用集群必须保证服务一年在线的时间占99.999%,也就是说一年的时间中仅允许服务电线 ...

最新文章

  1. 【c语言】C语言配置文件解析库——iniparser
  2. JSP_运维_JSP项目部署到server(适合0经验新手)
  3. [ Python ] PIL
  4. grunt使用小记之uglify:最全的uglify使用DEMO
  5. TypeError at / 'AnonymousUser' object is not iterable
  6. mysql multi innobackupex,mysql之 Innobackupex(全备+增量)备份恢复
  7. 自动化运维之kickstart自动化部署安装操作系统
  8. MySQL 优化之 EXPLAIN 关键字
  9. @Component,@Bean
  10. 数据结构:最小生成树
  11. 批处理命令启动和关闭tomcat
  12. 数据挖掘概念与技术(第三版)课后答案——第五章
  13. 微信小程序系列(5)如何用微信小程序写一个论坛?贴心代码详解(三)列表页
  14. linux 监听 ipv6,zabbix 监控 ipv6
  15. vue+element-ui的列表查询条件/筛选条件组件二次封装(Vue项目)——内置去除前后空格功能
  16. Swift之属性的使用和实例展示
  17. 条形码扫描模块的作用是什么?有什么应用意义?
  18. java虚无世界_我的世界1.7.10工业虚无整合包
  19. FA-PEG-NHS 叶酸PEG活性酯
  20. 第二届金融交易技术大会拥抱Fin Tech-创新、科技、融合在沪圆满落幕!

热门文章

  1. 实现无缝滑屏怎么实现_无缝扩展人工智能以实现分布式大数据
  2. 脸部识别算法_面部识别技术是种族主义者吗? 先进算法的解释
  3. 贝叶斯公式的图形记忆
  4. 计算机数字媒体学什么以后,数字媒体设计是学什么的?以后的发展方向是什么?...
  5. 为什么不能线程调用类的成员函数_SLAM从0到1——13.SLAM中的多线程编程(2)
  6. Kubernetes 持久化存储是个难题,解决方案有哪些?\n
  7. 不小心rm删除文件怎么办
  8. 国内车载信息安全产业联盟成立
  9. php安装mamcache扩展时报错
  10. SVN报Previous operation has not finished; run 'cleanup'