资讯专栏INFORMATION COLUMN

Spring Cloud 快速入门

fuyi501 / 368人阅读

摘要:服务注册中心一个服务注册中心,所有的服务都在注册中心注册,负载均衡也是通过在注册中心注册的服务来使用一定策略来实现。在客户端实现了负载均衡。

文章参考于史上最简单的 SpringCloud 教程 | 终章

Spring Cloud 是一个微服务框架,与 Spring Boot 结合,开发简单。将一个大工程项目,分成多个小 web 服务工程,可以分别独立扩展,又可以共同合作。

环境

spring 官网的 sts 3.9.2,就是有spring 相关插件的eclipse;

apache maven 3.5.4,配置阿里云镜像

jdk1.8

Spring Cloud Finchley版本

Spring Boot 2.0.3

Spring Cloud 组件

服务注册中心、服务、断路器、配置中心

服务的注册与发现 - Eureka

使用 Eureka 来实现服务注册中心、服务提供者和服务消费者。

服务注册中心

一个服务注册中心,所有的服务都在注册中心注册,负载均衡也是通过在注册中心注册的服务来使用一定策略来实现。

1.新建一个 Spring Boot 工程,用来管理服务,elipse右键 -> new -> Spring Starter Project :

点击 next ,选择 Cloud Discovery 下的 Eureka Server 组件

生成的 pom 文件:



    4.0.0

    com.beigege.cloud
    eureka-server
    0.0.1-SNAPSHOT
    jar

    eureka-server
    spring cloud学习

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.3.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
        Finchley.RELEASE
    

    
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        
    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



2.使用 @EnableEurekaServer 来说明项目是一个 Eureka:

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

3.修改配置文件,配置文件,可以是 application.properties 或 application.yml,这里改后缀,使用 yml,两种格式内容都是一样的 只是格式不同 application.yml:

server:
  port: 8761 #服务端口

spring:
  application:
    name: eurka-server #服务应用名称

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false #是否将自己注册到Eureka Server,默认为true
    fetchRegistry: false #是否从Eureka Server获取注册信息,默认为true
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #服务注册的 URL

默认 eureka server 也是一个 eureka client,registerWithEureka: false 和 fetchRegistry: false 来表明项目是一个 Eureka Server。

4.运行 EurekaServerApplication 类,启动项目,访问项目 http://localhost:8761/

服务提供者

1.新建项目名为 eureka-client 的 Spring Boot 项目,跟 Eureka Server 新建一样,只是在选择 Spring Boot 组件的时候,不选 Eureka Server ,选择 Eureka Client,这里为了测试,再添加一个 Web 下的 Web 组件:

2.修改配置,application.yml:

server:
  port: 8762
  
spring:
  application:
    name: service-hi #服务之间的调用都是根据这个 name
    
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

3.添加 @EnableEurekaClient 注解,并添加一个测试接口 /hi:

@SpringBootApplication
@EnableEurekaClient
@RestController
public class EurekaClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApplication.class, args);
    }

    @Value("${server.port}")
    String port;

    @RequestMapping("/hi")
    public String home(@RequestParam(value = "name", defaultValue = "beigege") String name) {
        return "hi " + name + " ,i am from port:" + port;
    }
}

4.启动 Eureka Server 和 Eureka Client 项目,访问http://localhost:8761 ,即eureka server 的网址:

服务消费者 - Ribbon | Feign

Spring Cloud 两种调用服务的方式,ribbon + restTemplate,和 feign。

Ribbon

ribbon 在客户端实现了负载均衡。

启动 Eureka Server 和 Eureka Client,修改 Eureka Client 端口,再启动一个 Eureka Client 实例,相当于一个小的集群,访问localhost:8761

1.新建一个 spring boot 工程,名称为 service-ribbon,创建过程和 eureka-client 一样,组件多选一个 Cloud Routing 下的 Ribbon,创建完成之后的 pom 文件依赖:

        
            org.springframework.cloud
            spring-cloud-starter-netflix-ribbon
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-hystrix
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

application.yml 配置:

server:
  port: 8764
  
spring:
  application:
    name: service-ribbon
    
eureka: 
  client: 
    service-url: 
      defaultZone: http://localhost:8761/eureka/

ServiceRibbonApplication 加上 @EnableEurekaClient 注解

