springCloud注册中心Eureka
1 注册中心配置
1.1pom 文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.duran</groupId>
<artifactId>eureka1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka1</name>
<description>eureka1</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR3</version><!-- eureka版本 -->
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
1.2yml文件
server:
port: 8081
eureka:
instance:
hostname: localhost
lease-renewal-interval-in-seconds: 10 #客户端向服务器(注册中心发送心跳的时间间隔)
lease-expiration-duration-in-seconds: 120 #服务器(注册中心)租期到期的时间, 也就是说服务器在收到最后一次心跳的时间上线
client:
registerWithEureka: false #自己不注册到注册中心上
fetchRegistry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
1.3启动类
@SpringBootApplication
@EnableEurekaServer
public class Eureka1Application {
public static void main(String[] args) {
SpringApplication.run(Eureka1Application.class, args);
}
}
2服务端配置
2.1pom文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.duran</groupId>
<artifactId>client1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>client1</name>
<description>client1</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR3</version><!-- eureka版本 -->
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2.2yml文件
spring:
application:
name: consumer-service
server:
context-path: /consumer
port: 8083
eureka:
instance:
hostname: localhost
client:
service-url:
defaultZone: http://127.0.0.1:8081/eureka/
2.3启动类
@EnableEurekaClient
@SpringBootApplication
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
}
双注册中心 高可用
注册中心1 yml配置
server:
port: 8081
eureka:
instance:
hostname: localhost
lease-renewal-interval-in-seconds: 10 #客户端向服务器(注册中心发送心跳的时间间隔)
lease-expiration-duration-in-seconds: 120 #服务器(注册中心)租期到期的时间, 也就是说服务器在收到最后一次心跳的时间上线
client:
service-url:
defaultZone: http://127.0.0.1:8082/eureka/ #互相注册到对方--高可用服务发现
注册中心2 yml配置
server:
port: 8082
eureka:
instance:
hostname: localhost
lease-renewal-interval-in-seconds: 10 #客户端向服务器(注册中心发送心跳的时间间隔)
lease-expiration-duration-in-seconds: 120 #服务器(注册中心)租期到期的时间, 也就是说服务器在收到最后一次心跳的时间上线
client:
service-url:
defaultZone: http://127.0.0.1:8081/eureka/ #互相注册到对方--高可用服务发现
服务端yml配置文件
spring:
application:
name: consumer-service
server:
context-path: /consumer
port: 8083
eureka:
instance:
hostname: localhost
client:
service-url:
defaultZone: http://localhost:8081/eureka/,http://localhost:8082/eureka/