1、Eureka server服务注册与发现中心

2020-01-27  本文已影响0人  wqjcarnation

目标

搭建

step0 准备

1、d:工作区springcloud
2、设置工作区编码方式为utf-8

官网

https://spring.io/projects/spring-cloud

step1 准备

进入官网 https://start.spring.io/ 搜索Eureka-server,把右侧搜索到的两个程序都下载到本地D:\springcloud
demo-server.zip

image.png

step 2 demo-server解压后eclipse导入

demo-server解压后eclipse导入(按maven工程导入),工程名为eureka-server

step 3 pom.xml

pom.xml(特殊说明:为了与之前的springboot版本一致,spring-boot-starter-parent版本我从下载时的2.1.3改为了2.2.2。spring-cloud.version 改为Hoxton.RELEASE 其他与下载时完全一致)

<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.2.RELEASE</version>
</parent>
<groupId>com.example</groupId>
<artifactId>eureka-server</artifactId>
<version>1.0</version>
<name>eureka-server</name>
<description>Demo project for Spring Boot</description>

<properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Hoxton.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>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.6</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>

step3 开发主启动类

注意添加@EnableEurekaServer注解

package com.example.demo;

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

@EnableEurekaServer
@SpringBootApplication
public class DemoApplication {

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

step4 application.yml配置参考

https://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/2.2.1.RELEASE/reference/html/#spring-cloud-eureka-server

1)springboot基本配置

spring:
application:
name: eureka-serve

2)eureka默认freemarker做为视图,所以在spring原有配置基础上添加配置

springboot基本配置

spring:
...
#eureka默认freemarker做为视图,所以需要做下配置,
freemarker:
template-loader-path: classpath:/templates/
prefer-file-system-access: false

3)server端其他配置,先拷贝过来,一会儿讲解作用

  server:
    port: 8090
  
  eureka:
    instance:
      hostname: localhost
    client:
      registerWithEureka: false
      fetchRegistry: false
      serviceUrl:
        defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka//

4)支持用ip访问

上述配置只能通过localhost访问,改为以下配置后,可以支持用ip访问

eureka默认freemarker做为视图,所以需要做下配置,

......
     

  eureka:
    instance:
      instanceId: ${eureka.instance.ipAddress}:${spring.application.name}:${server.port}
      ipAddress: 127.0.0.1
      #是否优先使用ip地址 
      preferIpAddress: true
      #用上述三行替代hostname,改为用ip地址访问
      #hostname: localhost
    client:
       #是否在eureka服务器上注册自己的信息以供其他服务发现 (server端为单机时false,server端有负载时为true client端为true)
      registerWithEureka: false
      #是否获取eureka服务器注册表上的注册信息 server端为false,client端为true
      fetchRegistry: false
      serviceUrl:
        #此处也改为用ipaddress
        defaultZone: http://${eureka.instance.ipAddress}:${server.port}/eureka

5)增加服务端仪表盘配置,方便看服务情况

dashboard:
enabled: true
#路径可以自己命名
path: /eurekaAdmin
environment: ${spring.application.name}

6、application.yml完整配置

  #springboot基本配置
  spring:
    application:
      name: eureka-server
      #eureka 仪表盘默认freemarker做为视图,所以需要做下配置备用
    freemarker:
      template-loader-path: classpath:/templates/
      prefer-file-system-access: false
      
  server:
    port: 8090
  
  eureka:
    instance:
      instanceId: ${eureka.instance.ipAddress}:${spring.application.name}:${server.port}
      ipAddress: 127.0.0.1
      #是否优先使用ip地址 
      preferIpAddress: true
      #用上述三行替代localhost,改为用ip地址访问
      #hostname: localhost
    client:
       #是否在eureka服务器上注册自己的信息以供其他服务发现 (server端为单机时false,server端有负载时为true client端为true)
      registerWithEureka: false
      #是否获取eureka服务器注册表上的注册信息 server端为false,client端为true
      fetchRegistry: false
      serviceUrl:
        defaultZone: http://${eureka.instance.ipAddress}:${server.port}/eureka
    #增加服务端仪表盘配置,方便看服务状态
    dashboard:
      enabled: true
      #路径可以自己命名
      path: /eurekaAdmin
    environment: ${spring.application.name}}

测试##

启动服务,并在地址栏里输入以下地址

http://127.0.0.1:8090/eurekaAdmin

显示界面如下,即为成功。

image.png
上一篇下一篇

猜你喜欢

热点阅读