2.向 Spring 注入一个 bean: restTemplate,并添加 @LoadBalanced 注解,表明这个 restTemplate 开启负载均衡功能:

@SpringBootApplication
@EnableEurekaClient
public class ServiceRibbonApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceRibbonApplication.class, args);
    }
    
    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

3.写一个接口用来调用之前的 service-hi 服务的 /hi 接口:
新建 service 层,名叫 HelloService,调用 /hi 接口,这里用服务名 SERVICE-HI 代替具体的 URL:

@Service
public class HelloService {

    @Autowired
    RestTemplate restTemplate;

    public String hiService(String name) {
        return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
    }

}

新建 controller 层,调用 service 层:

@RestController
public class HelloControler {

    @Autowired
    HelloService helloService;
    @RequestMapping(value = "/hi")
    public String hi(@RequestParam String name){
        return helloService.hiService(name);
    }
}

4.启动 service-ribbon 工程,多次访问该工程的 /hi 接口,即 localhost:8764/hi?name=test:

hi test ,i am from port:8762
hi test ,i am from port:8763

上面交替显示,说明实现了负载均衡,ribbon 会在客户端发送请求时做一个负载均衡。

Feign

整合了 Ribbon,具有负载均衡的能力,整合了Hystrix,具有熔断的能力.

1.创建一个 spring boot工程,创建过程和 eureka-client 一样,多添加一个组件 Cloud Routing 下的 Feign,项目名叫 service-feign,创建完后的 pom 文件依赖:

        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

application.yml 配置文件:

server:
  port: 8765

spring:
  application:
    name: service-feign

eureka:
  client:
    serviceUrl: 
      defaultZone: http://localhost:8761/eureka/

SericeFeignApplication 上添加 @EnableEurekaClient 和 @EnableFeignClients 注解:

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class SericeFeignApplication {

    public static void main(String[] args) {
        SpringApplication.run(SericeFeignApplication.class, args);
    }
}

2.写一个 SchedualServiceHi 接口,通过@ FeignClient(“服务名”),来指定调用哪个服务,@GetMapping("接口名"),来向接口发送 Get 请求,@RequestParam 是请求参数:

@FeignClient(value = "service-hi")
public interface SchedualServiceHi {

    @GetMapping("/hi")
    String sayHiFromClientOne(@RequestParam(value = "name") String name);
    
}

写一个 controller 层测试 SchedualServiceHi 接口:

@RestController
public class HiController {
    
    @Autowired
    SchedualServiceHi schedualServiceHi;

    @GetMapping(value = "/hi")
    public String sayHi(@RequestParam String name) {
        return schedualServiceHi.sayHiFromClientOne( name );
    }

}

3.启动 service-feign,访问localhost:8765/hi?name=test 测试,下面交替显示,说明实现了负载均衡:

hi test ,i am from port:8762 
hi test ,i am from port:8763
断路器 - Hystrix

链式调用服务,其中一个服务宕机,其他服务可能会跟着异常,发生雪崩,当对一个服务的调用失败次数到达一定阈值,断路器会打开,执行服务调用失败时的处理,避免连锁故障,上面已经启动:eureka-server、两个 eureka-client 实例、service-ribbon 和 service-feign,下面继续修改,分别在 service-ribbon 和 service-feign 上实现断路器。

service-ribbon + Hystrix

1.在 pom 文件中加入 Hystrix 依赖:


    org.springframework.cloud
    spring-cloud-starter-netflix-hystrix

2.在程序的启动类 ServiceRibbonApplication 加 @EnableHystrix 注解开启Hystrix:

@SpringBootApplication
@EnableEurekaClient
@EnableHystrix
public class ServiceRibbonApplication {

    public static void main(String[] args) {
        SpringApplication.run( ServiceRibbonApplication.class, args );
    }

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

}

3.添加熔断方法,改造 HelloService 类,在 hiService 方法上面添加 @HystrixCommand 注解,fallbackMethod 是熔断方法,当服务不可用时会执行,该方法,即 hiError 方法:

@Service
public class HelloService {

    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "hiError")
    public String hiService(String name) {
        return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
    }

    public String hiError(String name) {
        return "hi,"+name+",sorry,error!";
    }

}

重启 service-ribbon,访问 localhost:8764/hi?name=test

hi test ,i am from port:8763

