SpringCloud自定义Consul实现服务注册(解决多服务注册问题)

1.遇到的问题
多实例注册的问题,多实例的项目如果serviceid相同会造成覆盖之前的实例,但是如果使用随机数来注册实例项目重启或别的情况会造成无效实例过多。

spring.cloud.consul.discovery.instance-id=${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}

2.解决问题
我们可以重写ConsulServiceRegistry的register方法灵活的可以配置serviceid,重写后在启动类SpringBootApplicationRun加上相应代码。

#内网key 外网value
consul.registry.ip={"172.19.40.100":"106.14.106.100","10.24.169.100":"139.224.32.100"}
#是否开启外网 0不开启 1开启
consul.registry.status=1

MyConsulServiceRegistry.java

package com.consul;

import java.util.HashMap;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties;
import org.springframework.cloud.consul.discovery.HeartbeatProperties;
import org.springframework.cloud.consul.discovery.TtlScheduler;
import org.springframework.cloud.consul.serviceregistry.ConsulRegistration;
import org.springframework.cloud.consul.serviceregistry.ConsulServiceRegistry;

import com.ecwid.consul.v1.ConsulClient;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class MyConsulServiceRegistry extends ConsulServiceRegistry {

	@Value("${consul.registry.ip}")
	private String addressIp;
	
	@Value("${consul.registry.status}")
	private String registryStatus;
	
    public MyConsulServiceRegistry(ConsulClient client, ConsulDiscoveryProperties properties, TtlScheduler ttlScheduler, HeartbeatProperties heartbeatProperties) {
        super(client, properties, ttlScheduler, heartbeatProperties);
    }

    @Override
    public void register(ConsulRegistration reg) {
    	reg.getService().setId(reg.getService().getName() + "-" + reg.getService().getAddress() +"-"+ reg.getService().getPort());
    	if(registryStatus!=null&&!"".equals(registryStatus)&&registryStatus.equals("1")){//为开启外网ip注册
    		Gson gson = new Gson();
    		HashMap<String,String> ipMap=gson.fromJson(addressIp, new TypeToken<HashMap<String,String>>() {}.getType());
    		String ip=ipMap.get(reg.getService().getAddress());
    		if(ip!=null&&!"".equals(ip)){//查询不到ip就默认内网注册
    			reg.getService().setAddress(ip);
    		}
    	}
    	
        super.register(reg);
    }
}

SpringBootApplicationRun.java

@Configuration
@ConditionalOnConsulEnabled
@ConditionalOnProperty(value = "spring.cloud.service-registry.enabled", matchIfMissing = true)
@AutoConfigureBefore(ServiceRegistryAutoConfiguration.class)
public class SpringBootApplicationRun {

	@Bean
	@LoadBalanced
	RestTemplate restTemplate() {
		return new RestTemplate();
	}
	
	@Autowired(required = false)
    private TtlScheduler ttlScheduler;
	
	@Bean
	@Primary
	public ConsulServiceRegistry consulServiceRegistry(ConsulClient consulClient, ConsulDiscoveryProperties properties,
            HeartbeatProperties heartbeatProperties) {
		return new MyConsulServiceRegistry(consulClient, properties, ttlScheduler, heartbeatProperties);
	}
	
	public static void main(String[] args) {
        SpringApplication.run(SpringBootApplicationRun.class, args);
    }
}

至此可以灵活配置consul的serviceid,大家可以根据自己业务需求加上相应业务代码。

你可能感兴趣的:(SpringCloud自定义Consul实现服务注册(解决多服务注册问题))