• 微服务-springcloud


    感觉微服务都差不多概念,最近稍微看了下springcloud,感觉入门还是很简单的,框架用用就那么回事,深入的话需要很多时间投入了

    学一个东西,我推荐首先从概念上了解到他是做什么的,什么时候需要,基本模块是什么,然后可以自己写一些小的例子,后续根据需要深入到探寻源码

    某位热心同学写的入门例子,我下载学习了下:http://git.oschina.net/zhou666/spring-cloud-7simple

    集成了了Netfix的一些关键组件:

    服务发现——Netflix Eureka

    客服端负载均衡——Netflix Ribbon

    断路器——Netflix Hystrix

    服务网关——Netflix Zuul

    分布式配置——Spring Cloud Config

    Eureka服务发现,启动后可以在控制台看到其他配置了该eureka地址的服务

    application.properties中定义eureka服务相关信息

    server.port=1111
    eureka.instance.hostname=localhost
    eureka.client.registerWithEureka=false
    eureka.client.fetchRegistry=false
    eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
    spring.application.name=cloud-eureka-server
    manager url:http://localhost:1111/

    spring-boot 注解方式启动服务

    @SpringBootApplication
    @EnableEurekaServer
    public class EurekaServer {
        public static void main(String[] args) {
            SpringApplication.run(EurekaServer.class, args);
        }
    }

    打开控制台可以看到eureka服务已经启动成功

    启动两个server2222/2223,在配置中制定eureka的服务地址

    spring.application.name=ribbon-consumer
    server.port=2222
    eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
    @EnableDiscoveryClient
    @SpringBootApplication
    public class ComputeServiceApplication {
        public static void main(String[] args) {
            new SpringApplicationBuilder(ComputeServiceApplication.class).web(true).run(args);
        }
    }

    启动后可以看到eureka控制台有这两个服务出现

    修改端口为3333

    启动ribbon轮询负载均衡器:

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

    可以看到eureka可以发现该服务,并且访问http://localhost:3333/add?a=1&b=2时,后台分别传递给了2222和2223实现了负载均衡调度

    Hystrix熔断机制:

    application.yml

    server:
      port: 8989
    
    spring:
      application:
        name: turbine
      cloud:
        config:
          enabled: true
          uri: http://localhost:8888
    eureka:
      instance:
        leaseRenewalIntervalInSeconds: 10
      client:
        registerWithEureka: true
        fetchRegistry: true
        serviceUrl:
          defaultZone: http://localhost:1111/eureka/
    
    turbine:
      aggregator:
        clusterConfig: CLOUD-SIMPLE-UI
      appConfig: cloud-simple-ui
      clusterNameExpression: metadata['cluster']
    @SpringBootApplication
    @EnableEurekaClient
    @EnableHystrixDashboard
    @EnableTurbine
    public class TurbineApplication {
    
        public static void main(String[] args) {
             SpringApplication.run(TurbineApplication.class, args);
        }
    }

    Config配置管理:

    server.port=8888
    spring.cloud.config.server.git.uri=https://git.oschina.net/zhou666/spring-cloud-7simple.git
    spring.cloud.config.server.git.searchPaths=cloud-config-repo
    eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
    spring.application.name=cloud-config-server
    @Configuration
    @EnableAutoConfiguration
    @EnableDiscoveryClient
    @EnableConfigServer
    public class ConfigServerApplication {
        public static void main(String[] args) {
            SpringApplication.run(ConfigServerApplication.class, args);
        }
    }

    zuul服务网关,类似Nginx反向代理服务器,配置各种Ip过来给哪个下级处理

    logging:
      level.org.springframework.cloud: DEBUG
    server: 
      port: 8080
    zuul:
      ignoredPatterns: /health,/error
      retryable: true  
      routes:
        smarts:
          stripPrefix: true
          path: /smart/**  
          serviceId: smarts
    ribbon:
      eureka:
        enabled: false
    smarts:
      ribbon:
        listOfServers: localhost:2222,localhost:2223

    定义映射规则,当访问http://localhost:8080/smart/add?a=1&b=2会按照规则/smart/**将请求定位到2222/2223

    @EnableZuulProxy
    @SpringBootApplication
    public class ZuulApplication {
        public static void main(String[] args) {
            SpringApplication.run(ZuulApplication.class, args);
        }
    }
  • 相关阅读:
    研发环境容器化实施过程(docker + docker-compose + jenkins)
    Java虚拟机-字节码执行引擎
    Java虚拟机-类加载机制
    Java虚拟机-字节码指令
    Java虚拟机-类文件结构
    Java虚拟机理解-内存管理
    单元测试实践(SpringCloud+Junit5+Mockito+DataMocker)
    Git基础概念与Flow流程介绍
    谷歌最佳实践
    谷歌最佳实践
  • 原文地址:https://www.cnblogs.com/it-worker365/p/7008195.html
Copyright © 2020-2023  润新知