修改jar源码打包到mvn仓库

原因是 用了nacos分离开发的时候,保证网关层能自动根据ip地址,根据spring.appllication.name的应用名称注册到nacos的时候,同时带一个ip
如:spring.appllication.name为gzstrong-gateway,其注册到网关的时候服务可以是这种形式gzstrong-gateway-192.168.197.223,配置文件也是根据这一规则去获取配置

1.nacos discovery客户端

  • 修改spring-cloud-alibaba-nacos-discovery-0.2.2.RELEASE包的源码NacosDiscoveryProperties.java

NacosDiscoveryProperties.java在org.springframework.cloud.alibaba.nacos包下

@PostConstruct
    public void init() throws SocketException {

        metadata.put(PreservedMetadataKeys.REGISTER_SOURCE, "SPRING_CLOUD");
        if (secure) {
            metadata.put("secure", "true");
        }

        serverAddr = Objects.toString(serverAddr, "");
        if (serverAddr.lastIndexOf("/") != -1) {
            serverAddr = serverAddr.substring(0, serverAddr.length() - 1);
        }
        endpoint = Objects.toString(endpoint, "");
        namespace = Objects.toString(namespace, "");
        logName = Objects.toString(logName, "");

        if (StringUtils.isEmpty(ip)) {
            // traversing network interfaces if didn't specify a interface
            if (StringUtils.isEmpty(networkInterface)) {
                ip = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
            }
            else {
                NetworkInterface netInterface = NetworkInterface
                        .getByName(networkInterface);
                if (null == netInterface) {
                    throw new IllegalArgumentException(
                            "no such interface " + networkInterface);
                }

                Enumeration inetAddress = netInterface.getInetAddresses();
                while (inetAddress.hasMoreElements()) {
                    InetAddress currentAddress = inetAddress.nextElement();
                    if (currentAddress instanceof Inet4Address
                            && !currentAddress.isLoopbackAddress()) {
                        ip = currentAddress.getHostAddress();
                        break;
                    }
                }

                if (StringUtils.isEmpty(ip)) {
                    throw new RuntimeException("cannot find available ip from"
                            + " network interface " + networkInterface);
                }

            }
        }
        //修改源码 用于配置中心文件根据 "spring.application.name+IP地址" 的规则
        if("true".equals(environment.getProperty("nacos.discovery.ip.suffix"))){
            this.service=this.service+"-"+this.ip;
        }
        this.overrideFromEnv(environment);
    }
  • bootstrap.properties启动文件要添加配置 nacos.discovery.ip.suffix=false
#支持后缀添加ip地址
nacos.discovery.ip.suffix=false
#支持后缀添加ip地址
nacos.config.ip.suffix=true

2.Nacos 客户端的nacos-config

NacosPropertySourceLocator.java在org.springframework.cloud.alibaba.nacos.client包下

  • 找到NacosPropertySourceLocator.java,修改源码
 @Override
    public PropertySource locate(Environment env) {

        ConfigService configService = nacosConfigProperties.configServiceInstance();

        if (null == configService) {
            log.warn("no instance of config service found, can't load config from nacos");
            return null;
        }
        long timeout = nacosConfigProperties.getTimeout();
        nacosPropertySourceBuilder = new NacosPropertySourceBuilder(configService,
                timeout);
        String name = nacosConfigProperties.getName();

        String dataIdPrefix = nacosConfigProperties.getPrefix();
        if (StringUtils.isEmpty(dataIdPrefix)) {
            dataIdPrefix = name;
        }

        if (StringUtils.isEmpty(dataIdPrefix)) {
            try {
                //修改源码 用于配置中心文件根据 "spring.application.name+IP地址" 的规则
                String hostAddress = InetAddress.getLocalHost().getHostAddress();
                dataIdPrefix = env.getProperty("spring.application.name");
                if("true".equals(env.getProperty("nacos.config.ip.suffix"))){
                    dataIdPrefix = env.getProperty("spring.application.name") + "-" + hostAddress;
                    nacosConfigProperties.setPrefix(dataIdPrefix);
                }
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }

        }

        CompositePropertySource composite = new CompositePropertySource(
                NACOS_PROPERTY_SOURCE_NAME);

        loadSharedConfiguration(composite);
        loadExtConfiguration(composite);
        loadApplicationConfiguration(composite, dataIdPrefix, nacosConfigProperties, env);

        return composite;
    }

  • bootstrap.properties启动文件要添加配置 nacos.config.ip.suffix=true
#支持后缀添加ip地址
nacos.discovery.ip.suffix=false
#支持后缀添加ip地址
nacos.config.ip.suffix=true

3.将这两个文件编译成class文件

QQ截图20190521104201.png

4.解压jar包,替换class

在gradle的仓库路径spring-cloud-alibaba-nacos-discovery的路径,找的spring-cloud-alibaba-nacos-discovery-0.2.2.RELEASE.jar文件,将其用好压工具解压,在org.springframework.cloud.alibaba.nacos包下删除NacosDiscoveryProperties.class,将上面编译好的class文件,放进来

5.将文件夹,整理成jar包

进入到spring-cloud-alibaba-nacos-discovery的整理目录中

jar cvf spring-cloud-alibaba-nacos-discovery-0.2.2.RELEASE.M.jar .

注意后面有当前路径. 且其中.M.jar 意思是自己定义 M代表是在原来的jar包进行修改过

同理生成spring-cloud-alibaba-nacos-config-0.2.2.RELEASE.M.jar

6.将jar包上传到nexus私服

将jar包上传到nexus私服

你可能感兴趣的:(修改jar源码打包到mvn仓库)