springcloud-springboot搭建注册中心过程以及踩坑

2024-09-06 13:38

本文主要是介绍springcloud-springboot搭建注册中心过程以及踩坑,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

终于开始复习springcloud了, 我磨磨唧唧了小半年从springboot到springcloud, 其实springboot现在照样不会, 

长时间的迷迷糊糊让我明白了一个道理, "你一个车都开不好的人,总想着去修车,去看发动机原理,去看线路,去看四轮定位, 去看特斯拉发明的移动办公工具,然后还总是妄想去看改装车的线路,,,,,到最后, 呢, 你开车就撞人 , 你修车就修坏, 什么也不会,,,,,"

所以我在遇见短时间内不可能整明白的东西,就先放下, 先把这个流程跑通了吧, 人生苦短, 不要纠结太多东西, 前段时间看 大总管的 雪中悍刀行, 里头褚禄山说过一句话,人生两苦:想要得不到, 拥有却失去. 简直是话尽了芸芸众生的三万六千天啊.....

 我擦, 扯得有点太远了, 赶紧上代码吧, 因为很多老前辈用的都是2.0以前的版本, 所以我也打算从2.0以前的版本开始,

(你非要用2.0的话, 我在最后面贴一下2.0需要引入的依赖)

首先去springboot首页生成一个项目或者用idea创建一个

什么都不添加, 名字我叫 eureka-server

pom文件: 生成的是2.0.2版本的,改成1.5.6版本的

<?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.example</groupId><artifactId>eureka-server</artifactId><version>1.0.0-SNAPSHOT</version><packaging>jar</packaging><name>eureka-server</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.6.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><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-eureka-server</artifactId><version>1.3.4.RELEASE</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

配置文件

#表示注册中心的端口号
server.port=1111
#表示注册中心的主机域名
eureka.instance.hostname=localhost
#禁止这个微服务向自己注册自己的默认行为
eureka.client.register-with-eureka=false
#不去检索及其他的服务,因为此微服务只需要维护自己
eureka.client.fetch-registry=false
#默认访问域名
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka

启动类添加上这个注解:

然后启动,访问 http://localhost:1111/ 发现启动成功,而且下面没有任何微服务注册过来

 感觉贼简单???

接下来就弄一个服务提供者 ,来注册到这个注册中心里面

再次新建一个springboot项目名字叫做  service-provider

这次我们添加一个web的依赖

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.example</groupId><artifactId>service-provider</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>service-provider</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.6.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</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-eureka</artifactId><version>1.3.4.RELEASE</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

 配置文件就写两个:

spring.application.name=hello-service
eureka.client.service-url.defaultZone=http://localhost:1111/eureka

启动类添加,表示被注册和发现:

@EnableDiscoveryClient

 写一个controller

 既然添加了web的依赖,那么就要写一个controller了,

package com.example.serviceprovider.web;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestController
public class HelloController {private static final Logger logger = LoggerFactory.getLogger(HelloController.class);// 注册中心的东东@Autowiredprivate DiscoveryClient discoveryClient;@RequestMapping(value = "/hello", method = RequestMethod.GET)public String hello(){String s = "hello-service";List<ServiceInstance> instanceList = discoveryClient.getInstances(s);for (ServiceInstance instances : instanceList) {logger.info("hello-service这个微服务信息: host:" + instances.getHost() + ",service_id:" + instances.getServiceId());}return "Hello World";}
}

启动!

成功启动并且 发现已经被注册了, (关于被我标蓝选中的东西,这个可以了解下, 后面我会贴出来解决办法,新手不要纠结太多..我没头发我米豆腐....)

访问下: 

控制台  :    

 

ok,这样也就搭建完了. 这个注册中心, 类似于一个zookeeper, 专门用于管理很多个微服务的.

===============================以上===鸣谢一下资料大佬========================================

使用Spring Cloud搭建服务注册中心

https://blog.csdn.net/lc0817/article/details/54375802    第四问题解决了上图标蓝的问题

https://www.cnblogs.com/breath-taking/articles/7940364.html

 https://blog.csdn.net/qq_36752632/article/details/79497130   2.0爬过的坑

git上面一个2.0版本的pom文件

https://github.com/spring-cloud-samples/eureka/blob/master/pom.xml

===============================特别鸣谢博主=江南一点雨========================================

说一下spring boot为2.0 ,改的比较, ,,,那啥

首先 如果上面的是2.0以后的maven依赖, 

那么springcloud相关依赖就需要改为:

 <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId><version>2.0.2.RELEASE</version></dependency>

对比一下2.0 以前的, 发现不同了吗 ,  

 

多了一个 netflix  ,这个东西是springcloud的一个核心构成, 我也仅限于了解, 深入了解的还是去百度吧.

 

这篇关于springcloud-springboot搭建注册中心过程以及踩坑的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1142159

相关文章

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

SpringBoot+Docker+Graylog 如何让错误自动报警

《SpringBoot+Docker+Graylog如何让错误自动报警》SpringBoot默认使用SLF4J与Logback,支持多日志级别和配置方式,可输出到控制台、文件及远程服务器,集成ELK... 目录01 Spring Boot 默认日志框架解析02 Spring Boot 日志级别详解03 Sp

java中反射Reflection的4个作用详解

《java中反射Reflection的4个作用详解》反射Reflection是Java等编程语言中的一个重要特性,它允许程序在运行时进行自我检查和对内部成员(如字段、方法、类等)的操作,本文将详细介绍... 目录作用1、在运行时判断任意一个对象所属的类作用2、在运行时构造任意一个类的对象作用3、在运行时判断

java如何解压zip压缩包

《java如何解压zip压缩包》:本文主要介绍java如何解压zip压缩包问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java解压zip压缩包实例代码结果如下总结java解压zip压缩包坐在旁边的小伙伴问我怎么用 java 将服务器上的压缩文件解压出来,

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

Spring WebFlux 与 WebClient 使用指南及最佳实践

《SpringWebFlux与WebClient使用指南及最佳实践》WebClient是SpringWebFlux模块提供的非阻塞、响应式HTTP客户端,基于ProjectReactor实现,... 目录Spring WebFlux 与 WebClient 使用指南1. WebClient 概述2. 核心依

Spring Boot @RestControllerAdvice全局异常处理最佳实践

《SpringBoot@RestControllerAdvice全局异常处理最佳实践》本文详解SpringBoot中通过@RestControllerAdvice实现全局异常处理,强调代码复用、统... 目录前言一、为什么要使用全局异常处理?二、核心注解解析1. @RestControllerAdvice2

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

Spring事务传播机制最佳实践

《Spring事务传播机制最佳实践》Spring的事务传播机制为我们提供了优雅的解决方案,本文将带您深入理解这一机制,掌握不同场景下的最佳实践,感兴趣的朋友一起看看吧... 目录1. 什么是事务传播行为2. Spring支持的七种事务传播行为2.1 REQUIRED(默认)2.2 SUPPORTS2

怎样通过分析GC日志来定位Java进程的内存问题

《怎样通过分析GC日志来定位Java进程的内存问题》:本文主要介绍怎样通过分析GC日志来定位Java进程的内存问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、GC 日志基础配置1. 启用详细 GC 日志2. 不同收集器的日志格式二、关键指标与分析维度1.