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) 在软件工程领域越来越红火 ...
随机推荐
- 自动化运维工具ansible中常用模块总结
1.yum模块: name:要操作的软件包名字,可以是一个url或者本地rpm包路径,如name=nginx update_cache:更新软件包缓存,如update_cache=yes则更新软件包缓 ...
- 量化交易alpha、beta、shape等基本概念梳理
1.期货型基金(CTA)的 Alpha 和 Beta 是指什么? https://zhuanlan.zhihu.com/p/20700337 1980S ...
- POJ 1860 Currency Exchange【bellman-Ford模板题】
传送门:http://poj.org/problem?id=1860 题意:给出每两种货币之间交换的手续费和汇率,求出从当前货币s开始交换回到s,能否使本金增多. 思路:bellman-Ford模板题 ...
- 201609-1 最大波动 Java
用绝对值,后一天减去前一天即可 import java.util.Scanner; public class Main { public static void main(String[] args) ...
- Spring学习之Aspectj开发实现AOP
Aspectj是一个基于Java语言的Aop框架,它提供了强大的Aop功能. Aspectj简介: 1.Aspectj是一个面向切面的框架,它扩展了Java语言,它定义了一个Aop语法. 2.所以它有 ...
- JQuery 点击子控件事件,不会触发父控件的事件
$('.order-delete').on('tap', function (e) { console.log('删除1'); c ...
- Tensorflow学习教程------模型参数和网络结构保存且载入,输入一张手写数字图片判断是几
首先是模型参数和网络结构的保存 #coding:utf-8 import tensorflow as tf from tensorflow.examples.tutorials.mnist impor ...
- 小白学习之pytorch框架(4)-softmax回归(torch.gather()、torch.argmax()、torch.nn.CrossEntropyLoss())
学习pytorch路程之动手学深度学习-3.4-3.7 置信度.置信区间参考:https://cloud.tencent.com/developer/news/452418 本人感觉还是挺好理解的 交 ...
- ASP.NET core MVC动作过滤器执行顺序
using Microsoft.AspNetCore.Mvc.Filters; using System; using System.Threading.Tasks; namespace dotnet ...
- msgfmt - 翻译汉化
说明 目前大部分自由软件实现国际化使用的是gettext. 国际化就是让程序可以使用多国语言来显示程序里的字符串. 程序里一般都有很多字符串,菜单名也好,错误信息也好,都是字符串.假设字符串为stri ...