spring-cloud配置eureka服务端

Posted 叶家伟的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring-cloud配置eureka服务端相关的知识,希望对你有一定的参考价值。

spring-cloud配置eureka服务端

eureka用来发现其他程序

依赖

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>test</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        
        <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>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>

    </dependencies>

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

java

package test1.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableAutoConfiguration
@EnableEurekaServer
@SpringBootApplication
public class DemoApplication {
    
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

配置

---
# 单机版运行配置
spring:
  profiles: default
server:
  port: 9000  
eureka:
  instance:
    hostname: eureka # 这个hostname要在本地hosts文件中添加好
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/    
 

# 模拟的多个程序的配置
# 3个配置的运行命令如下
# java -jar -Dspring.profiles.active=c1 test-0.0.1-SNAPSHOT.jar
# java -jar -Dspring.profiles.active=c2 test-0.0.1-SNAPSHOT.jar
# java -jar -Dspring.profiles.active=c3 test-0.0.1-SNAPSHOT.jar
 
---
spring:
  profiles: c1
  application:
    name: eureka-server-clustered  
server:
  port: 9001  
eureka:
  instance:
    hostname: eureka1 
  client:
    registerWithEureka: false
    fetchRegistry: false       
    serviceUrl:
      defaultZone: http://eureka2:9002/eureka/,http:/eureka3:9003/eureka/
 
---
spring:
  profiles: c2
  application:
    name: eureka-server-clustered   
server:
  port: 9002
eureka:
  instance:
    hostname: eureka2
  client:
    registerWithEureka: false
    fetchRegistry: false       
    serviceUrl:
      defaultZone: http://eureka1:9001/eureka/,http://eureka3:9003/eureka/
 
---
spring:
  profiles: c3
  application:
    name: eureka-server-clustered  
server:
  port: 9003
eureka:
  instance:
    hostname: eureka3 
  client:
    registerWithEureka: false
    fetchRegistry: false   
    serviceUrl:
      defaultZone: http://eureka1:9001/eureka/,http://eureka2:9002/eureka/   

运行

运行 mvn install 将项目打包成jar
分别执行
java -jar -Dspring.profiles.active=c1 test-0.0.1-SNAPSHOT.jar
java -jar -Dspring.profiles.active=c2 test-0.0.1-SNAPSHOT.jar
java -jar -Dspring.profiles.active=c3 test-0.0.1-SNAPSHOT.jar

然后访问,即可看到其他两个程序
    http://eureka1:9001/
    http://eureka2:9002/
    http://eureka3:9003/

以上是关于spring-cloud配置eureka服务端的主要内容,如果未能解决你的问题,请参考以下文章

Spring-Cloud Eureka集群配置

spring-cloud配置eureka客户端

spring-cloud中eureka进行服务治理

spring-cloud: eureka之:ribbon负载均衡配置

spring-cloud中config配置中心使用(基于eureka的高可用)

spring-cloud: eureka之:ribbon负载均衡自定义配置