代码改变世界

Euerka环境搭建

2018-08-01 02:06  DoPeter  阅读(600)  评论(0编辑  收藏  举报

 

机器环境

  windows10,IntelliJ IDEA

配置host

  

 

单节点Eureka

一、pom文件

<?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>peter.test</groupId>
    <artifactId>singleeureka</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>singleeureka</name>
    <description>Demo project for Spring Cloud Eureka</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>10</java.version>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <!--
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
-->
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.0</version>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <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>


</project>
View Code

 

<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>

 在IDEA中,直接启动调试,会报错,添加进这个依赖就可以执行了。

注释掉 spring-boot-starter-test 是因为暂时不需要单元测试。

 

二、application.properties文件

spring.application.name=spring-cloud-eureka
server.port=9232
eureka.instance.hostname=peterhost1
eureka.client.service-url.defaultZone=http://peterhost2:9232/eureka/

debug=true
View Code

 

三、启动注解

@EnableEurekaServer
@SpringBootApplication
public class SingleeurekaApplication {

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

 

四、运行

  打包

    mvn clean package

  运行并访问

 

 

多节点Euerka

 

一、pom文件

同单节点一样

 

二、application.yml文件

使用yml文件可以在命令启动时指定profile

---
spring:
  application:
    name: spring-cloud-eureka
  profiles: peterhost1
server:
  port: 9000
eureka:
  instance:
    hostname: peterhost1
  client:
    service-url:
      defaultZone: http://peterhost2:9001/eureka/,http://peterhost3:9002/eureka/
---
spring:
  application:
    name: spring-cloud-eureka
  profiles: peterhost2
server:
  port: 9001
eureka:
  instance:
    hostname: peterhost2
  client:
    service-url:
      defaultZone: http://peterhost3:9002/eureka/,http://peterhost1:9000/eureka/
---
spring:
  application:
    name: spring-cloud-eureka
  profiles: peterhost3
server:
  port: 9002
eureka:
  instance:
    hostname: peterhost3
  client:
    service-url:
      defaultZone: http://peterhost1:9000/eureka/,http://peterhost2:9001/eureka/
View Code

 

作为高可用,第1个节点指定默认的zone为第2个和第3个节点,以此类推。

 

三、启动注解

@EnableEurekaServer
@SpringBootApplication
public class ThreeeurekaApplication {

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

 

四、运行

  打包

    同上

  运行并访问

  

java -jar 路径\threeeureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=peterhost1
java -jar 路径\threeeureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=peterhost2
java -jar 路径\threeeureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=peterhost3
View Code

 

 

 

配置文件中是按照hostname进行配置的,有时我们想直接使用ip,可如下设置:

  eureka.instance.prefer-ip-address=true

  eureka.ip-address=xxx.xxx.xxx.xxx

这样在Producer或者Consumer获取Eureka中的服务后,如使用Fegin等组件时则会使用ip进行访问。