一、网站架构演变过程
微服务架构 为什么出现了SpringCloud
网站架构模式:
单点应用---->分布式系统面向于服务架构(SOA)体系 webservice---->微服务架构
web项目三层架构

如果在互联网公司中,使用传统架构技术开发会产生代码冲突。
所以需要拆分项目:
这就产生了分布式开发
分布式架构:就是将一个大项目,拆分成n个多个子项目。
如:会员系统、支付系统、消息系统、微信系统
集群:将同一个项目,部署在多个服务器上。war包都相同
面向服务架构优点:代码服务、解耦、只适合大公司,人多的情况下使用。
缺点:网络延迟、分布式事务问题
小公司传统项目:
分布式架构系统:就是将一个大项目,拆分成n多个子项目,
再使用rpc远程调用技术。
你用过哪些rpc远程调用框架:
springCloud、Dubbo、HttpClient、Hessian
SOA:面向于服务架构
通讯协议SOAP:就是Http协议+xml传输的。反序列化与反序列化。
二、微服务架构概述
1、微服务架构(分布式架构):是在传统的SOA架构领域升级的。
微:就是细分
轻量级、通讯协议是Http协议+restful分格+json
来源:

注意:每一个服务都对应着一个数据库
RPC远程调用:使用http协议+json格式+restful分格
使用比较简单的通讯就是使用httpclient
接口只能允许在内网访问和外网进行接口对接。
主流的rpc解决框架:dubbo 、springCloud
微服务架构与面向服务架构区别:
面向与服务架构SOA:主要针对银行里面,xml格式。企业级的传统项目
微服务系统中:会更加细分。http+json+restful轻量级。独立运行
三、SpringCloud概述
1、接口地址怎么管理?有没有容错机制?负载均衡、网关、路由策略、
接口限流、断路。
微服务解决框架:SpringCloud
SpringCloud解决什么样的问题?
答:SpringCloud是做rpc远程调用的、配置管理、注册中心(Eureka、ZK)、
服务发现(表示服务可以被别人调用) 服务注册、断路器、路由策略、负载均衡、
全局锁、分布式会话系统、客户端调用。
接口网关(ZUUL)、服务管理系统。
SpringBoot与SpringCloud区别?
答:SpringBoot是简化xm配置的,能快速整合框架
SpringCloud是一套微服务解决方案,主要用在RPC远程调用。
关系是SpringCloud依赖接口(SpringMVC)与SpringBoot来实现的。
四、服务注册与服务发现
1、SpringCloud注册中心环境搭建 euraka
2、服务的注册与发现
3、SpringCloud客户端调用:rest调用、feign调用:客户端调用
ribbon:负载均衡。
zuul:接口网关。
euraka:服务注册。
案例:
会员服务提供用户信息
订单服务:查询订单
订单服务需要查询用户,订单服务调用会员接口。

五、搭建注册中心 euraka
注册中心的作用:管理服务的。
案例环境搭建:
第一步:搭建注册中心euraka
第二步:搭建接口提供工程
第三步:使用接口
一、搭建Eureka注册中心
pom.xml:
4.0.0
com.leeue
eureka-server
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE
UTF-8
UTF-8
1.8
org.springframework.cloud
spring-cloud-starter-eureka-server
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
Dalston.RC1
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
2、application.yml文件
server:
port: 8888
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
# eureka地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
启动类
package com.leeue.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer //这个就是服务
public class EurakaApp {
public static void main(String[] args) {
SpringApplication.run(EurakaApp.class, args);
}
}
运行效果:

二、搭建服务提供者 接口提供 service-member
pom.xml
4.0.0
com.leeue
39_service-member
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE
UTF-8
UTF-8
1.8
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
Dalston.RC1
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
application.yml
# 配置注册中心地址
eureka:
client:
serviceUrl:
# 地址 加上/eureka
defaultZone: http://localhost:8888/eureka/
server:
port: 8762
spring:
application:
name: service-member
启动类:
package com.leeue;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient //表示注册到注册中心里面去
public class MemberApp {
public static void main(String[] args) {
SpringApplication.run(MemberApp.class, args);
}
}
启动类
package com.leeue;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient //表示注册到注册中心里面去 注意是client
public class MemberApp {
public static void main(String[] args) {
SpringApplication.run(MemberApp.class, args);
}
}
运行后在eureka里面会找到这个服务

三、消费会员服务
服务调用使用:rest、feign
负载均衡使用ribbon
pom.xml
4.0.0
com.leeue
39_service-order
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE
UTF-8
UTF-8
1.8
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.cloud
spring-cloud-starter-ribbon
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
Dalston.RC1
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
application.yml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8888/eureka/
server:
port: 8764
spring:
application:
name: service-order
service
package com.leeue.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
/**
* 使用rest方式调用接口
* @classDesc: 功能描述:()
* @author:李月
* @Version:v1.0
* @createTime:2018年11月2日 下午4:44:55
*/
@Service
public class OrderMemberService {
@Autowired
private RestTemplate restTemplate; //注入rest模版
public List getOrderUser(){
return restTemplate.getForObject("http://service-member/getMemberAll", List.class);
}
}
control
package com.leeue.control;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.leeue.service.OrderMemberService;
@RestController
public class OrderController {
@Autowired
OrderMemberService orderMemberService;
@RequestMapping("/getOrderAll")
public List getOrderAll(){
System.out.println("订单服务调用会员服务");
return orderMemberService.getOrderUser();
}
}
启动类 启动类要注意 注入@RestTemplate
package com.leeue;
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 OrderApp {
public static void main(String[] args) {
SpringApplication.run(OrderApp.class, args);
}
/**
* 下面代码是 把
* @return
*/
@Bean //把ResTemplate注入到 Spring容器中去
@LoadBalanced //表示支持负载均衡
RestTemplate restTemplate() {
return new RestTemplate();
}
}
六、SpringCloud中rest、fegin调用原理
底层使用的技术原理分析:底层还是使用httpclient来调用。
两个服务都注册到注册中心,地址也在注册中心,如果一个服务要调用另一个服务,
就在注册中心获取到地址,然后本地使用httpclient进行请求资源。
