1.导入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
配置文件中
eureka:
instance:
hostname: eureka7001.com
client:
fetch-registry: false
service-url:
#集群指向其它eureka
defaultZone: http://eureka7002.com:7002/eureka/
#单机就是7001自己
register-with-eureka: false
# defaultZone: http://eureka7001.com:7001/eureka/
# defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
package com.atguigu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7001.class,args);
}
}
2.再创建一个 mudel (相同部分省掉)
server:
port: 7002
eureka:
instance:
hostname: eureka7002.com
client:
fetch-registry: false
service-url:
#集群指向其它eureka
defaultZone: http://eureka7001.com:7001/eureka/
#单机就是7001自己
register-with-eureka: false
# defaultZone: http://eureka7001.com:7001/eureka/
# defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
实现注册互看 联通
在hosts文件中添加
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
127.0.0.1 eureka7003.com
3.消费者调用
首先导入配置文件
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2。进行注册
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
#集群版
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
instance:
instance-id: payment8001
prefer-ip-address: true
# 单机版
# defaultZone: http://localhost:7001/eureka
在启动类上添加 @EnableEurekaClient 注解
package com.atguigu.springcloud.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class ApplicationContextConfig
{
@Bean
// 集群配置
@LoadBalanced
public RestTemplate getRestTemplate()
{
return new RestTemplate();
}
}