转载请注明出处 http://www.paraller.com
原文排版地址 点击跳转html
spring:
application:java
name: eureka-server-clustered3
profiles: peer3
server:
port: 9763
host: serverA
eureka:
instance:mysql
hostname: eureka-peer3 preferIpAddress: true instance-id: ${spring.cloud.client.ipAddress}:${server.port}:${spring.application.name}
client:git
registerWithEureka: true fetchRegistry: true serviceUrl: defaultZone: http://${server.host}:9761/eureka/,http://${server.host}:9762/eureka/
##### 配置2: application.properties application-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_peer1redis
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_peer2spring
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_peer3sql
##### 本地跑测试的时候,须要编辑 etc/hosts
127.0.0.1 serverAdocker
##### 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"]apache
##### 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"vim
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:
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
##### 本地测试
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 Server HTTP 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