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微服务全栈技术与案例解析》