关闭两个 eureka-client 实例,在访问 localhost:8764/hi?name=test,浏览器显示:

hi,test,sorry,error!

当服务不可用时,断路器会迅速执行熔断方法

service-feign + Hystrix

1.Feign 自带断路器,在D版本的Spring Cloud之后,它没有默认打开。需要在配置文件中配置打开:

feign: 
  hystrix: 
    enabled: true

2.在请求接口指定熔断实现类,fallback 指定熔断实现类:

@FeignClient(value ="service-hi",fallback = SchedualServiceHiHystric.class)
public interface SchedualServiceHi {
    
    @GetMapping("/hi")
    String sayHiFromClientOne(@RequestParam(value = "name") String name);

}

SchedualServiceHiHystric 类实现 SchedualServiceHi 接口:

@Component
public class SchedualServiceHiHystric implements SchedualServiceHi{

    @Override
    public String sayHiFromClientOne(String name) {
        return "sorry "+name;
    }

}

3.访问测试,重启 service-feign,访问 localhost:8765/hi?name=test:

sorry test

打开 eureka-client,再次访问 localhost:8765/hi?name=test:

hi test ,i am from port:8762

证明断路器起到作用,注意浏览器缓存。

路由网关 - Zuul

1.创建一个 spring boot 工程,名称为 service-zuul,创建过程和 eureka-client 一样,组件要多添加一个 Cloud Routing 下的 Zuul,pom 文件的依赖如下:

        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-zuul
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        

2.在入口 application 中添加 @EnableZuulProxy 注解,开启路由:

@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class ServiceZuulApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceZuulApplication.class, args);
    }
}

application.yml 配置:

server:
  port: 8769
 
spring:
  application:
    name: service-zuul
    
eureka:
  client:
    serviceUrl: 
      defaultZone: http://localhost:8761/eureka/
添加路由

修改 application.yml 添加路由配置:

server:
  port: 8769
 
spring:
  application:
    name: service-zuul
    
eureka:
  client:
    serviceUrl: 
      defaultZone: http://localhost:8761/eureka/
      
zuul:
  routes:
    api-a: 
      path: /api-a/**
      service-id: service-ribbon
    api-b:
      path: /api-b/**
      service-id: service-feign

8769 端口的所有 api-a 请求会转发到 service-ribbon 工程,api-b 到 service-feign

启动 service-zuul,访问 localhost:8769/api-a/hi?name=test,浏览器显示:

hi test ,i am from port:8762

访问 localhost:8769/api-b/hi?name=test,浏览器显示:

hi test ,i am from port:8762

说明路由成功。

过滤器

新建自定义过滤器,继承 ZuulFilter:

@Component
public class MyFilter extends ZuulFilter {

    private static Logger log = LoggerFactory.getLogger(MyFilter.class);

    /*
     * filterType:返回一个字符串代表过滤器的类型,在zuul中定义了四种不同生命周期的过滤器类型,具体如下:
     * pre:路由之前
     * routing:路由之时 
     * post: 路由之后 
     * error:发送错误调用 
     */
    @Override
    public String filterType() {
        return "pre";
    }

    /*
     * filterOrder:过滤的顺序
     */
    @Override
    public int filterOrder() {
        return 0;
    }

    /*
     * shouldFilter:这里可以写逻辑判断,是否要过滤,本文true,永远过滤。
     */
    @Override
    public boolean shouldFilter() {
        return true;
    }

    /*
     * run:过滤器的具体逻辑。可以很复杂,包括查sql,nosql去判断该请求到底有没有权限访问,
     * 这里是判断请求有没有 token 参数
     */
    @Override
    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        log.info(String.format("%s >>> %s", request.getMethod(), request.getRequestURL().toString()));
        Object accessToken = request.getParameter("token");
        if(accessToken == null) {
            log.warn("token is empty");
            ctx.setSendZuulResponse(false);
            ctx.setResponseStatusCode(401);
            try {
                ctx.getResponse().getWriter().write("token is empty");
            }catch (Exception e){}

            return null;
        }
        log.info("ok");
        return null;
    }
}

重启 service-zuul 访问 localhost:8769/api-a/hi?name=test,浏览器显示

token is empty

加上 token 参数:localhost:8769/api-a/hi?name=test&token=666

hi test ,i am from port:8762
分布式配置中心 远程资源库

