参考地址:https://blog.csdn.net/u010882691/article/details/82256587

参考地址:https://blog.csdn.net/oyh1203/article/details/82189445

参考地址:https://blog.csdn.net/small_to_large/article/details/77836672 Spring Cloud Ribbon和Spring Cloud Feign

参考地址:https://blog.csdn.net/5iasp/article/details/79881691

 

事务等级:https://blog.csdn.net/gududedabai/article/details/82993700

目前,在Spring cloud 中服务之间通过restful方式调用有两种方式 
- restTemplate+Ribbon 
- feign

从实践上看,采用feign的方式更优雅(feign内部也使用了ribbon做负载均衡)。

zuul也有负载均衡的功能,它是针对外部请求做负载,那客户端ribbon的负载均衡又是怎么一回事?

客户端ribbon的负载均衡,解决的是服务发起方(在Eureka注册的服务)对被调用的服务的负载,比如我们查询商品服务要调用显示库存和商品明细服务,通过商品服务的接口将两个服务组合,可以减少外部应用的请求,比如手机App发起一次请求即可,可以节省网络带宽,也更省电。

ribbon是对服务之间调用做负载,是服务之间的负载均衡,zuul是可以对外部请求做负载均衡。

参考地址:https://blog.csdn.net/jrn1012/article/details/77837658/

因为LCN实现分布式事务的回滚,需要在服务内部 微服务之间的 负载均衡的 请求操作,故而需要在配置文件中加上ribbon的相关配置,它不与使用feign冲突!!!

 

lcn使用spring boot2.0 报错解决方案:https://www.jianshu.com/p/453741e0f28f

lcn集成到自己到自己的spring cloud项目中:https://blog.csdn.net/zhangxing52077/article/details/81587988

 

参考使用步骤1:

https://m.wang1314.com/doc/webapp/topic/20308073.html

 

 修改LCN ,集成spring boot2.0

注意:LCN 4.1.0版本 目前不支持spring boot 2.x的版本,所以需要进行更改!!

【【因为我已经更改完成,打包了jar了,jar可以在百度网盘下载,然后直接走这一步的上传第三方jar包到本地maven仓库,然后在项目中直接引用即可】】

第一步:

先在lcn官网【http://www.txlcn.org/】 找到GitHub 地址【https://github.com/codingapi/tx-lcn】,拷下所有的源码

【分布式事务】spring cloud集成lcn解决分布式事务

 

 

第二步:

解压下载的zip,放置在一个目录下,用IDEA打开【注意打开父层项目】

【分布式事务】spring cloud集成lcn解决分布式事务

【分布式事务】spring cloud集成lcn解决分布式事务

 

【分布式事务】spring cloud集成lcn解决分布式事务

 导入完整的jar包,然后下面就要开始更改源码中不支持spring boot 2.X的部分

 

第三步:

修改

transaction-springcloud 项目下com.codingapi.tx.springcloud.listener包中的ServerListener.java

【分布式事务】spring cloud集成lcn解决分布式事务

源码更改为:

package com.codingapi.tx.springcloud.listener;

import com.codingapi.tx.listener.service.InitService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**
 * @Component注解会自动扫描配置文件中的server.port值
 */
@Component
public class ServerListener implements ApplicationListener<ApplicationEvent> {

    private Logger logger = LoggerFactory.getLogger(ServerListener.class);

    private int serverPort;

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

    @Autowired
    private InitService initService;

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        //        logger.info("onApplicationEvent -> onApplicationEvent. "+event.getEmbeddedServletContainer());
        //        this.serverPort = event.getEmbeddedServletContainer().getPort();
        //TODO Spring boot 2.0.0没有EmbeddedServletContainerInitializedEvent 此处写死;modify by young
        this.serverPort = Integer.parseInt(port);

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                // 若连接不上txmanager start()方法将阻塞
                initService.start();
            }
        });
        thread.setName("TxInit-thread");
        thread.start();
    }

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

    public void setServerPort(int serverPort) {
        this.serverPort = serverPort;
    }
}
View Code

相关文章: