springboot整合eureka搭建微服务

缘故为何      2022-04-27     657

关键词:

 

1.创建一个services项目,添加三个子模块client(客户端)、service(服务端)、registry(注册中心)

 1.1 创建一个services项目

 1.2 添加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>
    <packaging>pom</packaging>
    <modules>
        <module>client</module>
        <module>service</module>
        <module>registry</module>
    </modules>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.micro</groupId>
    <artifactId>services</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>services</name>
    <description>services</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</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-server</artifactId>
            <version>1.4.5.RELEASE</version>
        </dependency>

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

    </dependencies>

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

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

 

 1.3 添加子模块client(客户端)

 1.4 添加子模块service(服务端)

 

 1.5 添加子模块registry(注册中心)

 

2.搭建registry(注册中心)

 2.1 application.yml 配置

server:
  port: 8080

eureka:
  instance:
    hostname: localhost
  client: 
    registerWithEureka: false
    fetchRegistry: false

 

 2.2 编写启动程序

package com.services.registry;

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

@SpringBootApplication
@EnableEurekaServer
public class RegistryApplication {

    public static void main(String[] args) {

        SpringApplication.run(RegistryApplication.class);
    }
}

 

3.搭建service(服务端)

  3.1 application.yml 配置

server:
  port: 8081
spring:
  application:
    name: service
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8080/eureka/

 

 3.2 编写启动程序

package com.services.service;

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

@SpringBootApplication
@EnableDiscoveryClient
public class ServiceApplication {

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

}

 

 3.3 编写服务

package com.services.service;

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

@RestController
public class HelloWorldController {
    @RequestMapping("hello/{name}")
    public String hello(@PathVariable String name) {
        return name + " say hello";
    }

}

 

4.搭建client(客户端)

 4.1 application.yml 配置

server:
  port: 8082
spring:
  application:
    name: client
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8080/eureka/
app:
  service-url: http://localhost:8081/

 

 4.2 编写启动程序

package com.services.client;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
public class ClientApplication {

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

    @Bean
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

 

 4.3 编写service

package com.services.client;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class CallHelloService {

    @Value("${app.service-url}")
    private String appServiceUrl;

    @Autowired
    private RestTemplate restTemplate;

    public String callHello(String name) {
        // 是一个http client
        ResponseEntity result = restTemplate.postForEntity(appServiceUrl + "hello/" + name, null, String.class);
        return result.getBody().toString();
    }
}

 

 4.4 编写controller

package com.services.client;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CallHelloController {

    @Autowired
    private CallHelloService callHelloService;

    @GetMapping("hello")
    public String hello(String name) {
        String result = callHelloService.callHello(name);
        return result;
    }

}

5.启动registry-service-client模块

 5.1 registry

http://localhost:8080/

 

 

 5.2 service

 

刷新网页 http://localhost:8080/

 

