springCloud高可配方法

springCloud高可配方法

这个高科配,需要结合官方的文档来处理就一点也不难,只是网上大多都是抄来抄去,都一个意思,没有解决到问题。

下面我们已注册中心eureka高科配来说明:

Eureka can be made even more resilient and available by running multiple instances and asking them to register with each other. In fact, this is the default behaviour, so all you need to do to make it work is add a valid serviceUrl to a peer, e.g.

上面的意思是说eureka默认是支持运行多实例的,我们只需要进行简单的配置

In this example we have a YAML file that can be used to run the same server on 2 hosts (peer1 and peer2), by running it in different Spring profiles. You could use this configuration to test the peer awareness on a single host (there’s not much value in doing that in production) by manipulating /etc/hosts to resolve the host names. In fact, the eureka.instance.hostname is not needed if you are running on a machine that knows its own hostname (it is looked up using java.net.InetAddress by default).

意思是上,想上面那样的例子,把两个euraka的两个配置写进同一个配置文件即可,然后你可以设置主机名在一台电脑上模拟之类的


例子如下,用三断线来标识每一个配置

---
spring:
  profiles: peer1
eureka:
  instance:
    hostname: peer1
  client:
    serviceUrl:
      defaultZone: http://peer2/eureka/

---
spring:
  profiles: peer2
eureka:
  instance:
    hostname: peer2
  client:
    serviceUrl:
      defaultZone: http://peer1/eureka/

这样,我们在运行eureka的时候,输入peer1或者pee2的时候,就会执行对应的注册服务


那么怎么输入呢?网上几乎都是一笔带过,没有实际的说明怎么输入,下面我贴出主程序代码供大家参考测试

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
 * 
* @ClassName: MySpringCloudEurekaServer 
* @Description: TODO(EurekaServer入口文件) 
* @author YangLiang(何懮)
* @date 2018年5月9日 下午1:29:48 
*
 */
@SpringBootApplication
@EnableEurekaServer
public class MySpringCloudEurekaServer {
	public static void main(String[] args) {
	    // 读取控制台输入,决定使用哪个profiles
      Scanner scan = new Scanner(System.in);
      String profiles = scan.nextLine();
      new SpringApplicationBuilder(MySpringCloudEurekaServer.class).profiles(profiles).run(args);
	}

}

如此,当我们执行这个jar包的时候会提示我们输入运行那个配置,即输入 peer1或peer2


如果您觉得本文章对您有用,也为了更好的运营博客,您可以点击这里捐助

评论 抢沙发

昵称 (必填) 邮箱 (必填)
表情