1.这里在 码云 注册一个账号,用来创建存储配置的资源库,在码云上创建一个名叫 SpringCloudConfig 项目:

2.新建一个配置文件 config-client-dev.properties,也可以是 yml 配置文件,内容为 test = version 1,然后提交:

2.默认是 master 分支,新建一个分支,名字叫 aa:

修改 aa 分支中的 config-client-dev.properties 配置文件的 test 属性为 version 2:

config-server

1.新建 spring boot 项目,组件选择 Cloud Config 中的 Config Server,pom 中的依赖:

        
            org.springframework.cloud
            spring-cloud-config-server
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

在入口程序添加 @EnableConfigServer 注解:

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

application.yml 配置文件,如果是公开的不需要填写用户名密码:

server:
  port: 8888
  
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/candy-boy/SpringCloudConfig  #配置git仓库地址,后面的.git可有可无 
          username:  #访问git仓库的用户名,码云登陆用户名
          password:  #访问git仓库的用户密码,码云登陆密码

2.访问测试,启动程序,访问配置文件,可以如下访问:

{application}/{profile}[/{label}]

如 localhost:8888/config-client/dev,即访问 config-client-dev.properties,其中 {application} 就是 最后一道横线前面的 config-client,{profile} 是最后一道横线后面到点,即 dev,{label} 指的是资源库的分支,不填则为默认分支,刚创建的资源库,默认分支是 master,访问结果如下:

其他访问方式如:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

如果是下面这样,可能是用户名或密码错误:

3.测试其他分支,访问 aa 分支下的 config-client-dev.properties,localhost:8888/config-client/dev/aa

config-client

待续....

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/76532.html

相关文章

  • Spring Cloud 参考文档(Spring Cloud Config快速入门

    摘要:快速入门这个快速入门使用的服务器和客户端。属性在端点中显示为高优先级属性源,如以下示例所示。名为的属性源包含值为且具有最高优先级的属性。属性源名称中的是存储库,而不是配置服务器。 Spring Cloud Config快速入门 这个快速入门使用Spring Cloud Config Server的服务器和客户端。 首先,启动服务器,如下所示: $ cd spring-cloud-con...

    gekylin 评论0 收藏0
  • springcloud(二)——spring-cloud-alibaba集成sentinel入门

    摘要:介绍随着微服务的流行,服务和服务之间的稳定性变得越来越重要。以流量为切入点,从流量控制熔断降级系统负载保护等多个维度保护服务的稳定性。完备的实时监控同时提供实时的监控功能。您只需要引入相应的依赖并进行简单的配置即可快速地接入。 Sentinel 介绍 随着微服务的流行,服务和服务之间的稳定性变得越来越重要。 Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度...

    darkbug 评论0 收藏0
  • Spring Cloud构建微服务架构:消息驱动的微服务(入门)【Dalston版】

    摘要:它通过使用来连接消息代理中间件以实现消息事件驱动的微服务应用。该示例主要目标是构建一个基于的微服务应用,这个微服务应用将通过使用消息中间件来接收消息并将消息打印到日志中。下面我们通过编写生产消息的单元测试用例来完善我们的入门内容。 之前在写Spring Boot基础教程的时候写过一篇《Spring Boot中使用RabbitMQ》。在该文中,我们通过简单的配置和注解就能实现向Rabbi...

    smallStone 评论0 收藏0
  • 使用maven快速入门

    摘要:基础知识官网传送门项目结构文件文件代表工程对象模型它是使用工作的基本组件,位于工程根目录。表示被依赖的仅参与测试相关的处理,包裹测试代码的编译,执行。 Maven 基础知识 官网: 传送门 Maven 项目结构 $ MavenProject |-- pom.xml |-- src | |-- main | | `-- java | | `-- resources |...

    HelKyle 评论0 收藏0
  • 2019年java架构师视频

    摘要:并发专题一内存模型原理实现机制单例模式并发基础并发集合原子操作基本类型线程池互联网工程专题二简介安装工具编程介绍,入门程序仓库依赖管理简介常用操作命令 01.并发专题(一)2018-09-11(1)-Java内存模型2018-09-13(2)-synchronized原理2018-09-16(3)-volatile实现机制2018-09-18(4)-DCL-单例模式2018-09-21...

    Object 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<