SpringCloud入门

单体应用缺点

1、扩展能力受限:不方便局部扩展
2、复杂性高:项目大,代码臃肿
3、不方便开发,不方便维护,升级
4、模块、业务耦合度高
5、一个模块挂掉,整个项目挂掉。一个模块升级,整个项目重启
6、技术选型单一

微服务

将一个大的应用拆分成多个小的应用(服务)。每个小的应用相对独立,并且都有自己的容器(Tomcat),有自己的进程。
这些小的服务通过网络协议(Http Rest)进行通信。

微服务优点

1、方便局部扩展
2、技术选型多样化
3、单个微服务复杂性低
4、单个微服务易于开发和维护
5、微服务之间相对松耦合
6、数据库选型多样化
7、当项目规模大,微服务整体性能好

微服务缺点

1、微服务之间数据交互速度受网络影响
2、技术成本,开发成本高
3、整个项目总体复杂
4、微服务部署麻烦


SpringCloud组件

1、服务注册发现——Netflix Eureka : 管理服务的通信地址
2、客服端负载均衡——Netflix Ribbon\Feign : 解决服务请求与分发(根nginx很像)
3、断路器——Netflix Hystrix :解决微服务故障的
4、服务网关——Netflix Zuul :微服务的大门(安保部门)
5、分布式配置——Spring Cloud Config :统一管理微服务的配置
SpringCloud注册中心(Eureka)
微服务启动时会把 服务IP+端口+服务名称 提交给注册中心
并且从注册中心下载服务通信地址清单
当某个微服务挂掉,注册中心会把该服务标记为下线,其他服务也会同步通信地址清单
SpringCloud项目结构搭建
1、springcloud-parent(父工程)
2、子模块
springcloud-eureka-server-1000(注册中心)
springcloud-producer-user-server-2000
springcloud-consumer-pay-server-3000
父工程pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.5.RELEASE</version>
</parent>
<!--2.管理SpringCloud的jar包-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
EurekaServer搭建

1、pom.xml

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

2、主配置类EurekaServerApplication1000

@SpringBootApplication
@EnableEurekaServer//开启注册中心
public class EurekaServerApplication1000 {
    public static void main( String[] args ) {
        SpringApplication.run(EurekaServerApplication1000.class);
    }
}

3、application.yml

server:
  port: 1000
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false #不注册到Eureka
    fetch-registry: false #不从注册中心获取服务
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka # http://localhost:1000/eureka
EurekaClient搭建

1、pom.xml

<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--  集成Web的jar包-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

2、主配置类

/**
 * 用户服务主配置类
 * @EnableDiscoveryClient :开启服务发现 (开启注册中心的客户端功能)
 * @EnableEurekaClient : 开启Eureka client客户端(只是针对Eureka有用)
 */
//@EnableDiscoveryClient
//@EnableEurekaClient
@SpringBootApplication
public class UserServerApplication2000 {
    public static void main( String[] args ) {
        SpringApplication.run(UserServerApplication2000.class);
    }
}

3、appication.yml

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:1000/eureka/,http://localhost:1001/eureka/ #注册中心服务端的注册地址
  instance:
    prefer-ip-address: true #使用ip进行注册
    instance-id: user-server:2000  #服务注册到注册中心的id
server:
  port: 2000
#应用的名字
spring:
  application:
    name: user-server
Eureka集群搭建(高可用)

application.xml

#使用SpringBoot多环境配置的方式来配置 2个 注册中心
#主配置
spring:
  profiles:
    active: peer1 #peer2两个都要启动   #你激活谁,启动的时候就是用的谁的配置
---
#第一个EurekaServer的配置
application.yml
spring:
  profiles: peer1
  application:
    name: eureka-server
eureka:
  instance:
    hostname: peer1
    prefer-ip-address: true
    instance-id: eureka-server:1000
  client:
    serviceUrl:
      defaultZone: http://peer2:1001/eureka/
server:
  port: 1000
---
#第二个EurekaServer的配置
spring:
  profiles: peer2
  application:
    name: eureka-server
