参考资料: [JavaEE] 五分钟搭建SpringCloud环境, 进入微服务时代

感谢上篇博文大佬带领走进springcloud世界, 本博文主要目的为记录自己学习springcloud的点点滴滴, 给自己的知识进行整理, 如果能帮助更多的小伙帮那就更好了.

ps: 本文主要作为备忘及快速搭建springcloud之用, 只记录主要步骤. 原文讲述更加细致, 想深入学习的同学推荐点击上方连接学习.

操作系统: Windows 10

IDE: IntelliJ IDEA 2018.3.6

JAVA: JDK 1.8.X

Meave: 3.6.0

SpringBoot: 2.1.7

1. 创建一个工程

创建springboot工程

2. 创建Eureka注册中心

依然选择springboot工程

选择eureka服务器

导包, 开启服务管理器

为eureka注册中心添加注解开启服务

配置eureka注册中心配置文件

server:
# 配置服务端口
port: 8081
eureka:
client:
service-url:
# 配置eureka服务器地址
defaultZone: http://127.0.0.1:8081/eureka
#是否需要将自己注册到注册中心(注册中心集群需要设置为true)
register-with-eureka: false
#是否需要搜索服务信息 因为自己是注册中心所以为false
fetch-registry: false

注意缩进, 因为yml使用缩进来区分不同字段的.

运行ServiceEurekaApplication文件启动项目, 访问注册中心

http://localhost:8081

3. 创建两个微服务service-a,service-b

创建一个springboot模块

起个名字

选择web

选择客户模块

路由设置

配置微服务的入口文件 @EnableEurekaClient

配置application.yml

service-a

server:
# 服务端口号
port: 8082
spring:
application:
# 服务名称 - 服务之间使用名称进行通讯
name: service-objcat-a
eureka:
client:
service-url:
# 填写注册中心服务器地址
defaultZone: http://localhost:8081/eureka
# 是否需要将自己注册到注册中心
register-with-eureka: true
# 是否需要搜索服务信息
fetch-registry: true
instance:
# 使用ip地址注册到注册中心
prefer-ip-address: true
# 注册中心列表中显示的状态参数
instance-id: ${spring.cloud.client.ip-address}:${server.port}

service-b

server:
# 服务端口号
port: 8083
spring:
application:
# 服务名称 - 服务之间使用名称进行通讯
name: service-objcat-b
eureka:
client:
service-url:
# 填写注册中心服务器地址
defaultZone: http://localhost:8081/eureka
# 是否需要将自己注册到注册中心
register-with-eureka: true
# 是否需要搜索服务信息
fetch-registry: true
instance:
# 使用ip地址注册到注册中心
prefer-ip-address: true
# 注册中心列表中显示的状态参数
instance-id: ${spring.cloud.client.ip-address}:${server.port}

运行微服务

分别运行注册中心及微服务模块

出现端口号表示启动成功

编写测试接口

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class TestAController { @RequestMapping("testA")
public String TestAController(){
return "Hello,SpringCloud for TestA";
}
}

重启服务

访问下面地址

http://localhost:8082/testA

访问成功

使用服务b调用服务a的接口
这时我们就需要用到eurka(注册中心)feign客户端了
首先我们在service-b中创建interface

在微服务b中, 创建一个ServiceAFeignClient接口

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping; // 填入注册中心中的应用名, 也就是要调用的微服务的应用名
// 在eureka页面中可以找到
@FeignClient("SERVICE-OBJCAT-A")
public interface ServiceAFeignClient {
@RequestMapping("testA")
public String TestAController();
}

应用名可以在eureka中找到
http://localhost:8081

在服务b中添加个控制器

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
// 添加注解声明是注册中心客户端
@EnableEurekaClient
// 实现不同子服务调用
@EnableFeignClients
public class TestBController { @Autowired
private ServiceAFeignClient serviceAFeignClient; @RequestMapping("call")
public String call(){
String result = serviceAFeignClient.TestAController();
return "b to a 访问结果 ---" + result;
}
}

解决@Autowired实例报错

重新运行服务b 在网站上访问试试吧

http://localhost:8083/call

PS: 在springcloud中一个子服务调用另一个子服务默认超时时间是1s, 也就是说要是被调用的子服务返回超过一秒就会出现错误, 针对此问题需要修改调用服务的yml文件.

举例: 在本案例中, service-a是被调用者, service-b是调用者, 则在service-b的yml文件中加入

ribbon:
#建立连接超时时间
ReadTimeout: 5000
#读取资源超时间
ConnectTimeout: 5000

注意首行缩进, service-b完整配置如下

server:
# 服务端口号
port: 8083
spring:
application:
# 服务名称 - 服务之间使用名称进行通讯
name: service-objcat-b
eureka:
client:
service-url:
# 填写注册中心服务器地址
defaultZone: http://localhost:8081/eureka
# 是否需要将自己注册到注册中心
register-with-eureka: true
# 是否需要搜索服务信息
fetch-registry: true
instance:
# 使用ip地址注册到注册中心
prefer-ip-address: true
# 注册中心列表中显示的状态参数
instance-id: ${spring.cloud.client.ip-address}:${server.port}
ribbon:
#建立连接超时时间
ReadTimeout: 5000
#读取资源超时间
ConnectTimeout: 5000

