SpringCloud快速搭建
1.SpringCloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、负载均衡、微代理、事件总线、全局锁、决策竞选、分布式会话等等。它运行环境简单,可以在开发人员的电脑上跑。另外说明spring cloud是基于Springboot的,所以需要开发中对Springboot有一定的了解。
2.服务提供者与消费关系
就是我我们常说的消费者和生产者
生产者:提供服务给消费者调用
消费者:调用生产者提供的服务,从而实现自身的功能模块
3.服务注册中心Eureka
与duboo类似,duboo我们一般选择zookeper做服务的注册中心,而springcloud是使用Eureka做服务注册中心的。他的作用是就是服务注册与服务发现。以下是比较官方的说明
  官方的介绍在这里Eureka wiki。Eureka是Netflix开源的一个RESTful服务,主要用于服务的注册发现。Eureka由两个组件组成:Eureka服务器和Eureka客户端。Eureka服务器用作服务注册服务器。Eureka客户端是一个java客                                   户端,用来简化与服务器的交互、作为轮询负载均衡器,并提供服务的故障切换支持。Netflix在其生产环境中使用的是另外的客户端,它提供基于流量、资源利用率以及出错状态的加权负载均衡。
       在我看来,Eureka的吸引力来源于以下几点:
开源:大家可以对实现一探究竟,甚至修改源码。
可靠:经过Netflix多年的生产环境考验,使用应该比较靠谱省心
功能齐全:不但提供了完整的注册发现服务,还有Ribbon等可以配合使用的服务。
基于Java:对于Java程序员来说,使用起来,心里比较有底。
spring cloud可以使用Spring Cloud, 与Eureka进行了很好的集成,使用起来非常方便。
4.服务注册
1.那么我们首先创建一个空的maven项目,就叫springcloud
  
创建完之后,将src目录删除,我们需要的只是这个外层的一个框架。
2.创建模块springboot工程
  
这里我的注册中心的项目名是:eureka-server
3.application.yml配置
server:
port: 8888
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
application.yml
4.springboot的核心配置类
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication { public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
} }
EurekaServerApplication.java
@EnableEurekaServer:开启EurekaServer的注解支持
5.启动项目,在浏览器上输入http://localhost:8888/当看到以下页面表示成功搭建注册中心

5.生产者producer
项目创建方式与服务注册中心的创建方式一样
1.引入pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
spring-boot-starter-web
2.核心配置文件
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8888/eureka/
server:
port: 8763
spring:
application:
name: service-producer
application.yml
3.生产者类方法
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList;
import java.util.List; @RestController
public class ProducerController {
@RequestMapping("getUser")
public List<String>getUser(){
List<String> lists = new ArrayList<>();
lists.add("zhangsan");
lists.add("lisi");
lists.add("wangwu");
return lists;
}
}
ProducerController
4.核心配置类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication
@EnableEurekaClient
public class ProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class, args);
}
}
ProducerApplication
@EnableEurekaClient:可以理解为将该项目注册到Eureka中 5.启动项目,刷新http://localhost:8888/可以看到该服务注册到Eurika中
6.消费者consumer
项目搭建与上诉生产者一样的,没什么变化
1.pom依赖,与producer使用的一样(我们现在使用的是rest方式传递信息)
2.application.yml配置文件
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8888/eureka/
server:
port: 8764
spring:
application:
name: service-consumer
application.yml(1)
3.创建service包和controller包
service包下的类:调用服务
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; import java.util.List; @Service
public class ConsumerService {
@Autowired
private RestTemplate restTemplate;
public List<String> getUser(){
List forObject = restTemplate.getForObject("http://service-producer/getUser", List.class);
return forObject;
}
}
ConsumerService
RestTemplate:该类是使用rest方式传递信息的工具类,可以获取到注册到Eurika上的服务
controller包下的类:获取信息
List forObject = restTemplate.getForObject("http://service-producer/getUser", List.class);
第一个参数是请求生产者的url连接,service-producer:生产者在配置文件中配置的名字;第二个是返回值的类型
import com.zy.consumer.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController
public class ConsumerController {
@Autowired
private ConsumerService service;
@RequestMapping("/getUser")
public List<String> getUser(){
List<String> users = service.getUser();
return users;
}
}
ConsumerController
与正常mvc的调用顺序是一样的,没有变化
4.核心配置类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; @SpringBootApplication
@EnableEurekaClient
public class ConsumerApplication { public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
} }
ConsumerApplication
RestTemplate:该类没有做自动配置,需要我们手动注入到spring容器当中去
@LoadBalanced:表示该项目支持负载均衡
5.启动服务(启动顺序:注册中心>生产者>消费者)
使用浏览器请求http://localhost:8764/getUser 可以看到在生产者中创建的集合信息,消费者可以获取到
师承cmy 0.0
SpringCloud快速搭建的更多相关文章
- 快速搭建 SpringCloud 微服务开发环境的脚手架
		
