1、什么是springcloud
1.SpringCloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、负载均衡、微代理、事件总线、全局锁、决策竞选、分布式会话等等。它运行环境简单,可以在开发人员的电脑上跑。另外说明spring cloud是基于Springboot的,所以要求开发中对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、创建springcloud微服务项目,服务注册
①、create new project
②、new project,下一步
③、此项目是springcloud微服务项目,那么我就随便起个名叫cloud,下一步
④、finish
⑤、进行cloud服务项目后,删除里面的src目录,我们需要的只是这个外层的一个框架,并将maven配置好(你们自己的maven项目本地位置)
⑥、file->new->module
⑦、创建springboot项目,下面我就快速搭建了,我的博客中都有详细搭建springboot教程。next
我们就以一个电商为例来创建,每个系统对应一个springboot工程,下面假设这是一个购物车系统,ok下一步
添加依赖,下一步
finish
看一下大致结构
重复上一创建springboot操作,最后完成如下图这样(这只是我举得例子)
5、对每个系统进行布局及配置
①、之前在创建springboot项目时候已经添加过Eureka Server依赖,接下来在启动类中加上@EnableEurekaServer 注解,它表示开启EurekaServer的注解支持
②、手动添加static和templates文件夹在resources目录下,这样静态数据才会生效。(教大家一个小技巧,再添加yml配置文件时,可以右击resources然后选择file,在里面输入application-dev.yml即可自动生成,prod也一样操作)
6、配置yml文件
①、配置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/
7、启动项目,在浏览器上输入http://localhost:8888/当看到以下页面表示成功搭建注册中心
8、生产环境与开发环境的配置
application.yml指向application-prod.yml
spring:
profiles:
active: prod
①、配置生产环境producer
1、引入pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2、核心配置文件
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8888/eureka/
server:
port: 8763
spring:
application:
name: service-producer
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;
}
}
4、启动类方法
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient // 开启EurekaServer的注解支持
public class ProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class, args);
}
}
②、配置开发环境consumer
application.yml指向application-dev.yml
spring:
profiles:
active: dev
1、引入pom依赖
<!-- 引入前端web,此注解不存在Controller层注解会报一系列错误 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.7.RELEASE</version>
</dependency>
2、核心配置文件
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8888/eureka/
server:
port: 8764
spring:
application:
name: service-consumer
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;
}
}
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;
}
}
与正常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();
}
}
RestTemplate:该类没有做自动配置,需要我们手动注入到spring容器当中去
@LoadBalanced:表示该项目支持负载均衡
5.启动服务(启动顺序:注册中心>生产者>消费者)
使用浏览器请求http://localhost:8764/getUser 可以看到在生产者中创建的集合信息,消费者可以获取到