• Stars
    star
    163
  • Rank 231,141 (Top 5 %)
  • Language
    Java
  • Created about 9 years ago
  • Updated about 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

spring cloud独立功能介绍和demo 分布式配置服务器、客户端、服务发现、负载均衡、断路器Hytrix

j360-cloud-all

基于spring cloud应用在分布式系统中的运用

##配置文件上下文##

  • bootstrap.yml
  • application.yml
  • 【注】建议采用bootstrap+application组合形式,一般bootstrap配置工程不可变参数,且在启动时需要设定的内容,application配置可变参数

##spring-cloud-configserver##

  • configserver提供了HTTP,为外部资源提供API配置的服务,只需要在Springboot程序中提供@EnableConfigServer注解即可。
    • @EnableConfigServer
    • 增加以下依赖 eureka(可选,作为discover服务器必选)
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-starter-eureka</artifactId>
                </dependency>

###环境变量仓库使用3个变量:###

  • 查看/env可以看到下面三个属性

    • {application} 在客户端映射到"spring.application.name"
    • {profile} 在客户端映射到"spring.active.profiles",使用逗号分隔
    • {label}这是一个服务器端功能标签“版本”的一组配置文件
  • 默认的环境变量库使用git服务,也可以使用文件系统的服务

    • Config Server支持一个或者多个git仓库
spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo
          repos:
            simple: https://github.com/simple/config-repo
            special:
              pattern: pattern*,*pattern1*
              uri: https://github.com/special/config-repo
            local:
              pattern: local*
              uri: file:/home/configsvc/config-repo
   - Every repository can also optionally store config files in sub-directories, and patterns to search for those directories can be specified as searchPaths. 
spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo
          searchPaths: foo,bar*
    - To use HTTP basic authentication on the remote repository add the "username" and "password" properties separately (not in the URL), e.g
spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo
          username: trolley
          password: strongpassword
  • 文件系统后端

    • 使用 spring.profiles.active=native
  • 嵌入式ConfigServer

    • 官方建议独立部署

##spring-cloud-client##

  • 使用以下依赖
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
  • 配置优先启动

    • 默认的springcloud应用程序启动行为,所有的客户端应用程序想要使用配置服务器的配置,需要:
  • Eureka优先启动

    • 如果使用的是Spring CloudNetflix 和eureka service discovery,可以使用配置服务器注册Eureka
  • 环境变量刷新

    • post -> localhost:8080/refresh
    • 定时
    • 手动
The Config Server itself is stateless, so you can spin up as many as these as you need and find them via eureka. Underneath the server itself, the git implementation you point to needs to be highly available as well. So if you point to github (private or public), then git is as available as github is. If the config server can't reach git it will continue to serve what it has checked out even if it is stale.
As far as gradual config changes, you could use a different branch and configure the canary to use that branch via spring.cloud.config.label and them merge the branch. You could also use profiles (eg application-<profilename>.properties) and configure the canary to use the specified profile.
I think the branch makes a little more sense, because you wouldn't have to reconfigure the non-canary nodes to use the new profile each time, just configure canary to use the branch.
Either way, the only time apps see config chages (when using spring cloud config client) is on startup or when you POST to /refresh on each node. You can also POST to /bus/refresh?destination=<servicename> if you use the Spring Cloud Bus to refresh all instances of a service at once.
  • 基于spring cloud应用在分布式系统中的运用
    • config
    • eureka
    • monitor
    • servicediscover

##Spring Cloud Netflix##

  • 应用程序+Netflix 组件构建大型分布式系统。包括
    • 服务发现(Eureka)
    • 断路器(Hystrix)
    • 智能路由(Zuul)
    • 客户端负载均衡(Ribbon)