eureka:
  instance:
    hostname: peer2
    prefer-ip-address: true
    instance-id: eureka-server:1001
  client:
    serviceUrl:
      defaultZone: http://peer1:1000/eureka/
server:
  port: 1001
微服务之间通信

1、发送方主配置类

@LoadBalanced//负载均衡
@Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

2、发送方controller

@Autowired
    private RestTemplate restTemplate;
    @GetMapping("/pay/user/{id}")
    public User pay(@PathVariable("id")Long id){
        //远程调用user-server
        //java代码发送http请求
        //String url = "http://localhost:2000/user/"+id;
        String url = "http://user-server/user/"+id;
        return restTemplate.getForObject(url,User.class);
    }
Ribbon-----客户端负载均衡

Ribbon工作原理:
微服务之间通信,需要制定被调用方的服务名,被调用方做了集群,
服务名对应两个ip端口,ribbon服务名找到这两个服务,ribbon按照负载均衡算法(轮询/随机)调用其中一个服务
调用方继承ribbon:
1、导包

<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>

2、主配置类
RestTemplate 的Bean上打标签@LoadBalanced//负载均衡
发送方controller的url这样String url = “http://user-server/user/”+id;
负载均衡策略:默认轮询
随机负载均衡算法:主配置类

 //随机负载均衡
    @Bean
    public IRule randomRule(){
        return new RandomRule();
    }
Feign-----底层基于Ribbon

1、导包

<!--集成Feign-->
<dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

2、调用方FeignClient接口

@FeignClient(value = "user-server",fallback = UserFeignClientFallback.class)//feign的客户端接口,可以实现远程调用
public interface UserFeignClient {
    @GetMapping(value = "/user/{id}")
    public User getUserById(@PathVariable("id") Long id);//要求与目标服务的controller一样(url、参数、返回值)
}

3、调用方controller

 @Autowired
    private UserFeignClient userFeignClient;
    @GetMapping("/user/{id}")
    public User getUserById(@PathVariable("id")Long id){
        return userFeignClient.getUserById(id);
    }

4、用方主配置类

@EnableFeignClients("com.lxn.feignclients")//开启Feign
Hystrix-----断路器:解决微服务故障

分布式系统雪崩效应:一个微服务的故障导致整个微服务调用链全部瘫痪
Hystrix:解决服务器故障(雪崩)的一个组件 ,它可以实现:隔离 ,熔断 ,降级,缓存
1、隔离 :包括线程池隔离和信号量隔离,限制调用分布式服务的资源使用,某一个调用的服务出现问题不会影响其他服务调用。
2、熔断 :当请求次数达到规定的阀值都出现服务故障(超时),Hystrix就把服务标记为短路状态.
正常情况下,断路器处于关闭状态(Closed),如果调用持续出错或者超时,电路被打开进入熔断状态(Open),后续一段时间内的所有调 用都会被拒绝(Fail Fast),一段时间以后,保护器会尝试进入半熔断状态(Half-Open),允许少量请求进来尝试,
如果调用仍然失败,则回到熔断状态
如果调用成功,则回到电路闭合状态;
3、降级 :高并发情况下 ,为了保证一些主要的服务有足够的资源不出问题 ,会人为的关掉一些无关紧要的服务,然后返回一些托底的数据,给用户一个友好的提示。
4、缓存 :Hystrix内部会把请求做缓存

Ribbon集成Hystrix

1、调用方导包

<dependency>
      <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

2、调用方主配置类上打标签
@EnableCircuitBreaker//开启hystrix
3、调用方controller方法熔断打标签
@HystrixCommand(fallbackMethod = “payFallback”)
4、调用方controller中的payFallback托底方法

public User payFallback(@PathVariable("id")Long id){
     return new User(-1L,"ribbon:用户服务不可用!");
}
Feign集成Hystrix

Feign已经集成了Hystrix
1、application.yml中开启Hystrix

#开启Hystrix
feign:
  hystrix:
    enabled: true

2、在被调用方FeignClient客户端接口上打标签
@FeignClient(value = “user-server”,fallback = UserFeignClientFallback.class)//feign的客户端接口,可以实现远程调用
3、托底类实现FeignClient客户端接口

