历史上最详细的SpringCloud搭建微服务的过程。(包括注册中心,服务提供者和服务消费者)
首先搭建注册中心,创建一个springboot的maven工程。



工程创建完成之后,先在资源文件中的application.properties中写配置文件。
server.port=
spring.application.name=register
eureka.instance.hostname=localhost
#fetch-registry 拉取服务,作为注册中心的时候,要禁用 设为false
eureka.client.fetch-registry=false
#register-with-eureka 把自己注册到注册中心,当作为注册中心的时候 同样要禁用 设为false
eureka.client.register-with-eureka=false
eureka.client.service-url.default=http://${eureka.instance.hostname}:${server.port}/eureka
在启动类上添加注解 @EnableEurekaServer
@EnableEurekaServer
@SpringBootApplication
public class SpringcloudRegisterApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudRegisterApplication.class, args);
}
}
然后启动测试一下,直接访问http://localhost:801,出现下图则表示访问成功

然后开始创建服务提供者,创建过程不详细说了,依旧是创建一个springboot的maven工程,勾选eureka server的依赖。
先编写配置文件(resources资源下的application.properties文件),设置端口以及eureka的地址
server.port=802
spring.application.name=server-provide
eureka.client.service-url.defaultZone=http://localhost:801/eureka/
然后在启动类加注解 @EnableEurekaClient,表明该项目是客户端,用于提供服务
@EnableEurekaClient
@SpringBootApplication
public class SpringcloudServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudServerApplication.class, args);
}
}
然后在写一个接收请求的代码,用于外界访问,直接在启动类上修改就可以,代码如下
@RestController
@EnableEurekaClient
@SpringBootApplication
public class SpringcloudServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudServerApplication.class, args);
}
@Value("${server.port}") //@Value注解可以把配置文件中的内容赋值到对应属性上
String port;
@RequestMapping("hello")
String test(String name){
return "你好"+name+",该回复来自端口:"+port;
}
}
启动项目,在浏览器访问 http://localhost:802/hello?name=reader,出现下图,即表示成功