文中案例下载

springcloud学习之路: (一) 最简单的搭建springcloud的方法的更多相关文章

  1. springboot 学习之路 1(简单入门)

    目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...

  2. springboot 学习之路 9 (项目启动后就执行特定方法)

    目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...

  3. SpringCloud学习之手把手教你用IDEA搭建入门项目【番外篇】(一)

    之前的文章里,我曾经搭建了一个Springcloud项目,但是那个时候我对于SpringCloud架构的很多组件不甚清楚,只是通过查找资料然后动手稀里糊涂的把一个项目成功搭建起来了,其中有很多不合理和 ...

  4. C++学习之路——1.linux下环境搭建

    学习之路,可说各有各的看法和方法.对于我来说,完全是兴趣一下子来了,脑壳发热吧.就想有个干净的环境,只想着与程序有关的东西练一练. 目前想学习C++,可是打开VC++6,再安了VS2010.VS201 ...

  5. SpringCloud学习之手把手教你用IDEA搭建入门项目(三)

    本篇博客是承接上一篇<手把手教你用IDEA搭建SpringCloud入门项目(二)>,不清楚的请到我的博客空间查看后再看本篇博客,上面两篇博客成功创建了一个简单的SpringCloud项目 ...

  6. [原创]java WEB学习笔记54:Struts2学习之路---概述,环境的搭建

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  7. SpringCloud学习之手把手教你用IDEA搭建入门项目(一)

    SpringCloud简单搭建 jdk:1.8开发工具:IDEA注:需要了解springcloud 1.创建最简单的Maven项目 1)开始创建一个新的项目 ​ 2)创建一个空模板的maven项目,用 ...

  8. SpringCloud学习之手把手教你用IDEA搭建入门项目(二)

    本篇博客是承接上一篇<手把手教你用IDEA搭建SpringCloud入门项目(一)>,不清楚的请到我的博客空间查看后再看本篇博客 1)先创建一个Eureka服务注册中心模块,用来作为服务的 ...

  9. Android开发学习之路--RxAndroid之简单原理

      学习了RxAndroid,其实也就是RxJava了,但是还是不是非常清楚到底RxAndroid有什么用呢?为什么要使用RxAndroid呢?这篇文章讲得不错,RxJava的原理.但是这里还是把整个 ...

随机推荐

  1. Python正则表达式中re.S作用

    re.S的作用: 不使用re.S时,则只在每一行内进行匹配,如果存在一行没有,就换下一行重新开始,使用re.S参数以后,正则表达式会将这个字符串看做整体,在整体中进行匹配 对比输出结果: import ...

  2. redis常规命令记录

      概述 因为redis是单线程执行,所以不用关心并发问题. 简单记录一下redis的操作命令,留作查阅,回头再整理一下事物等操作. reids中存储的是kev-value形式, 其中的value有几 ...

  3. 初学JavaScript正则表达式(九)

    分组:可以用 ( ) 来进行分组 一.Byron重复三次             Byron{3} --------- Byronnn 只是将紧挨着量词的字符重复            (Byron) ...

  4. Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) F. Tree Factory 构造题

    F. Tree Factory Bytelandian Tree Factory produces trees for all kinds of industrial applications. Yo ...

  5. [LeetCode] 84. Largest Rectangle in Histogram 直方图中最大的矩形

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  6. golang基础之初识

    golang 简介 很久以前,有一个IT公司,这公司有个传统,允许员工拥有20%自由时间来开发实验性项目.在2007的某一天,公司的几个大牛,正在用c++开发一些比较繁琐但是核心的工作,主要包括庞大的 ...

  7. 微信小程序:使用wx.request()请求后台接收不到参数

    问题描述: 微信小程序:wx.request()请求后台接收不到参数,我通过wx.request()使用POST方式调用请求,参数传递不到后台 解决方案: Content-Type': 'applic ...

  8. Mac终端常用快捷键

    Ctrl + a 跳到行首Ctrl + e 跳到行尾Ctrl + d 删除一个字符,相当于通常的Delete键(命令行若无所有字符,则相当于exit:处理多行标准输入时也表示eof)Ctrl + h ...

  9. iOS性能优化-数组、字典便利时间复杂

    上图是几种时间复杂度的关系,性能优化一定程度上是为了降低程序执行效率减低时间复杂度. 如下是几种时间复杂度的实例: O(1) return array[index] == value; 复制代码 O( ...

  10. axios 源码解析(中) 代码结构

    axios现在最新的版本的是v0.19.0,本节我们来分析一下它的实现源码,首先通过 gitHub地址获取到它的源代码,地址:https://github.com/axios/axios/tree/v ...