分布式

spring-cloud微服务项目实战(2)-搭建eureka注

2019-02-08  本文已影响97人  爱编程的凯哥

目标:

搭建高可用eureka注册中心,服务端注册server服务,对于客户端调用,本章不讨论,下一章会讨论

eureka简介

Eureka是Spring Cloud Netflix微服务套件中的一部分,可以与Springboot构建的微服务很容易的整合起来。
Eureka包含了服务器端和客户端组件。服务器端,也被称作是服务注册中心,用于提供服务的注册与发现。Eureka支持高可用的配置,当集群中有分片出现故障时,Eureka就会转入自动保护模式,它允许分片故障期间继续提供服务的发现和注册,当故障分片恢复正常时,集群中其他分片会把他们的状态再次同步回来。


eureka架构图

操作

  1. 新建eureka-server服务
新建eureka-server moudle
  1. 添加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>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>open.template.work</groupId>
    <artifactId>eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-server</name>
    <description>Demo project for Spring Boot</description>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </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-security</artifactId>
        </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>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

  1. 添加application.yml
spring:
  application:
    name: temmplate-eureka-server
#开启安全机制,需要账号密码访问
security:
  basic:
    enabled: true
  user:
    name: root
    password: 123

#---做为连接符,分开不同配置
---
spring:
  profiles: peer1

server:
  port: 8761
eureka:
  instance:
    hostname: peer1
#eureka服务彼此注册,添加账号密码模式
  client:
    service-url:
      defaultZone: http://root:123@peer2:8762/eureka/,http://root:123@peer3:8763/eureka/

---
spring:
  profiles: peer2
server:
  port: 8762
eureka:
  instance:
    hostname: peer2
  client:
    service-url:
      defaultZone: http://root:123@peer1:8761/eureka/,http://root:123@peer3:8763/eureka/

---
spring:
  profiles: peer3
server:
  port: 8763
eureka:
  instance:
    hostname: peer3
  client:
    service-url:
      defaultZone: http://root:123@peer1:8761/eureka/,http://root:123@peer2:8762/eureka/
  1. 应用入口添加Eureka注解
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

}
  1. 修改hosts,配置三个域名测试
127.0.0.1       peer1
127.0.0.1       peer2
127.0.0.1       peer3
  1. idea配置多个run
配置多个run

这样配置三个run客户端,通过profiles参数区分配置,分别启动三个项目,可以看到三个服务运行状态


eureka
  1. 在udm-server上配置eureka客户端,注册服务
@SpringBootApplication
@Configuration
@EnableEurekaClient
public class UdmServerApplication {

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

    @Bean
    public static Endpoint<Map<String, Object>> servertime() {
        return new MyEndPoint();
    }
}

application.yml添加注册信息

server:
  port: 8000
info:
  app:
    name: @project.artifactId@
#management:
#  security:
#    enabled: true
endpoints:
  shutdown:
    enabled: true
    id: shut
  beans:
    enabled: false
security:
  basic:
    enabled: true
  user:
    name: root
    password: 12
spring:
  Jackson:
    serialization:
      indent-output: true
  application:
    name: udm-server
  cloud:
    inetutils:
#多网卡匹配模式,单网卡可不用
      preferred-networks:
        - 192.168
eureka:
  client:
    service-url:
      defaultZone: http://root:123@peer1:8761/eureka/,http://root:123@peer2:8762/eureka/,http://root:123@peer3:8763/eureka/
  instance:
    prefer-ip-address: true

pom添加Eureka依赖,然后重启udm-server服务,再看eureka服务

udm-server服务

以上就是通过Eureka客户端直接注册服务,对于一些非java端web应用,基于微服务的宗旨一样支持,可以通过xml配置方式完成需求,具体这里没有讨论,可以参考https://blog.csdn.net/wo18237095579/article/details/83271829
参考资料:https://blog.csdn.net/jek123456/article/details/74171055/

完工!

上一篇下一篇

猜你喜欢

热点阅读