springcloud 实现注册端口和服务启动端口不同

需求

使用openscoring 做实时预测,想将其整合到springcloud中。openscoring默认使用jersey框架实现http。为了少改代码,只做了简单的修改:在ServerApplication.class启动后,再启动openscoring Server。

@Component
@ConfigurationProperties(ignoreUnknownFields = true, prefix = "com.pactera.rd.service.openservice")
public class OpenService implements ApplicationRunner{

	@Setter
	int  openPort = 8080;
	@Override
	public void run(ApplicationArguments args) throws Exception {
		System.out.println("MyApplicationRunner class will be execute when the project was started!");
		System.out.println(openPort);
		String [] config = new String[2];
		config[0] = "--port";
		config[1] = "" + openPort;
		Main.execute(Main.class, config);
	}

}

存在问题

使用springcloud是为了服务注册和发现,如果我设置server.port=8080openscoring Server 监听8081,那么eureka注册信息即为8080端口,而实际上最好注册openscoring Server的监听信息。

1. 设置eureka.instance.non-secure-port

eureka.instance.non-secure-port = ${com.pactera.rd.service.openservice.openport}

2. 设置 eureka.instance.instance-id

参考文档:https://blog.csdn.net/shog808/article/details/77161071
默认情况

  eureka.instance.instance-id=${spring.cloud.client.hostname}:${spring.application.name}:${spring.application.instance_id:${server.port}}

只需改变其配置即可,最终配置如下:

com.pactera.rd.service.openservice.openport = 50052

eureka.instance.non-secure-port = ${com.pactera.rd.service.openservice.openport}

eureka.instance.instance-id=${spring.cloud.client.hostname}:${spring.application.name}:${spring.application.instance_id:${com.pactera.rd.service.openservice.openport}}


你可能感兴趣的:(java)