SpringCloud项目搭建之(二)Eureka服务注册工程

  好久没有写博客了,最近疫情期间互联网的行情也是越来越不景气,为了保住自己的工作,不能被时代的洪流给淹没,最好的办法就是不断的去提示自己,目前SpringCloud框架已经火了好几年了,虽然很多的公司还是在使用分布式微服务甚至是SSM框架。但是掌握SpringCloud架构也逐渐成为各大公司招聘的一个标准。今天能就先给大家分享一下关于SpringCloud的注册中心Eureka怎么去搭建。

首先呢,先介绍下什么是springcloud,目前不是已经有了分布式微服务了吗, 那为什么要用springcloud呢?

1.什么是springcloud?

Spring Cloud 是一个相对比较新的微服务框架,2016 才推出 1.0 的 Release 版本. 但是其更新特别快,几乎每 1-2 个月就有一次更新,虽然 Spring Cloud 时间最短, 但是相比 Dubbo 等 RPC 框架, Spring Cloud 提供的全套的分布式系统解决方案。

2.为什么要用springcloud呢?

Spring Cloud 为开发者提供了在分布式系统(配置管理,服务发现,熔断,路由,微代理,控制总线,一次性 Token,全居琐,Leader 选举,分布式 Session,集群状态)中快速构建的工具,也就是说springcloud的生态圈比较完善,而且使用 Spring Cloud 的开发者可以快速的启动服务或构建应用、同时能够快速和云平台资源进行对接 。

那么,现在我们就开始进入正题,怎么去搭建springCloud的服务提供者——Eureka

当 Client 向 Server 注册时,它会提供一些元数据,例如主机和端口,URL,主页等。Eureka Server 从每个 Client 实例接收心跳消息。 如果心跳超时,则通常将该实例从注册 Server 中删除。

那么我们首先就是要先上一个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>

    <parent>
        <groupId>com.spring.cloud</groupId>
        <artifactId>hello-spring-cloud-dependencies</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../hello-spring-cloud-dependencies/pom.xml</relativePath>
    </parent>

    <artifactId>hello-spring-cloud-service-admin</artifactId>
    <packaging>jar</packaging>

    <name>hello-spring-cloud-service-admin</name>
    <url>http://www.springCloud.com</url>
    <inceptionYear>2018-Now</inceptionYear>

    <dependencies>
        <!-- Spring Boot Begin -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Spring Boot End -->

        <!-- Spring Cloud Begin -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <!-- Spring Cloud End -->
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.hello.spring.cloud.service.admin.ServiceAdminApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

启动类

Application

通过注解 @EnableEurekaClient 表明自己是一个 Eureka Client.

package com.hello.spring.cloud.service.admin;

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

@SpringBootApplication
@EnableEurekaClient //这里一定要加上 来表示自己是一个服务的提供者
public class ServiceAdminApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServiceAdminApplication.class, args);
    }
}

配置文件

application.yml

spring:
  application:
    name: hello-spring-cloud-service-admin

// 需要指明 spring.application.name,这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个

重要

很重要

很很重要!!

server:
  port: 8762

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

这个时候你就可以写一个controller

package com.hello.spring.cloud.service.admin.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AdminController {

    @Value("${server.port}")
    private String port;

    @RequestMapping(value = "hi", method = RequestMethod.GET)
    public String sayHi(@RequestParam(value = "message") String message) {
        return String.format("Hi,your message is : %s i am from port : %s", message, port);
    }
}

启动工程,打开 http://localhost:8761 ,即 Eureka Server 的网址:

你会发现一个服务已经注册在服务中了,服务名为 HELLO-SPRING-CLOUD-SERVICE-ADMIN ,端口为 8762

这时打开 http://localhost:8762/hi?message=HelloSpring ,你会在浏览器上看到 :

Hi,your message is :"HelloSpring" i am from port:8762

发布了4 篇原创文章 · 获赞 0 · 访问量 189

猜你喜欢

转载自blog.csdn.net/jack19950729/article/details/105451189