//托底类--谁的托底类就实现谁
@Component
public class UserFeignClientFallback implements UserFeignClient{
    @Override
    public User getUserById(Long id) {
        return new User(-1L,"feign:用户服务不可用!");
    }
}

4、配置饥饿加载
application.yml

ribbon:
      eager-load:
        enabled: true #饥饿加载
服务网关——Netflix Zuul

zuul作为独立的应用,zuul作为微服务群的请求入口,维护着微服务的安全,
可用通过zuul实现统一权限校验,限流,请求日志/监控,负载均衡(请求分发)等功能
也需要注册到Eureka
集成Zuul
1、pom.xml

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--集成zuul-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>
        <!--  集成Web的jar包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.62</version>
        </dependency>

2、zuul主配置类开启zuul标签
@EnableZuulProxy//开启zuul代理


路由访问地址:http://localhost:9527/user-server/user/1
Zuul外网部署,其他微服务内网部署

Zuul路由配置
zuul的application.yml

zuul:
  ignored-services: "*" #禁止浏览器通过服务名直接访问微服务
  routes:
    pay-server: "/pay/**" #给pay-server取个别名/pay/**
    
ribbon: #ribbon超时
  ReadTimeout: 30000
  ConnectTimeout: 30000
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 40000

路由访问地址:http://localhost:9527/pay/pay/user/1

自定义zuulFilter实现登录检查

自定义Filter类继承ZuulFilter

//登录检查
@Component
public class LoginCheckFilter extends ZuulFilter {
    //filter类型  前置Filter
    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }
    //执行顺序,值越小越先执行
    @Override
    public int filterOrder() {
        return 1;
    }
    //true则执行run方法,false不执行run方法
    @Override
    public boolean shouldFilter() {
        //拿到请求对象
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        String requestURI = request.getRequestURI();
        //如果请求路径/login不需要登录检查,否则需要登录检查
        if(StringUtils.hasLength(requestURI) && requestURI.endsWith("login")){
            //是登录请求
            return false;
        }
        return true;
    }
    //登录检查
    @Override
    public Object run() throws ZuulException {
        //获取到请求头中的Token
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        String token = request.getHeader("token");
        //没有token
        if(!StringUtils.hasLength(token)){
            errorResponse();
            return null;
        }
        //有token,获取Redis中查询登录信息,集成Redis
        AjaxResult ajaxResult = redisFeignClient.get(token);
        if(!ajaxResult.isSuccess() || ajaxResult.getResultObj() == null){
            errorResponse();
        }
        return null;
    }
    private void errorResponse(){
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletResponse response = currentContext.getResponse();
        response.setContentType("application/json;charset=utf-8");
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
		//不再继续执行了
        currentContext.setSendZuulResponse(false);
        // 返回错误信息 AjaxResult
        AjaxResult ajaxResult = AjaxResult.me().setSuccess(false).setMessage("请登录!");

        try {
            response.getWriter().print(JSON.toJSONString(ajaxResult));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

使用Postman设置请求头测试
在这里插入图片描述

SpringCloud Config分布式配置中心

微服务架构中,每个项目都有一个yml配置,管理起来麻烦。要使用spring cloud config来统一管理。
在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client。
搭建配置中心:
1、pom.xml

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--  集成Web的jar包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

2、主配置类上打标签
@EnableConfigServer//开启配置中心
3、application.yml码云配置

#应用的名字
spring:
  application:
    name: config-server
  #码云配置
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/用户名/springcloud-config.git
          username: xxx(用户名)
          password: (密码)

测试访问:http://localhost:5200/(文件名)

微服务集成配置中心客户端

1、导包

<dependency>
     <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

2、将application.yml改为application-user-dev.yml,并且提交到码云上
3、bootstrap.yml

#指向配置中心 ,拉取配置文件
spring:
  cloud:
    config:
      uri: http://localhost:6000  #配置中心的地址 application-user-dev.yml
      name: application-user         #配置文件名字
      profile: dev  #环境
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值