美文网首页
eureka实现服务中心

eureka实现服务中心

作者: Angle_洛熙 | 来源:发表于2020-09-16 22:09 被阅读0次

Spring Cloud 封装了 Netflix 公司开发的 Eureka 模块来实现服务注册和发现。Eureka 采用了 C-S 的设计架构。Eureka Server 作为服务注册功能的服务器。
但是Eureka在2018年已经不在进行维护,主流使用consul多一些,后续更新consul作为注册中心。


架构图

Eureka Server 服务需要添加如下依赖

<?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 https://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.1.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ping</groupId>
    <artifactId>eureka</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>EurekaServerDemo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
    </properties>

    <dependencies>
        <!-- SpringCloud 启动依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter</artifactId>
        </dependency>
        <!-- Eureka 服务器端依赖 -->
        <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>
    </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>

Eureka server单机版

创建配置文件

#应用名称
spring.application.name=Eureka Server Demo
#应用的端口号
server.port=8080
#是否注册到Eureka Server,默认true
eureka.client.register-with-eureka=false
#是否从Eureka Server获取注册信息,默认为true
eureka.client.fetch-registry=false
#设置与Eureka Server交互地址
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/

启动代码中添加@EnableEurekaServer注解

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

启动工程后,访问:http://localhost:8080/

Eureka server集群

修改配置文件,使用application.yml来配置

---
server:
  port: 8081
spring:
  profiles: node1
  application:
    name: eurekaCluster
eureka:
  instance:
    hostname: node1
  client:
    service-url:
      defaultZone: http://node2:8082/eureka/,http://node3:8083/eureka/
    fetch-registry: true
---
server:
  port: 8082
spring:
  profiles: node2
  application:
    name: eurekaCluster
eureka:
  instance:
    hostname: node2
  client:
    service-url:
      defaultZone: http://node1:8081/eureka/,http://node3:8083/eureka/
    fetch-registry: true

---
server:
  port: 8083
spring:
  profiles: node3
  application:
    name: eurekaCluster
eureka:
  instance:
    hostname: node3
  client:
    service-url:
      defaultZone: http://node1:8081/eureka/,http://node2:8082/eureka/
    fetch-registry: true

修改本机hosts文件,以Mac 为例 sudo vi /etc/hosts

#节点node1
127.0.0.1  node1
#节点node2
127.0.0.1  node2
#节点node3
127.0.0.1  node3

DNS的作用是将域名解析到对应的IP地址中。例如www.baidu.com对应的其中一个地址是183.121.232.123(有可能有多个服务器),访问 www.baidu.com实际上就是访问IP地址为183.121.232.123的服务器。 上面的配置 访问http://node1,http://node2,http://node3都是访问IP地址为127.0.0.1的主机。加上端口号就会出现“服务中心”的管理。

分别打开三个bash界面,启动注册中心服务

java -jar eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=node1
java -jar eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=node2
java -jar eureka-0.0.1-SNAPSHOT.jar --spring.profiles.active=node3

启动完成后,浏览器输入:http://localhost:8081/http://localhost:8082/http://localhost:8083/

eureka集群搭建完成

over!!!!

PS: 最近学习《spring cloud服务架构实战派》一书, 代码例子借鉴书中代码,及网上优秀博客!

相关文章

网友评论

      本文标题:eureka实现服务中心

      本文链接:https://www.haomeiwen.com/subject/iezhyktx.html