[SpringCloud扫盲]---(2)服务注册

Author:赵志乾
Date:2019-02-17
Declaration:All Right Reserved!!!

 

1、服务注册

微服务架构中,分散的服务最常采用注册中心来实现服务治理。而服务注册,就是指微服务架构中的一个实体服务将自己的信息和状态推送到注册中心,以便注册中心实现服务的统一管理、维护。

————————————————————————————————————————

服务注册

前置条件:按照《注册中心搭建》,搭建SpringCloud Eureka注册中心;

1、新建maven项目,命名为HelloProvider;

2、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     
                             http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zhaozhiqian</groupId>
    <artifactId>hello-provider</artifactId>
    <version>1.0-SNAPSHOT</version>


    <!--指定父项目,目的是继承依赖项-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath />
    </parent>

    <dependencies>
        <!-- eureka -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <!--指定Spring Cloud为父项目,从而实现多继承-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR4</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

注:pom文件中各标签含义见《POM文件之常用标签

3、创建启动类:HelloProviderApplication.java

package com.zhaozhiqian.start;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

//@SpringBootApplication注解表示该类为启动类
@SpringBootApplication
//@EnableDiscoveryClient注解表示该服务为Eureka客户端
@EnableDiscoveryClient
public class HelloProviderApplication {
    public static void main(String[] args){
        SpringApplication.run(HelloProviderApplication.class,args);
    }
}

4、在目录src/main/resources下创建一个application.properties属性文件,内容如下:

spring.application.name=zhaozhiqian-hello-provider
server.port=8081
#注册中心Eureka的地址
eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/

5、执行HelloProviderApplication,输出如下信息,表明服务启动成功。

2019-02-17 16:12:32.924  INFO 22892 --- [           main] com.netflix.discovery.DiscoveryClient    : Saw local status change event StatusChangeEvent [timestamp=1550391152924, current=UP, previous=STARTING]
2019-02-17 16:12:32.927  INFO 22892 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_ZHAOZHIQIAN-HELLO-PROVIDER/LAPTOP-2FCC7743.lan:zhaozhiqian-hello-provider:8081: registering service...
2019-02-17 16:12:33.088  INFO 22892 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8081 (http)
2019-02-17 16:12:33.089  INFO 22892 --- [           main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8081
2019-02-17 16:12:33.099  INFO 22892 --- [           main] c.z.start.HelloProviderApplication       : Started HelloProviderApplication in 7.127 seconds (JVM running for 8.404)
2019-02-17 16:12:33.147  INFO 22892 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_ZHAOZHIQIAN-HELLO-PROVIDER/LAPTOP-2FCC7743.lan:zhaozhiqian-hello-provider:8081 - registration status: 204

6、访问网址:http://localhost:8888/,即可看到Eureka提供的WEB控制台,当前注册实列表中可以看到我们的服务HelloProvider,表明服务确实已成功注册。

 

 

参考资料:《SpringCloud微服务全栈技术与案例解析》

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我叫白小猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值