###服务发现(Eureka)###

  • Eureka Client

  • 注册

    • @EnableEurekaClient
    • application.yml
        eureka:
          client:
            serviceUrl:
              defaultZone: http://localhost:8761/eureka/
    
  • Eureka Server

  • @EnableEurekaServer

  • 【注】服务器有一个主页界面,HTTP API Eureka功能端点 /eureka/ ####Standalone Mode####

  • application.yml (Standalone Eureka Server)

    • serviceUrl指向同一个本地实例的host
server:
  port: 8761
eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

####High Availability, Zones and Regions####

  • ...

###断路器(Hystrix) ###

  • Hystrix Client
  • Hystrix DashBoard

###智能路由(Zuul) ###

  • Netflix uses Zuul for the following:
    • Authentication
    • Insights
    • Stress Testing
    • Canary Testing
    • Dynamic Routing
    • Service Migration
    • Load Shedding
    • Security
    • Static Response handling
    • Active/Active traffic management

###客户端负载均衡(Ribbon) ###

  • Ribbon提供客户端的负载均衡,可以为http和tcp客户端提供控制功能,Feign 同样使用Ribbon
  • 定制Ribbon客户端
  • Eureka中使用Ribbon

  • 直接使用RibbonAPI
public class MyClass {
    @Autowired
    private LoadBalancerClient loadBalancer;
    public void doStuff() {
        ServiceInstance instance = loadBalancer.choose("stores");
        URI storesUri = URI.create(String.format("http://%s:%s", instance.getHost(), instance.getPort()));
        // ... do something with the URI
    }
}

##声明式 REST Client: Feign##

  • Spring Cloud整合Ribbon和Eureka提供负载均衡的http client时使用Feign.

  • 实例案例

@Configuration
@ComponentScan
@EnableAutoConfiguration
@EnableEurekaClient
@EnableFeignClients
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
  • 调用(StoreClient.java)
@FeignClient("stores")
public interface StoreClient {
    @RequestMapping(method = RequestMethod.GET, value = "/stores")
    List<Store> getStores();
    @RequestMapping(method = RequestMethod.POST, value = "/stores/{storeId}", consumes = "application/json")
    Store update(@PathParameter("storeId") Long storeId, Store store);
}

###继承支持###

UserService.java
public interface UserService {
    @RequestMapping(method = RequestMethod.GET, value ="/users/{id}")
    User getUser(@PathVariable("id") long id);
}
UserResource.java
@RestController
public class UserResource implements UserService {
}
UserClient.java
@FeignClient("users")
public interface UserClient extends UserService {
}

##外部配置: Archaius##

  • netfilx客户端配置框架
    • push changes到客户端
    • poll 资源来更新资源
  • 通常不直接使用

##Spring Cloud Bus##

  • Spring Cloud Bus通过轻量级消息代理连接分布式系统的每个节点
  • 添加依赖spring-cloud-starter-bus-amqp
  • Rabbit config
