赞
踩
等个30s?
Individual services registering will take up to 30 seconds to show up in the Eureka service because Eureka requires three consecutive heartbeat pings from the service spaced 10 seconds apart before it will say the service is ready for use.
- eureka:
- instance:
- preferIpAddress: true
- client:
- registerWithEureka: true
- fetchRegistry: true
- serviceUrl:
- defaultZone: http://localhost:8761/eureka/
- ResponseEntity< Organization > restExchange =
- restTemplate.exchange(
- serviceUri, //手工从ServiceInstance拼接?
- HttpMethod.GET,
- null, Organization.class, organizationId);
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-hystrix</artifactId>
- </dependency>
- <dependency>
- <groupId>com.netflix.hystrix</groupId>
- <artifactId>hystrix-javanica</artifactId>
- <version>1.5.9</version>
- </dependency>
Bulkheads
- @HystrixCommand(fallbackMethod = "buildFallbackLicenseList",
- threadPoolKey = "licenseByOrgThreadPool",
- threadPoolProperties =
- {@HystrixProperty(name="coreSize", value="30"),
- @HystrixProperty(name="maxQueueSize", value="10")} //产品环境中config值应外部配置
- )
当maxQueueSize=-1时使用SynchronousQueue,这将使得线程池用光时后续rpc阻塞等待;>0使用LinkedBlockingQueue。
Netflix推荐下列公式:(requests per second at peak when the service is healthy * 99th percentile latency in seconds) + small amount of extra threads for overhead.
10-second window?当至少15个rpc调用通过窗口时,将计算timeout失败率,并决定是否“断路”
断路之后的恢复尝试:这似乎违反了“fail fast”原则?
不同的隔离策略:
Introducing Spring Cloud and Netflix Zuul(不就是个反向代理嘛,为什么不用NginX来实现)
- eureka:
- instance:
- preferIpAddress: true
- client:
- registerWithEureka: true
- fetchRegistry: true
- serviceUrl:
- defaultZone: http://localhost:8761/eureka/
配置路由
- zuul:
- prefix: /api
- routes:
- organizationservice: /organization/**
- hystrix.command.`licensingservice`.execution.isolation.thread.timeoutInMilliseconds: 7000
- licensingservice.ribbon.ReadTimeout: 70
Using the correlation ID in your service calls
靠,这里的代码看着好恶心... 不就是一个随header传递的tmx-correlation-id嘛,一堆垃圾代码!
- private static final ThreadLocal<UserContext> userContext = new ThreadLocal<>();
- ......
-
- RestTemplate template = new RestTemplate();
- List interceptors = template.getInterceptors();
- if (interceptors==null){
- template.setInterceptors(
- Collections.singletonList(
- new UserContextInterceptor()));
- } else{
- interceptors.add(new UserContextInterceptor());
- template.setInterceptors(interceptors);
- }
Log aggregation?Spring Cloud Sleuth?
OAuth2认证服务器
需要实现用户token与资源之间的授权关系???还是说仅仅处理认证?
- @Configuration
- public class ResourceServerConfiguration extends
- ResourceServerConfigurerAdapter {
- @Override
- public void configure(HttpSecurity http) throws Exception{
- http
- .authorizeRequests()
- .antMatchers(HttpMethod.DELETE, "/v1/organizations/**")
- .hasRole("ADMIN")
- .anyRequest()
- .authenticated();
- }
- }
Spring Cloud Stream
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-stream-kafka</artifactId>
- </dependency>
- @Autowired
- public SimpleSourceBean(Source source){
- this.source = source;
- }
- source
- .output()
- .send(
- MessageBuilder
- .withPayload(change)
- .build());
- spring:
- application:
- name: organizationservice
- stream:
- bindings:
- output:
- destination: orgChangeTopic
- content-type: application/json
- kafka:
- binder:
- zkNodes: localhost
- brokers: localhost
simpleSourceBean.publishOrgChange("SAVE", org.getId());
- @EnableBinding(Sink.class)
- public class Application {
- @StreamListener(Sink.INPUT)
- public void loggerSink(
- OrganizationChangeModel orgChange) { ... } //注意这里全局的listener方法;
- ...
- input:
- destination: orgChangeTopic
- content-type: application/json
- group: licensingGroup
A Spring Cloud Stream use case: distributed caching
- <dependency>
- <groupId>org.springframework.data</groupId>
- <artifactId>spring-data-redis</artifactId>
- <version>1.7.4.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>redis.clients</groupId>
- <artifactId>jedis</artifactId>
- <version>2.9.0</version>
- </dependency>
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-pool2</artifactId>
- <version>2.0</version>
- </dependency>
- @Bean
- public RedisTemplate<String, Object> redisTemplate() {
- RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
- template.setConnectionFactory(jedisConnectionFactory());
- return template;
- }
Spring Data
to access your Redis store, you need to define a repository class.
- @Autowired
- private OrganizationRedisRepositoryImpl(RedisTemplate redisTemplate) {
- this.redisTemplate = redisTemplate;
- }
- @PostConstruct
- private void init() {
- //import org.springframework.data.redis.core.HashOperations;
- hashOperations = redisTemplate.opsForHash(); //见鬼
- }
Defining custom channels
- import org.springframework.cloud.stream.annotation.Input;
- import org.springframework.messaging.SubscribableChannel;
-
- public interface CustomChannels {
- @Input("inboundOrgChanges")
- SubscribableChannel orgs();
- }
如果是发布消息:
- @OutputChannel(“outboundOrg”) //怎么感觉这里的标注不一致?
- MessageChannel outboundOrg();
本书里构建的微服务应用还真是简单,没有复杂的业务逻辑处理,或者复杂的数据设计及OLTP事务处理。期待可以看到深层次的微服务架构设计实例解析。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。