Eureka微服务注册实践

Eureka微服务注册实践

1. 新建Eureka注册中心

1.2 pom.xml

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
         <dependency>
        <groupId>org.codehaus.janino</groupId>
        <artifactId>janino</artifactId>
        <version>3.0.7</version>
       </dependency>

    </dependencies>

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

1.2 application.properties

#Eureka注册中心名
spring.application.name=eureka-server
#服务地址
eureka.instance.hostname=localhost
#服务端口
server.port=1111

#是否将自己注册到Eureka-Server中
eureka.client.registerWithEureka=false
#是否从Eureka-Server中获取服务注册信息
eureka.client.fetchRegistry=false
#注册中心URL
eureka.client.service-url.default-zone=http://${eureka.instance.hostname}:${server.port}/eureka

1.3 启动类

package com.hezy.springbootEurekaServer;

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

// EurekaServer声明
@EnableEurekaServer
@SpringBootApplication
public class SpringbootEurekaServerApplication {

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

}

1.4 启动

java -jar ./springbootEurekaServer/target/springbootEurekaServer-0.0.1-SNAPSHOT.jar

在这里插入图片描述
在这里插入图片描述
查看注册中心内容
在这里插入图片描述

2.新建springboot微服务,springbootHello

2.1 pom.xml

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>2020.0.3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

2.2 aplication.properties

#微服务名
spring.application.name=HelloServices
#注册中心URL
eureka.client.service-url.defaultZone=http://localhost:1111/eureka/

2.3 启动类

package com.hezy.springbootHello;

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

// EurekaClient声明
@EnableEurekaClient
@SpringBootApplication
public class SpringbootHelloApplication {

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

}

2.4 controller

package com.hezy.springbootHello.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("hello")
public class HelloController {
       @RequestMapping("")

       public String hello() {
           return "Hello World!";
       }
}

2.5 启动

java -jar ./springbootHello/target/springbootHello-0.0.1-SNAPSHOT.jar --server.port=8081

在这里插入图片描述

查看注册中心内容,已发现微服务
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值