最后创建服务消费方,创建工程,勾选eureka server的依赖,修改配置文件,启动类,这里要用restTemplate去访问其他微服务。
配置文件
server.port=
spring.application.name=consumer
eureka.client.service-url.defaultZone=http://localhost:801/eureka/
启动类
@EnableDiscoveryClient //表明该项目是 用于发现eureka的客户端,然后去消费它
@SpringBootApplication
public class SpringcloudServerComsumerApplication { public static void main(String[] args) {
SpringApplication.run(SpringcloudServerComsumerApplication.class, args);
} @Bean //把restTemplate注入到容器中
RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
创建消费的方法,要引入RestTemplate
@Service
public class ConsumerService {
@Autowired
RestTemplate restTemplate;
public String getServer(String name){
return restTemplate.getForObject("http://localhost:802/hello?name="+name,String.class);
}
}
创建消费的controller
@RestController
public class ControllerConsumer {
@Autowired
ConsumerService service;
@RequestMapping("/go")
public String go (String name){
return service.getServer(name);
} }
启动服务之后,先看一下eureka中这两个服务有没有出现

然后访问 http://localhost:803/go?name=reader

至此,项目搭建成功,后续还会更新负载均衡等一系列项目供大家学习。
历史上最详细的SpringCloud搭建微服务的过程。(包括注册中心,服务提供者和服务消费者)的更多相关文章
- 一个C#开发者学习SpringCloud搭建微服务的心路历程
前言 Spring Cloud很火,很多文章都有介绍如何使用,但对于我这种初学者,我需要从创建项目开始学起,所以这些文章对于我的启蒙,帮助不大,所以只好自己写一篇文章,用于备忘. SpringClou ...
- 史上最详细Windows版本搭建安装React Native环境配置 转载,比官网的靠谱亲测可用
史上最详细Windows版本搭建安装React Native环境配置 2016/01/29 | React Native技术文章 | Sky丶清| 95条评论 | 33530 views ...
- SpringCloud(一)之微服务核心组件Eureka(注册中心)的介绍和使用
一 Eureka服务治理体系1.1 服务治理服务治理是微服务架构中最为核心和基础的模块,它主要用来实现各个微服务实例的自动化注册和发现. Spring Cloud Eureka是Spring Clou ...
- SpringCloud 源码系列(1)—— 注册中心 Eureka(上)
Eureka 是 Netflix 公司开源的一个服务注册与发现的组件,和其他 Netflix 公司的服务组件(例如负载均衡.熔断器.网关等)一起,被 Spring Cloud 整合为 Spring C ...
- SpringCloud Alibaba实战(7:nacos注册中心管理微服务)
源码地址:https://gitee.com/fighter3/eshop-project.git 持续更新中-- 在上一节我们已经完成了Nacos Server的本地部署,这一节我们学习如何将Nac ...
- SpringCloud 源码系列(3)—— 注册中心 Eureka(下)
十一.Eureka Server 集群 在实际的生产环境中,可能有几十个或者几百个的微服务实例,Eureka Server 承担了非常高的负载,而且为了保证注册中心高可用,一般都要部署成集群的,下面就 ...
- 微服务系列之 Consul 注册中心
原文链接:https://mrhelloworld.com/posts/spring/spring-cloud/consul-service-registry/ Netflix Eureka 2.X ...
- 【spring cloud】一个ms微服务想要给注册中心eureka发现,需要满足这些条件,微服务不能被eureka注册中心发现的解决方案
在spring cloud中,一个新的微服务想要被注册中心发现,需要注意几个地方: 1.pom.xml文件依赖中需要有这个依赖 spring boot 2.x 需要这个依赖 <dependenc ...
- SpringCloud系列(一):Eureka 注册中心
在演示spring cloud之前得要知道我们为什么需要微服务框架. 先讲讲我的经历,以前我们做项目时所有功能都写在一起,只是做了分层(模型,数据,业务),所有业务逻辑都写在业务层,刚开始还好,等时间 ...
随机推荐
- jvm加载包名和类名相同的类的规则,以及如何加载包名和类名相同的类(转)
jvm包括三种类加载器: 第一种:bootstrap classloader:加载java的核心类. 第二种:extension classloader:负责加载jre的扩展目录中的jar包. 第三种 ...
- Linux常用功能脚本
设置定时任务 crontab -e 1 0 * * * /bin/find /mnt/tomcat/logs/ -mtime +3 -type f -name "*.log" -e ...
- win10 + VS2015 编译 ARPACK
step 1: 下载ARPACK , mingw-w64-install 和 mingw-get-inst-20120426.exe: step 2: 安装 MinGW-64默认安装路径即可. ste ...
- 【leetcode】915. Partition Array into Disjoint Intervals
题目如下: 解题思路:题目要求的是在数组中找到一个下标最小的index,使得index左边(包括自己)子序列的最大值小于或者等于右边序列的最小值.那么我们可以先把数组从最左边开始到数组最右边所有子序列 ...
- Python--多态与多态性、绑定方法与非绑定方法
多态与多态性 多态 多态指的是一类事物有多种形态,(一个抽象类有多个子类,因而多态的概念依赖于继承) 1. 序列类型有多种形态:字符串,列表,元组. s='hello' l=[,,] t=('a',' ...
- Navicat12破解教程
Navicat12破解教程 1.下载Navicat12 并安装,打开Navicat12 点击14天试用,关闭软件 2.下载注册机: 个人百度网盘(版本更新可能不及时):https://pan.baid ...
- BZOJ 3105: [cqoi2013]新Nim游戏(线性基)
解题思路 \(nim\)游戏先手必胜的条件是异或和不为\(0\),也就是说第一个人拿走了若干堆后不管第二个人怎么拿都不能将剩余堆的异或和变成\(0\).考虑线性基,其实就是每个数对线性基都有贡献,任何 ...
- 用MyEclipse将java文件转换成UML类图
用MyEclipse将java文件转换成UML类图 参考: 用MyEclipse将java文件转换成UML类图 - 君临天下的博客 - CSDN博客 http://blog.csdn.net/dan ...
- js千位符 | js 千位分隔符 | js 金额格式化
js 千位分隔符 千位分隔符,其实就是数字中的逗号.依西方的习惯,人们在数字中加进一个符号,以免因数字位数太多而难以看出它的值.所以人们在数字中,每隔三位数加进一个逗号,也就是千位分隔符,以便更加容易 ...
- 建立一个更高级别的查询 API:正确使用Django ORM 的方式
https://www.oschina.net/translate/higher-level-query-api-django-orm