springcloud.3.服务注册与发现

还是在springcloud总目录下创建一个springboot工程,用于实现服务注册发现功能
首先创建pom文件,内容如下:

<?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>

    <artifactId>hello-spring-cloud-eureka</artifactId>
    <packaging>jar</packaging>

    <parent>
        <groupId>com.yuu</groupId>
        <artifactId>hello-spring-cloud-dependencies</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../hello-spring-cloud-dependencies/pom.xml</relativePath> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <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.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!--程序入口类  为了java -jar 命令能够使程序启动,没有此配置则使用java -jar命令时会报错-->
                    <mainClass>com.yuu.hello.spring.cloud.eureka.EurekaApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

几个重点内容:

  • 引用统一依赖:
 <relativePath>../hello-spring-cloud-dependencies/pom.xml</relativePath>
  • 依赖eureka,这个依赖是这个模块的核心,不依赖eureka无法实现服务注册与发现
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
  • 这个配置的作用是在项目打成jar包,并且以jar包的形式启动时,使jar包能够找到启动类,没有此项配置,以jar包形式启动的时候会报错
<configuration>
    <mainClass>com.yuu.hello.spring.cloud.eureka.EurekaApplication</mainClass>
</configuration>

接下来是springboot的核心配置,application.yml

spring:
  application:
    name: hello-spring-cloud-eureka
  zipkin:
    base-url: http://localhost:9411

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    #此两项配置区分eureka服务端与客户端,禁止服务端注册自身begin
    registerWithEureka: false
    fetchRegistry: false
    #end
    serviceUrl:
      #defaultZone: http://localhost:8761/eureka/
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

其中application.name的作用是使各个服务在使用eureka的时候能够找到eureka
registerWithEureka和fetchRegistry两项,在作为eureka服务端的时候此两项的值须设为false,作为客户端的时候设为true
defaultZone就是eureka服务端的访问地址

接下来创建启动类

package com.yuu.hello.spring.cloud.eureka;

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

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

一定要加@EnableEurekaServer,否则不会启动服务注册功能的

到此位置,eureka服务端创建完成
启动程序后,在浏览器输入localhost:8761/,浏览器显示如下效果即为成功
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值