《深入理解Springcloud与微服务》

key word

工匠精神
OO(object oriented)
雪崩效应,
CAP理论 consistency,avaliability,partition,微服务AP

basic infomation of micro service


微服务的特点(image)
服务之间通信数据格式(json,xml)(protobuf)
springcloud的熔断机制(Hystrix).
熔断器的作用:1,防止雪崩效应,设置阀值,失败数超过阀值会开启熔断,服务休眠
                            2,自我修复机制
consistency:常见问题,分布式事务,2阶段提交(image)。

驱动架构的发展一定是业务的发展。没有最好的框架和工具,关键适合业务和需求。
微服务架构的三大难题(服务故障传播性,服务的划分,分布式事务)。

微服务基本模块

微服务特点

Component of springcloud

config:服务配置中心
eureka:服务注册发现
hystrix:熔断
zuul:路由网关
robbon:负载均衡
sleuth:链路追踪
stream:消息流
ps:netfix四件套:eureka,hystrix,zuul,archaius(api管理)

路由是微服务架构中必须(integral )的一部分,比如,“/” 可能映射到你的WEB程序上,”/api/users “可能映射到你的用户服务上,
api/shop”可能映射到你的商品服务商。(注解:我理解这里的这几个映射就是说通过Zuul这个网关把服务映射到不同的服务商去处理,从而变成了微服务!)

springcloud:框架-〉微服务。
k8s:容器编排-〉微服务。

eureka

server基本配置

#服务注册中心端口号
server.port=1110
#服务注册中心实例的主机名
eureka.instance.hostname=localhost
#是否向服务注册中心注册自己,防止自己注册自己
eureka.client.register-with-eureka=false
#是否检索服务
eureka.client.fetch-registry=false
#服务注册中心的配置内容,指定服务注册中心的位置
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

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

client基本配置

#在此指定服务注册中心地址
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${eureka.port}/eureka/
server.port=8762
spring.application.name=eureka-client

@EnableEurekaClient
@SpringBootApplication
public class IefopEurekaClientApplication {
         publicstatic void main(String[] args) {
                   SpringApplication.run(IefopEurekaClientApplication.class,args);
         }
}

基本概念:
register:clinet向server注册,client提供自己元数据.
renew:30s一次心跳,超过90s,server剔除client.
fecth register:client从server获取服务注册信息.
cancel:client下线对server的请求
eviction:超过90s服务剔除。

集群eureka
配置文件类似server.配置2个不同的
portal1:--server.port=18088
portal1:--server.port=18087

robbon

负载均衡的2种实现模式:
    1,独立服务做负载均衡策略,实现请求转发,如nginx.
    2,负责均衡封装到消费者,消费者维护一份服务提供列表,通过负责均衡策略请求不同的服务提供者。如robbon

基本配置:robbon+resttemplate实现负载均衡

server:
  port: 8010
spring:
  application:
    name: microservice-consumer-movie
eureka:
  client:
    serviceUrl:
      defaultZone:http://localhost:8761/eureka/

@EnableEurekaClient
@SpringBootApplication
public class IefopEurekaClientApplication {
         publicstatic void main(String[] args) {
                   SpringApplication.run(IefopEurekaClientApplication.class,args);
         }
}

@configuration
public class RibbonConfiguration {
    @Bean
    @LoadBlanced
   RestTemplate resttemplate(){
        return new RestTemplate ();
    }

@RestController
public class controller{
@AutoWired
Service service;
...method..{service.hi(params)}
}

@Service
public class service{
@Autowired
Resttemplate resttemplate;
public String hi(params){
return resttemplate.getForObject("apiurl",String.class);}
}


hystrix

@EnableEurekaClient
@EnableHystrix
@SpringBootApplication
public class IefopEurekaClientApplication {
         publicstatic void main(String[] args) {
                   SpringApplication.run(IefopEurekaClientApplication.class,args);
         }
}

@Service
public class service{
@Autowired
Resttemplate resttemplate
//当apiurl提供的服务不可用的时候,hi这个方法会打开熔断,熔断方式为fallbackMethod
@HystrixCommand(fallbackMethod="hiError")
public String hi(params){
return resttemplate.getForObject("apiurl",String.class);}
}

Config

server
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

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


spring.application.name=config-server
server.port=8888
spring.cloud.config.server.git.uri=https://github.com/forezp/SpringcloudConfig/
spring.cloud.config.server.git.searchPaths=respo
spring.cloud.config.label=master
spring.cloud.config.server.git.username=your username
spring.cloud.config.server.git.password=your password

远程仓库[https://github.com/forezp/SpringcloudConfig/](https://github.com/forezp/SpringcloudConfig/) 中有个文件config-client-dev.properties
foo = foo version 3

client 
spring.application.name=config-client
spring.cloud.config.label=master
spring.cloud.config.profile=dev
spring.cloud.config.uri= http://localhost:8888/
server.port=8881
@SpringBootApplication
@RestController

public class ConfigClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }
    @Value("${foo}")
    String foo;
    @RequestMapping(value = "/hi")
    public String hi(){
        return foo;
    }
}
访问得到:foo version 3


zuul

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

@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
@EnableDiscoveryClient
public class ServiceZuulApplication {

    public static void main(String[] args) {
        SpringApplication.run( ServiceZuulApplication.class, args );
    }
}
以/api-a/ 开头的请求都转发给service-ribbon服务;
以/api-b/开头的请求都转发给service-feign服务;

//过滤
@Component
public class MyFilter extends ZuulFilter {
    private static Logger log = LoggerFactory.getLogger(MyFilter.class);
    @Override
    public String filterType() { return "pre";
     }
   @Override
    public int filterOrder() {    return 0;
    }
    @Override
    public boolean shouldFilter() {return true;
    }
    @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;
    }
}

filterType:返回一个字符串代表过滤器的类型,在zuul中定义了四种不同生命周期的过滤器类型,具体如下: 
pre:路由之前
routing:路由之时
post: 路由之后
error:发送错误调用
filterOrder:过滤的顺序
shouldFilter:这里可以写逻辑判断,是否要过滤,本文true,永远过滤。
run:过滤器的具体逻辑。可用很复杂,包括查sql,nosql去判断该请求到底有没有权限访问。

sleuth

Springcloud的sleuth是集成twitter的Zipkin。

server-zipkin,它的主要作用使用ZipkinServer 的功能,收集调用数据,并展示;
一个service-hi,对外暴露hi接口;
一个service-miya,对外暴露miya接口;

server-zipkin
java -jar zipkin-server-2.10.1-exec.jar,访问浏览器localhost:9494

service-hi
server.port=8988
spring.zipkin.base-url=http://localhost:9411
spring.application.name=service-hi

    @RequestMapping("/info")
    public String info(){
        LOG.log(Level.INFO, "calling trace service-hi ");

        return "i'm service-hi";

    }

service-miya
server.port=8989
spring.zipkin.base-url=http://localhost:9411
spring.application.name=service-miya

   @RequestMapping("/miya")
    public String info(){
        LOG.log(Level.INFO, "info is being called");
        return restTemplate.getForObject("http://localhost:8988/info",String.class);
    }

miya调用hi,2者都注册到链路追踪服务中。spring.zipkin.base-url=http://localhost:9411
2279594-48cfbe426b23b7a5.png

refer

https://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/multi/multi_spring-cloud.html
https://blog.csdn.net/forezp
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容