首先搭建注册中心,创建一个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. UVA1411 Ants

    想出的一道题竟然是原题QAQ 非常有趣的一个题 根据三角形两边之和大于第三边 所以相交的线段一定是比不相交的线段要长的 所以直接二分图构图 最小费用最大流即可 (我不管我不管我要把这个出到NOIP膜你 ...

  2. atomic 原子操作的类

    import java.util.concurrent.atomic.AtomicInteger; /** * 原子操作的类 atomic */ public class VolatileDemo { ...

  3. bzoj 2435

    http://www.lydsy.com/JudgeOnline/problem.php?id=2435 noi 你为什么那么diao, 这种世纪水题刷一道少一道啊... 我原来还以为是两边的联通块大 ...

  4. hdu 2089 不要62 (数位dp)

    Description 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer). 杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别 ...

  5. CentOS 安装MySQL rpm方式安装

    MySQL源码方式安装:https://www.cnblogs.com/deverz/p/10997723.html 从最新版本的linux系统开始,默认的是 Mariadb而不是mysql!这里依旧 ...

  6. CenterOS 设置静态IP

    本文主要介绍这样再CenterOS 中设定静态IP. 工具 centerOS 6.9 步骤 执行命令:vi /etc/sysconfig/network-scripts/ifcfg-eth0 编辑,填 ...

  7. Objective-C UIWebview JS 交互

    一.在OC中调用网页中的 js 方法. Objective-C 代码 [self.webView stringByEvaluatingJavaScriptFromString:@"alert ...

  8. PHP面试 AJAX基础内容

    AJAX基础内容 Ajax的基本工作原理 Ajax基础概念:通过在后台与服务器进行少量数据交换,Ajax可以使用网页实现异步更新 Ajax工作原理:XMLHttpRequest是Ajax的基础     ...

  9. 图片模式CMYK和RGB在浏览器下的变化

    手机浏览器里面不能显示出模式为CMYK的图片, 模式为CMYK的图片在IE浏览器里面显示和模式为RGB的一样 模式为CMYK的图片在火狐里面显示有色差

  10. HBase 入门之数据刷写(Memstore Flush)详细说明

    接触过 HBase 的同学应该对 HBase 写数据的过程比较熟悉(不熟悉也没关系).HBase 写数据(比如 put.delete)的时候,都是写 WAL(假设 WAL 没有被关闭) ,然后将数据写 ...