org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
server:
port: 7001
eureka:
instance:
hostname: localhost #eureka服务端的实例名字
client:
register-with-eureka: false #表识不向注册中心注册自己
fetch-registry: false #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
@EnableEurekaServer
@SpringBootApplication
public class EurekaMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7001.class, args);
}
}
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
server:
port: 8001
spring:
application:
name: cloud-payment-service
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
@EnableEurekaClient
@SpringBootApplication
public class PaymentMain {
public static void main(String[] args) {
SpringApplication.run(PaymentMain.class, args);
}
}
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
server:
port: 8001
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
spring:
application:
name: cloud-payment-service
@Configuration
public class ApplicationContextConfig {
@LoadBalanced
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
@RestController()
@RequestMapping("order")
@Slf4j
public class OrderController {
public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
@GetMapping("/get/{id}")
public CommonResult getPayment(@PathVariable("id") Long id){
return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
}
eureka:
instance:
instance-id: payment${server.port}
eureka:
instance:
prefer-ip-address: true
eureka:
server:
enable-self-preservation: false
eviction-interval-timer-in-ms: 2000
eureka:
instance:
lease-renewal-interval-in-seconds: 10
lease-expiration-duration-in-seconds: 10
server:
port: 7002
eureka:
instance:
hostname: eureka7002.com
ip-address: 192.168.2.3 #强制指定IP地址,默认会获取本机的IP地址
prefer-ip-address: true
instance-id: ${eureka.instance.ip-address}:${server.port}
client:
fetch-registry: false #表示是否从Eureka Server获取注册的服务信息
register-with-eureka: false #falase表示不向注册中心注册自己
service-url:
defaultZone: http://192.168.2.17:7001/eureka/ #设置与eureka server交互的地址查询服务和注
server:
enable-self-preservation: false #关闭自我保护机制
eviction-interval-timer-in-ms: 2000 #移除失效服务时间
spring:
config:
location: classpath:/,file:./config/
cloud:
inetutils:
# 忽略指定网卡,支持正则表达式(这里使用正则表达式忽略所有虚拟机网卡)
ignored-interfaces: ['VMware.*']
use-only-site-local-interfaces: true