**注册中心解决问题:
1微服务注册标识,服务之间通信便于发现
2 相同服务多实例部署,增加服务延续性**
1 pom配置
<!-- spring boot的parent 配置文件,有大部分spring boot需要用的Jar包 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
</properties>
<profiles>
<profile>
<id>250</id>
<properties>
<!-- 系统参数 -->
<instance.hostname>eureka-250</instance.hostname>
<instance.address>123.207.218.250</instance.address>
<serviceUrl.defaultZone>http://111.231.112.151:8001/eureka/</serviceUrl.defaultZone>
<error.subject>register-250</error.subject>
</properties>
</profile>
<profile>
<id>151</id>
<properties>
<!-- 系统参数 -->
<instance.hostname>eureka-151</instance.hostname>
<instance.address>111.231.112.151</instance.address>
<serviceUrl.defaultZone>http://123.207.218.250:8001/eureka/</serviceUrl.defaultZone>
<error.subject>register-151</error.subject>
</properties>
</profile>
</profiles>
<dependencies>
<!-- eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<!-- spring boot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring security -->
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency> -->
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- spring boot的maven打包插件 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>utf-8</encoding>
<useDefaultDelimiters>true</useDefaultDelimiters>
</configuration>
</plugin>
</plugins>
<finalName>register</finalName><!-- 包名 -->
</build>
2 程序入口
package com.ttd;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.google.common.collect.Maps;
@EnableEurekaServer
@SpringBootApplication
@RestController
public class ServiceCenterApplication {
@Autowired DiscoveryClient discoveryClient;
public static void main(String[] args) {
SpringApplication.run(ServiceCenterApplication.class, args);
}
@RequestMapping("/info")
public String info() {
List<String> services = discoveryClient.getServices();
Map<String, Object> re = Maps.newHashMap();
for(String temp : services){
re.put(temp, discoveryClient.getInstances(temp));
}
return re.toString();
}
}
3 应用配置
#\u6ce8\u518c\u670d\u52a1\u7684\u7aef\u53e3\u53f7
server.port=8001
spring.application.name=register
#eureka.instance.hostname=${instance.hostname}
eureka.instance.prefer-ip-address=true
eureka.instance.ip-address=${instance.address}
eureka.environment=product
#client register yes or no?
eureka.client.register-with-eureka=true
#Indicates whether this client should fetch eureka registry information from eureka\n server
eureka.client.fetch-registry=true
#cross register
eureka.client.serviceUrl.defaultZone=${serviceUrl.defaultZone}
#eureka server清理无效节点的时间间隔,默认60000毫秒,即60秒
eureka.servere.viction-interval-timer-in-ms=3000
#self-protected model
eureka.server.enable-self-preservation=true