SpringCloudAlibaba学习实战

时间:2021-7-3 作者:qvyue

一、高可用nacos搭建

搭建方案:使用haproxy做为nacos节点的负载均衡器,部署三个nacos节点,分别是192.168.200.133、192.168.200.134、192.168.200.135,负载均衡端口是16443,各个nacos节点的端口是8850

高可用server端 nacos配置

下载压缩包

SpringCloudAlibaba学习实战

分别上传到192.168.200.133、192.168.200.134、192.168.200.135,并解压

SpringCloudAlibaba学习实战

各个节点配置host

vim /etc/hosts

这里是133的节点的host

SpringCloudAlibaba学习实战

各个节点都进入nacos的config目录配置文件,修改cluster.conf

SpringCloudAlibaba学习实战

然后在修改application..properties

SpringCloudAlibaba学习实战

启动mysql,这里我是用docker启动的,然后在mysql下创建nacos_config表

SpringCloudAlibaba学习实战
SpringCloudAlibaba学习实战

启动各个节点的nacos

sh /home/software/nacos-8850/bin/startup.sh

haproxy配置

 cat /etc/haproxy/haproxy.cfg

global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #
    #    local2.*                       /var/log/haproxy.log
    #
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  app
    bind                  *:16443
    mode                   tcp
    option                 tcplog
    default_backend        app

#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
    balance     roundrobin
    server master133  192.168.200.133:8850 check
    server master134  192.168.200.134:8850 check
    server master135  192.168.200.135:8850 check

启动haproxy并开机自起

systemctl enable haproxy &&  systemctl start haproxy

访问http://192.168.200.133:16443/nacos/index.html,nacos的高可用完成

SpringCloudAlibaba学习实战

高可用client端 nacos配置

在同一个应用名称下部署两个节点,这里由于条件有限,我只能使用同一个节点地址下的不同端口代表不同的节点

server:
  port: 8080
spring:
  profiles: clientA01
  application:
    name: clientA
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    name: defaultDataSource
    url: jdbc:mysql://localhost:3306/employees?serverTimezone=UTC
    username: root
    password: 123456
  cloud:
    nacos:
      discovery:
        username: nacos
        password: nacos
        server-addr: 192.168.200.133:8850,192.168.200.134:8850,192.168.200.135:8850
        namespace: public
---
server:
  port: 8081
spring:
  profiles: clientA02
  application:
    name: clientA
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    name: defaultDataSource
    url: jdbc:mysql://localhost:3306/employees?serverTimezone=UTC
    username: root
    password: 123456
  cloud:
    nacos:
      discovery:
        username: nacos
        password: nacos
        server-addr: 192.168.200.133:8850,192.168.200.134:8850,192.168.200.135:8850
        namespace: public

根据不同的profile条件,选择不同的环境

SpringCloudAlibaba学习实战

分别启动clientA01和clientA02

SpringCloudAlibaba学习实战

看看nacos的前端界面,部署成功

SpringCloudAlibaba学习实战

二、集群的负载均衡器 Ribbon

springcloud已经与Ribbon进行了整合,我们只需要在RestTemplate上进行配置,Ribbon能够将注册列表的实例进行负载均衡

@SpringBootApplication
public class NacostestApplication {

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

    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

获取服务端口号的工具类

@Configuration
public class ServiceInfoUtil implements ApplicationListener {
    private int serverPort;

    @Override
    public void onApplicationEvent(WebServerInitializedEvent event) {
        this.serverPort = event.getWebServer().getPort();
    }

    public int getPort() {
        return this.serverPort;
    }
}

Controller层

@RestController
@Slf4j
public class TestController {
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    //获取被调用服务的端口的工具类
    @Autowired
    private ServiceInfoUtil serviceInfoUtil;

    //提供给服务消费端的接口
    @GetMapping("/provide")
    public void test(){
        List services = discoveryClient.getServices();
        log.info("服务消费:【{}】,端口号:【{}】",services,serviceInfoUtil.getPort());
    }