 5.3 client

刷新网页 http://localhost:8080/

client 访问 http://localhost:8082/hello?name=tao

源码地址: https://github.com/80905949/SpringCloud.git

7-springcloud-eureka-3-搭建与配置eureka服务注册中心(代码片段)

...和方便,SpringCloud中的Eureka服务注册中心实际上也是一个SpringBoot工程,我们只需通过引入相关依赖和注解配置就能让SpringBoot构建的微服务应用轻松地与Eureka进行整合。具体步骤如下:1、创建一个SpringBoot项目,并且添加SpringBoot... 查看详情

java之springcloud微服务eureka(第一个阶段)springboot项目实现商品服务器端是调用

...ava之SpringCloud微服务搭建(第一个阶段)【一】【SpringBoot项目实现商品服务器端是调用】Java之SpringCloud微服务Eureka(第一个阶段)【二】【SpringBoot项目实现商品服务器端是调用】Java之SpringCloud微服务搭建Ribbonÿ 查看详情

java之springcloud微服务eureka(第一个阶段)springboot项目实现商品服务器端是调用(代码片段)

Java之SpringCloud微服务Eureka【二】【SpringBoot项目实现商品服务器端是调用】一、服务注册Eureka基础1、微服务的注册中心(1)注册中心的主要作用(2)常见的注册中心2、Eureka的概述(1)Eureka的基础知识࿰... 查看详情

微服务之springcloud实战:springcloudeureka服务治理

...完成微服务的服务治理功能,SpringCloud通过为Eureka增加了SpringBoot自动化配置,只需要简单的依赖和配置就可完成Eureka整合搭建。   服务治理可以说是微服务中做核心的模块,主要负责服务的自动化注册与发现,在最 查看详情

【微服务】-springbootadmin

参考技术A目录:SpringBootAdmin用于管理和监控一个或者多个SpringBoot应用,SpringBootAdmin分为Server端和Client端,Client通过http向Server端注册,也可以结合SpringCloud的服务注册组件Eureka进行注册。SpringAdmin的监测详细信息有如下:本文的... 查看详情

springcloudeureka——简介

...服务架构中的服务治理功能。SpringCloud通过为Eureka增加了SpringBoot风格的自动化配置,我们只需要通过简单引入依赖和注解配置就能让SpringBoot构建的微服务应用轻松地与Eureka服务治理体系进行整合。SpringCl 查看详情

Springboot 微服务注册到 Eureka 但打不上

】Springboot微服务注册到Eureka但打不上【英文标题】:SpringbootMicroserviceisregisteredwithEurekabutcannothitit【发布时间】:2017-09-0320:35:38【问题描述】:基本问题,所以只是想确保我理解正确。我已经创建了一个发现服务器:@SpringBootAppli... 查看详情

springcloud微服务demo-上篇(代码片段)

...式2.1RPC/RMI2.2Http2.3如何选择3.Http客户端工具3.1RestTemplate4.SpringBoot搭建项目5.SpringCloud简介6.微服务场景模拟7.Eureka注册中心7.1简介7.2原理图7.3搭建注册中心7.4将user-service注册到eureka7.5user-consume从eureka中获取服务7.6eureka 查看详情

微服务架构:eureka集群搭建

...方要能发现目标服务。在我们的微服务架构中我们采用了Eureka来完成微服务的注册与发现。微 查看详情

springboot整合eureka

...,一般是不会出错的,如果出错了,那么百分之99是因为springboot的版本跟springcloud的版本对应不上,然后出现各种莫须有的问题在搭建的过程中我也出现了各种问题,随后找到了一个可以适配的版本这里在上面的例子上进行操作... 查看详情

springcloud整合swagger统一服务api(代码片段)

Swagger整合多个微服务统一API接口文档单系统SpringBoot整合Swagger或者Swagger注解介绍 请看另一篇文章Swagger(丝袜哥)快速入门(超详细介绍)准备工具1. 自己搭建准备好微服务环境2.里面至少包含一个注册中心,一个网关... 查看详情

springcloudconfig相关配置简介使用整合eureka

...ConfigServer基本使用  3、ConfigClient基本使用  4、Config整合Eureka  5、Config配置搜索路径 SpringCloudConfig简介 SpringCloudConfig为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环... 查看详情

.netcore+eureka+springboot服务注册与调用(代码片段)

.netcore+eureka+springboot服务注册与简单的调用假期小长假遇上疫情只能去家里蹲了,刚好有时间总结一下。概述微服务架构是当前比较火的分布式架构,本篇基于.netcore微服务和Eureka服务注册于发现中心,实现将.netcore的微服务注册... 查看详情

第三章服务治理:springcloudeureka

...服务架构中的服务治理功能。SpringCloud通过为Eureka增加了SpringBoot风格的自动化配置,我们只需通过引入依赖和注解配置就能让SpringBoot构建的微服务应用轻松的与Eureka服务治理体系进行整合。 服务治 查看详情

springboot微服务搭建指南架构设计

前言本系列将介绍如果从零构建一套分布式系统。同时也是对自己过去工作的一个梳理过程。本文先整理出构建系统的主要技术选型,以及技术框架。其实在形成如下框架前,我参考了许多资料和结构,也结合中小型公司的时间... 查看详情

集群搭建

...方要能发现目标服务。在我们的微服务架构中我们采用了Eureka来完成微服务的注册与发现。微服务通过Eureka进行注册,服务调用方通过Eureka找到目标服务。由于服 查看详情

springcloud基础

Springcloud是基于springboot,管理Springboot创建各个微服务应用,注册服务、服务发现注册服务使用eureka搭建eurekaserver启动类加上@EnableEurekaServerapplication.ymlregisterWithEure、fetchRegistry设置为false表明自己server端server:port:8761eureka:instanc 查看详情

微服务架构eureka集群高可用配置(代码片段)

工具:idea环境:java8、maven3版本:springboot 1.5.15.RELEASE1.搭建springbooteureka项目 2.pom.xml添加相应依赖,如下:<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmln 查看详情