《深入理解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)。

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

《深入理解Springcloud与微服务》_第1张图片
微服务基本模块

《深入理解Springcloud与微服务》_第2张图片
微服务特点

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
《深入理解Springcloud与微服务》_第3张图片
2279594-48cfbe426b23b7a5.png

refer

https://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/multi/multi_spring-cloud.html
https://blog.csdn.net/forezp

你可能感兴趣的:(《深入理解Springcloud与微服务》)