微服务系列(2)-SpringCloud服务注册

一、前言

在上一篇文章中讲了使用Spring Cloud的Eureka搭建注册中心,这一篇中将讲解使用Spring Cloud的注册中心来实现服务的注册与消费。
在本篇中将新建两个Spring Boot项目作为服务的消费者和生产者,废话不多说,直接上代码

二、开发工具及相关版本号

开发工具 版本号
idea 2018.2.4
spring boot 2.1.3
spring cloud Greenwich.RELEASE
java 1.8

二、服务的注册

新建一个Spring Boot项目,项目命名micro-consumer作为服务消费者。

  1. pom.xml如下
<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

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

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>

</project>
  1. application.yml配置文件如下
spring:
  application:
    name: micro-consumer
server:
  port: 8111

eureka:
  client:
    serviceUrl:
      #注意! 因为eureka注册中心添加了security,所以Eureka的defaultZone为 http://username:password@url的形式
      defaultZone: http://admin:admin@localhost:8080/eureka/
  1. 启动类代码
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EnableEurekaClient
@SpringBootApplication
public class DemoApplication {
    
    

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

}
  1. 启动注册中心、启动consumer服务
    打开注册中心首页 http://localhost:8080/,如下图 便可看到刚才启动的consumer服务已经注册到注册中心了。
    在这里插入图片描述

四、注意

  1. 添加maven依赖时要记得添加starter-web依赖,否则客户端启动成功后便会自动停止运行。

五、小节

使用Spring Boot新建服务时还是很方便的,一些简单的配置便可以启动一个微服务了。
博客中所有的代码都以上传到我的github上了,地址:俞兆鹏的github

猜你喜欢

转载自blog.csdn.net/zhaopeng_yu/article/details/87733332
今日推荐