首先搭建注册中心,创建一个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搭建微服务的过程。(包括注册中心,服务提供者和服务消费者)的更多相关文章

  1. 一个C#开发者学习SpringCloud搭建微服务的心路历程

    前言 Spring Cloud很火,很多文章都有介绍如何使用,但对于我这种初学者,我需要从创建项目开始学起,所以这些文章对于我的启蒙,帮助不大,所以只好自己写一篇文章,用于备忘. SpringClou ...

  2. 史上最详细Windows版本搭建安装React Native环境配置 转载,比官网的靠谱亲测可用

    史上最详细Windows版本搭建安装React Native环境配置   2016/01/29 |  React Native技术文章 |  Sky丶清|  95条评论 |  33530 views ...

  3. SpringCloud(一)之微服务核心组件Eureka(注册中心)的介绍和使用

    一 Eureka服务治理体系1.1 服务治理服务治理是微服务架构中最为核心和基础的模块,它主要用来实现各个微服务实例的自动化注册和发现. Spring Cloud Eureka是Spring Clou ...

  4. SpringCloud 源码系列(1)—— 注册中心 Eureka(上)

    Eureka 是 Netflix 公司开源的一个服务注册与发现的组件,和其他 Netflix 公司的服务组件(例如负载均衡.熔断器.网关等)一起,被 Spring Cloud 整合为 Spring C ...

  5. SpringCloud Alibaba实战(7:nacos注册中心管理微服务)

    源码地址:https://gitee.com/fighter3/eshop-project.git 持续更新中-- 在上一节我们已经完成了Nacos Server的本地部署,这一节我们学习如何将Nac ...

  6. SpringCloud 源码系列(3)—— 注册中心 Eureka(下)

    十一.Eureka Server 集群 在实际的生产环境中,可能有几十个或者几百个的微服务实例,Eureka Server 承担了非常高的负载,而且为了保证注册中心高可用,一般都要部署成集群的,下面就 ...

  7. 微服务系列之 Consul 注册中心

    原文链接:https://mrhelloworld.com/posts/spring/spring-cloud/consul-service-registry/ Netflix Eureka 2.X ...

  8. 【spring cloud】一个ms微服务想要给注册中心eureka发现,需要满足这些条件,微服务不能被eureka注册中心发现的解决方案

    在spring cloud中,一个新的微服务想要被注册中心发现,需要注意几个地方: 1.pom.xml文件依赖中需要有这个依赖 spring boot 2.x 需要这个依赖 <dependenc ...

  9. SpringCloud系列(一):Eureka 注册中心

    在演示spring cloud之前得要知道我们为什么需要微服务框架. 先讲讲我的经历,以前我们做项目时所有功能都写在一起,只是做了分层(模型,数据,业务),所有业务逻辑都写在业务层,刚开始还好,等时间 ...

随机推荐

  1. Java集合体系结构(List、Set、Collection、Map的区别和联系)

    Java集合体系结构(List.Set.Collection.Map的区别和联系) 1.Collection 接口存储一组不唯一,无序的对象 2.List 接口存储一组不唯一,有序(插入顺序)的对象 ...

  2. ganglia监控部署

    1.ganglia组件 ganglia 相比于falcon和zabbix主要在于集群的状态集中显示,可以很便捷的对比各主机的性能状态. gmond:相当于是agent端,主要用于收集各node的性能状 ...

  3. 深度 | 带领国产数据库走向世界,POLARDB底层逻辑是什么?

    POLARDB 是阿里云自主研发的下一代云原生分布式数据库,100%兼容MySQL.PostgreSQL等开源数据库,高度兼容Oracle语法,使用RDS服务的客户不需要修改应用代码,可以一键迁移到P ...

  4. <Jmeter入门不放弃>之<2.常用功能>

    大家这里参考学习的时候,我就不在这里配截图了,因为需要你打开工具根据文档自己去找,才有印象,大家一定要启动JMeter!跟着理解操作 一.测试计划 用来描述一个性能测试,所有内容都是基于这个计划,这谁 ...

  5. SQL Server数据库备份&还原

    一.备份 1.登录数据库 2.找到要还原的数据库 右键-任务-备份-添加(路径只写一个,刚开始二个总是报错)-确定 二.还原数据库 这个之间报错了二次 1.报错1:备份集中的数据库与现有数据库“XXX ...

  6. flutter环境搭建及跑起来demo(多图慎入)

    话不多说,直接上 [1]环境搭建 从git上面clone下来 git clone -b beta https://github.com/flutter/flutter.git 由于国内网络的问题,我就 ...

  7. 爬取猎聘大数据岗位相关信息--Python

    猎聘网站搜索大数据关键字,只能显示100页,爬取这一百页的相关信息,以便做分析. __author__ = 'Fred Zhao' import requests from bs4 import Be ...

  8. NtCallbackReturn是否导致了用户态栈的不平衡

    0:000> u ntdll!KiFastSystemCall ntdll!KiFastSystemCall: 7c92eb8b 8bd4 mov edx,esp 7c92eb8d 0f34 s ...

  9. pat甲级题目1001 A+B Format详解

    pat1001 A+B Format (20 分) Calculate a+b and output the sum in standard format -- that is, the digits ...

  10. Pandas DataFrame操作

    DataFrame的创建 >>> import pandas as pd >>> from pandas import DataFrame #define a di ...