spring:
  rabbitmq:
    host: mybroker.com
    port: 5672
    username: user
    password: secret
  • http端点
    • /bus/*
    • /bus/env 发送key/values键值对更新spring环境变量
    • /bus/refresh 更新所有的环境变量

##Spring cloud Zookeeper##

  • zookeeper仅仅是作为数据存储的地方,同eureka一样,但是eureka简单很多,目前推荐eureka

##Spring cloud Cluster##

  • 分布式系统集成zookeeper、redis等,实现选举、分布式锁、一致性状态检查等等,目测该项目还未完成start自动配置
    • zookeeper 依赖 spring-cloud-cluster-zookeeper
    • redis 依赖 spring-cloud-cluster-redis

More Repositories

1

j360-dubbo-app-all

基于dubbo的分布式工程开发规范实例工程,分布式跟踪、ID生成、分布式事务、分布式治理、分表分库、分布式锁、选举、分布式配置、API文档生成器...
Java
483
star
2

j360-tools

Java底层知识点、源码解读,技术栈相关原理知识点、工具解读最佳实践、功能点实战,问题排查,开发技巧等
Java
130
star
3

j360-boot

j360-boot --- 使用Spring Boot封装j360,Spring Boot入门到深入学习demo
Java
110
star
4

j360-boot-app-all

基于Spring boot 2.0+ 脚手架重构微服务系统,CI、DevOps、Redisson、Shiro、RocketMQ tx、Dubbo、Sentinel、Sharding-Sphere、Fescar、Skywalking、Apollo、SocketIO, 小程序服务端快速上手Sample, Netty Socket Game/PUSH Server
Java
72
star
5

j360-trace

分布式链路跟踪监控插件,开箱即用,使用方式参考module:j360-trace-example , 本项目供学习之用,参考Google Dapper论文设计,收集存储分离,使用kafka,http,mq等进行解耦
Java
61
star
6

j360-datasource

分布式数据源分表分库、读写分离应用层框架
Java
55
star
7

j360-microservice

基于springboot和docker部署的微服务架构
Java
51
star
8

j360-jdk

读书笔记系列之JDK,高性能编程
Java
34
star
9

j360-idgen

ID生成服务器,多种高性能ID生成策略
Java
17
star
10

j360-redis-async-db

j360系列 - 缓存异步写数据库的框架
Java
15
star
11

j360-shiro

shiro在app客户端和web应用中的使用,shiro-springmvc/springboot/jwt
Java
13
star
12

dctemplate

数据清洗模板程序(spring batch)
Java
11
star
13

j360-gitflow

gitflow工作流使用规范
11
star
14

j360-canal-binlog-river

binlog工具,elasticsearch river、kafka river、cache sync
Java
10
star
15

j360-docker

j360-boot系列在docker中的发布使用
9
star
16

j360-admin

j360-admin(ajax版本) j360系列admin前端,bootstrap、jquery、模板语言、异步内容加载
JavaScript
8
star
17

j360

All in j360 for J2EE.
Java
8
star
18

j360-mysql-proxy

轻量级Mysql代理服务器
Java
7
star
19

j360-kafka-storm

storm使用案例,simple storm,storm-kafka,trident stream and example
Java
6
star
20

j360-conf

maven(war、jar)工程代码配置分离策略,支持多profile+单元测试+maven jetty:run插件
Java
4
star
21

j360-raft

Raft 实现指南
Java
4
star
22

j360-jni-all

JNI处理对接接口Sample, 图片处理、音视频、裁剪、滤镜,Java->C, C->Java, C Callback
Java
4
star
23

j360-cloud

基于spring cloud应用在分布式系统中的运用
Java
4
star
24

j360-instrumenter-all

instrumenter工具汇总
Java
3
star
25

j360-book-list

书单
3
star
26

j360-spark-parent

spark kafka spark-streaming 计算DMP
Java
3
star
27

j360-tcc

lite tcc for java, based on j360-rpc or others
Java
3
star
28

j360-lts

lts框架集成和分析
Java
2
star
29

j360-protobuf

java protobuf and redis
Java
2
star
30

j360-motan

weibo motan rpc demos for test.
2
star
31

j360-framework-parent

j360-framework 微服务通用核心构建框架,0基础快速构建Spring Boot,Dubbo RPC,Shiro,Common Util,SocketIO,Boot-Starters
Java
2
star
32

j360-swagger

Http Web API文档描述服务集成配置、打包、服务化shell,nginx + JWT + swagge/ spring-boot/springmvc/embeded jetty
Java
2
star
33

j360-activemq

1
star
34

xuminwlt.github.io

博客地址:
HTML
1
star
35

j360-zipkin-parent

zipkin分布式链路跟踪生产环境集成
Java
1
star
36

config-repo

j360-cloud config repo
1
star
37

j360-netty-parent

netty功能抽象和应用集合
Java
1
star
38

j360-boot-kafka-counter

可定制计数器 (PV、赞数、评论数、发帖数等)
Java
1
star