SpringCloud---单节点Eureka Server

废话不多说,直接上代码。

单节点Eureka Server

  • 配置pom文件

<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.0modelVersion>

    <groupId>com.examplegroupId>
    <artifactId>eurekaartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>

    <name>eurekaname>
    <description>这是一个注册中心description>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.5.4.RELEASEversion>
        <relativePath/> 
    parent>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
        <spring-cloud.version>Dalston.SR1spring-cloud.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-eureka-serverartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>


project>
  • 配置配置文件
server:
  #端口号
  port: 8761
eureka:
  instance:
    hostname: localhost
  client:
    #注册中心 false代表不向注册中心注册自己(默认为true)
    registerWithEureka: false
    #表示是否从 Eureka Server 获取注册信息,默认为true。因为这是一个单点的,
    #不需要同步同步其它的Eureka Server节点的数据,设置false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:8761/eureka/
  • 启动类
    在原有的里面加上@EnableEurekaServer
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }
}
  • 测试
    在浏览器输入http://localhost:8761/
    SpringCloud---单节点Eureka Server_第1张图片
    测试成功!!!

服务注册到Eureka

  • 在普通服务启动类加上:@EnableDiscoveryClient
@EnableDiscoveryClient
@SpringBootApplication
public class Hell0ServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(Hell0ServiceApplication.class, args);
    }
}
  • 在配置文件:
#服务命名(用于指定注册到Eureka Server上的应用名称)
spring:
  application:
    name: hello-service
##eureka配置
eureka:
  client:
    serviceUrl:
     defaultZone: http://localhost:8761/eureka/
#表示将自己的ip注册到Eureka Server(如不配置该属性或者设置false,则表示微服务所在操作系统的name到Eureka Server)
  instance:
    prefer-ip-address: true
  • 刷新之前Eureka
    SpringCloud---单节点Eureka Server_第2张图片

你可能感兴趣的:(SprinCloud)