本文适合有 SpringBoot 和 SpringCloud 基础知识的人群,跟着本文可使用和快速搭建 SpringCloud 项目. 本文作者:HelloGitHub-秦人 HelloGitHub ...
 - 「开源」SpringCloud+vue搭建的商城项目
		
最近在研究SpringCloud,看到一个基于SpringCloud+vue搭建的模拟商城项目.用来辅助学习SpringCloud企业级开发还是很有帮助的.强烈推荐!! 源码地址在最后. spring ...
 - spring-cloud项目搭建
		
springCloud项目搭建手册 springcloud应用场景及微服务框架发展趋势 Spring Cloud为开发人员提供了工具,以快速构建分布式系统中的某些常见模式(例如,配置管理,服务发现,断 ...
 - Nginx学习笔记--001-Nginx快速搭建
		
Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器.Nginx是由Igor Sysoev为俄罗斯访问量第二的R ...
 - Github pages + jekyll 博客快速搭建
		
Github pages + jekyll 博客快速搭建 寻找喜欢的模版 https://github.com/jekyll/jekyll/wiki/sites http://jekyllthemes ...
 - NodeJS 最快速搭建一个HttpServer
		
最快速搭建一个HttpServer 在目录里放一个index.html cd D:\Web\InternalWeb start http-server -i -p 8081
 - 利用yeoman快速搭建React+webpack+es6脚手架
		
自从前后端开始分离之后,前端项目工程化也显得越来越重要了,之前写过一篇搭建基于Angular+Requirejs+Grunt的前端项目教程,有兴趣的可以点这里去看 但是有些项目可以使用这种方式,但有些 ...
 - 基于Docker快速搭建多节点Hadoop集群--已验证
		
Docker最核心的特性之一,就是能够将任何应用包括Hadoop打包到Docker镜像中.这篇教程介绍了利用Docker在单机上快速搭建多节点 Hadoop集群的详细步骤.作者在发现目前的Hadoop ...
 - 基于 Jenkins 快速搭建持续集成环境
		
什么是持续集成 随着软件开发复杂度的不断提高,团队开发成员间如何更好地协同工作以确保软件开发的质量已经慢慢成为开发过程中不可回避的问题.尤其是近些年来,敏捷(Agile) 在软件工程领域越来越红火 ...
 
随机推荐
- PAT A1001-A1004
			
题集通道:https://pintia.cn/problem-sets/994805342720868352/problems/type/7 A1001 : A+B Format (20 point ...
 - frp内网穿透,centos7+frp成功样例
			
准备工作: 阿里云服务器一台,备案域名一个,本地服务器一台(本人用的虚拟机centos7) frp文件:frp_0.22.0_linux_amd64.tar.gz 链接:https://pan.bai ...
 - mysql  my.cnf常用的配置
			
[mysqld]basedir = /usr/local/mysql/datadir = /usr/local/mysql/dataport = 3306pid-file = /usr/local/m ...
 - ImageTag,图片左上侧有一个小标签
			
这实现思路其实很简单 需求:1. 图片比原来的<div>大,需要切割图片.2. 图片左上角显示标签 解决思路: 1. 把照片设置成div的backgroundImage,然后用CSS3切割 ...
 - 重载(overloading)和重写@Override
			
一.重写:@Override 定义:字类方法覆盖父类方法,通俗来说就是方法里面的内容可以不一样,其他都一样. (1)必须保证权限大于等于父类的权限public>protetcted>默认& ...
 - 解析java实体类
			
对java实体类的众多理解: A .就是属性类,通常定义在model层里面 B. 一般的实体类对应一个数据表,其中的属性对应数据表中的字段. 好处: 1.对对象实体的封装,体现OO思想. 2.属性可以 ...
 - liunx 常用操作(自用)
			
Centos7解压文件 tar -zxvf 文件名[test.tar.gz] Centos7安装vim yum -y install vim* Centos7安装ifconfig yum instal ...
 - PAT甲级——1146 Topological Order (25分)
			
This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...
 - spark shc hbase 超时问题  hbase.client.scanner.timeout.period 配置
			
异常信息 20/02/27 19:36:21 INFO TaskSetManager: Starting task 17.1 in stage 3.0 (TID 56, 725.slave.adh, ...
 - Disruptor的简单介绍与应用
			
前言 最近工作比较忙,在工作项目中,看了很多人都自己实现了一套数据任务处理机制,个人感觉有点乱,且也方便他人的后续维护,所以想到了一种数据处理模式,即生产者.缓冲队列.消费者的模式来统一大家的实现逻辑 ...