架构师学习第18周——微服务架构认知,服务治理Eureka

基础组件的认识

  1. Eureka——服务治理
  2. Ribbon——负载均衡
  3. Hystrix——服务容错
  4. Feign——服务间调用
  5. Config——分布式配置中心
  6. Bus——消息总线
  7. GateWay——服务网关
  8. Sleuth——调用链追踪
  9. Stream——消息驱动
  10. Sentinel——限流

架构师角度如何权衡服务的利弊

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

阿里新零售业务商品中心的微服务过程

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

电商系统微服务化思想

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

服务治理组件比较

在这里插入图片描述

解读注册中心UI

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

Eureka使用

eureka-server(注册中心)
application.properties

spring.application.name=eureka-server
server.port=20000

eureka.instance.hostname=localhost
#禁止自己当做服务注册
eureka.client.register-with-eureka=false
#表示是否从Eureka Server获取注册的服务信息
eureka.client.fetch-registry=false

#强制关闭服务自保(自动开关不起作用)
eureka.server.enable-self-preservation=false

#每个多久触发一次服务剔除
eureka.server.eviction-interval-timer-in-ms=

启动类

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

eureka-client(发布到服务中心的服务)
application.properties

spring.application.name=eureka-client
server.port=30000
eureka.client.serviceUrl.defaultZone=http://localhost:20000/eureka/
#每个5秒钟,向服务中心发送一条续约指令
eureka.instance.lease.renewal-interval-in-seconds=5
#如果30秒内,依然没有收到续约请求,判定服务过期
eureka.instance.lease-expiration-duration-inseconds=30

启动类

@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApplication {
    public static void main(String[] args) {
        new SpringApplicationBuilder(EurekaClientApplication.class)
                                .web(WebApplicationType.SERVLET)
                                .run(args);
    }
}

Controller

@RestController
@Slf4j
public class Controller {

    @Value("${server.port}")
    private String port;

    @GetMapping("/sayHi")
    public String sayHi()
    {
        return "This is " + port;
    }

    @PostMapping("/sayHi")
    private Friend sayHiPost(@RequestBody Friend friend)
    {
        log.info("You are " + friend.getName());
        friend.setPort(port);
        return friend;
    }
}

eureka-consumer(调用方)
application.properties

spring.application.name=eureka-consumer
server.port=31000
eureka.client.serviceUrl.defaultZone=http://localhost:20000/eureka/

启动类

@SpringBootApplication
@EnableDiscoveryClient
public class EurekaConsumerApplication {

    @Bean
    public RestTemplate register()
    {
        return new RestTemplate();
    }

    public static void main(String[] args) {
        new SpringApplicationBuilder(EurekaConsumerApplication.class)
                                    .web(WebApplicationType.SERVLET)
                                    .run(args);
    }
}

Controller

@RestController
@Slf4j
public class Controller {

    @Autowired
    private LoadBalancerClient client;

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/hello")
    public String helloGet()
    {
        ServiceInstance instance = client.choose("eureka-client");
        if(instance == null)
        {
            return "No available instances";
        }
        String target = String.format("http://%s:%s/sayHi",instance.getHost(),instance.getPort());
        log.info("url is {}",target);
        return restTemplate.getForObject(target,String.class);
    }

    @PostMapping("/hello")
    public Friend helloPost()
    {
        ServiceInstance instance = client.choose("eureka-client");
        if(instance == null)
        {
            return null;
        }
        String target = String.format("http://%s:%s/sayHi",instance.getHost(),instance.getPort());
        log.info("url is {}",target);
        Friend friend = new Friend();
        friend.setName("Eureka Consumer");
        return restTemplate.postForObject(target,friend,Friend.class);
    }

}

注册中心高可用化改造

eureka-server-peer1
启动类

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

application.properties

spring.application.name=eureka-server-peer1
server.port=20001

eureka.instance.hostname=peer1
#注册中心注册
eureka.client.service-url.defaultZone=http://peer2:20000/eureka

eureka-server-peer2
启动类

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

application.properties

spring.application.name=eureka-server-peer2
server.port=20000
eureka.instance.hostname=peer2
eureka.client.service-url.defaultZone=http://peer1:20001/eureka

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值