springcloud--eureka

版权声明:本文为博主原创文章,未经博主允许不得转载。否则切鸡鸡~~ https://blog.csdn.net/kang5789/article/details/83022240

如果习惯了eclipse,又不习惯idea,推荐使用sts写springboot项目,很方便

源码下载

我这里eureka本地部署2个模拟集群,得需要修改hosts如下

127.0.0.1    www.eureka1.com
127.0.0.1    www.eureka2.com

pom依赖:

配置文件指向对方的url,多个用逗号分开

eureka-server1

spring.application.name=eureka-server
server.port=1001

eureka.instance.hostname=eureka1
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://www.eureka2.com:1002/eureka

eureka-server2

spring.application.name=eureka-server
server.port=1002

eureka.instance.hostname=eureka2
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://www.eureka1.com:1001/eureka

启动类加@EnableEurekaServer注解

扫描二维码关注公众号,回复: 4958541 查看本文章

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

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(EurekaServerApplication.class, args);
	}
}

启动项目访问localhost:1001  、1002 可以看到已经实现高可用

注册中心已经有了,开始写一个服务提供者

启动类加注解@EnableEurekaClient

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EnableEurekaClient
@SpringBootApplication
public class EurekaClient1Application {

	public static void main(String[] args) {
		SpringApplication.run(EurekaClient1Application.class, args);
	}
}

配置文件,写的是上边两个eureka地址,如果服务要做高可用,复制一份项目改下端口即可

spring.application.name=eureka-client
server.port=2001

eureka.client.serviceUrl.defaultZone=http://www.eureka1.com:1001/eureka,http://www.eureka2.com:1002/eureka

启动,发现注册了两个节点,服务提供者高可用成功

猜你喜欢

转载自blog.csdn.net/kang5789/article/details/83022240