    //服务消费端的消费接口
    @GetMapping("/consummer")
    public String consummer(){
        return restTemplate.getForEntity("http://clientA/provide",String.class).getBody();
    }
}

我们启动一个消费者的服务

server:
  port: 8082
spring:
  profiles: consummerA
  application:
    name: consummerA
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    name: defaultDataSource
    url: jdbc:mysql://localhost:3306/employees?serverTimezone=UTC
    username: root
    password: 123456
  cloud:
    nacos:
      discovery:
        username: nacos
        password: nacos
        server-addr: 192.168.200.133:8850,192.168.200.134:8850,192.168.200.135:8850
        namespace: public
logging:
  config: classpath:log4j2.xml

SpringCloudAlibaba学习实战

启动消费者服务器,然后在重启两个clientA

SpringCloudAlibaba学习实战

消费端访问http://localhost:8082/consummer,服务打到了端口为8080的clientA上

SpringCloudAlibaba学习实战

消费端再次访问http://localhost:8082/consummer,服务打到了端口为8081的clientA上

SpringCloudAlibaba学习实战

证明负载均衡是生效的

三、熔断限流 sentinel

下载安装sentinel

SpringCloudAlibaba学习实战
java -jar sentinel-dashboard-1.8.0.jar

引入sentinel依赖

com.alibaba.cloudspring-cloud-starter-alibaba-sentinel

访问http://192.168.200.133:8080/,由于sentinel是懒加载,所以刚刚启动什么都看不到,只有请求了接口才能看到内容

SpringCloudAlibaba学习实战

四、调用链监控 sleuth和zipking

curl -sSL https://zipkin.io/quickstart.sh | bash -s
nohup java -jar zipkin.jar  > zipking.log  2>&1 &

访问:http://192.168.200.133:9411/
引入依赖,由于zipkin已经包含sleuth,所以可以将sleuth的依赖移除

        org.springframework.cloudspring-cloud-starter-zipkin

配置

server:
  port: 8080
spring:
  profiles: clientA01
  application:
    name: clientA
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    name: defaultDataSource
    url: jdbc:mysql://localhost:3306/employees?serverTimezone=UTC
    username: root
    password: 123456
  cloud:
    sentinel:
      transport:
        dashboard: 192.168.200.133:8080
    nacos:
      discovery:
        username: nacos
        password: nacos
        server-addr: 192.168.200.133:8850,192.168.200.134:8850,192.168.200.135:8850
        namespace: public
  zipkin:
    base-url: http://192.168.200.133:9411
  sleuth:
    sampler:
      probability: 1.0
logging:
  config: classpath:log4j2.xml
SpringCloudAlibaba学习实战

五、服务网关 gateway

可以做到统一用户认证,达到了不需要在每个模块都做一次登录授权的功能,另外统一管理服务接口对外暴露一个统一的域名,有利于系统的后期的重构或迁移,内部服务如何变化对外部无感知,减少运维的成本

server:
  port: 8083
spring:
  application:
    name: gateway
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    name: defaultDataSource
    url: jdbc:mysql://localhost:3306/employees?serverTimezone=UTC
    username: root
    password: 123456
  cloud:
    sentinel:
      transport:
        dashboard: 192.168.200.133:8080
    nacos:
      discovery:
        username: nacos
        password: nacos
        server-addr: 192.168.200.133:8850,192.168.200.134:8850,192.168.200.135:8850
        namespace: public
    gateway:
      discovery:
        locator:
          enabled: true
      routes:
        - id: clientA
          uri: lb://clientA
          predicates:
            - Path=/clientA/**

        - id: consummerA
          uri: lb://consummerA
          predicates:
            - Path=/consummerA/**

访问:http://localhost:8083/consummerA/consummer 即可转发到consummerA应用下的/consummer接口

六、配置管理 nacos-config

SpringCloudAlibaba学习实战
spring:
  application:
    name: consummerA
  profiles: consummerA
  cloud:
    nacos:
      config:
        server-addr: 192.168.200.133:8850,192.168.200.134:8850,192.168.200.135:8850
        file-extension: yaml

到nacos上添加配置

SpringCloudAlibaba学习实战
@RestController
@Slf4j
public class TestController {
    //获取被调用服务的端口的工具类
    @Autowired
    private ServiceInfoUtil serviceInfoUtil;

    @Value("${test.config}")
    private String config;

    @GetMapping("/getconfig")
    public String getconfig(){
        return config;
    }
}

访问:http://localhost:8083/consummerA/getconfig

SpringCloudAlibaba学习实战

另外在配置中心里content-center.yaml是所有应用通用的配置,data id为content-center.yaml时,无论应用处于何种环境或者是服务名称都能拿到配置

声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:qvyue@qq.com 进行举报,并提供相关证据,工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。