<!-- eureka server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
eureka:
client:
#表示向注册中心注册自己 默认为true
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetch-registry: true
service-url:
# 入驻地址
defaultZone: http://localhost:7001/eureka
@EnableEurekaClient
步骤同上
互相注册,相互守望
############ spring cloud 2020#########
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
# 修改instance.hostname
# 修改client.service-url.defaultZone
# cloud-eureka-server7001
server:
port: 7001
eureka:
instance:
# 单机 hostname: localhost
# eureka服务端的实例名称,在host文件中查看
hostname: eureka7001.com
client:
# false表示不向注册中心注册自己
register-with-eureka: false
# false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要检索服务
fetch-registry: false
service-url:
# 设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
# 单机 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
# 相互注册
defaultZone: http://eureka7002.com:7002/eureka/
# cloud-eureka-server7002
server:
port: 7002
eureka:
instance:
# eureka服务端的实例名称
# 单机 hostname: localhost
hostname: eureka7002.com
client:
# false表示不向注册中心注册自己
register-with-eureka: false
# false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要检索服务
fetch-registry: false
service-url:
# 设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
# 单机 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
# 相互注册
defaultZone: http://eureka7001.com:7001/eureka/
# 只修改defaultZone
eureka:
client:
service-url:
# 集群版
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
import org.springframework.beans.factory.annotation.Value;
@RestController//@Controller+@ResponseBody
@Slf4j
public class PaymentController {
@Resource
private PaymentService paymentService;
//为了说明该次请求是从provider集群中的哪个端口提供的
@Value("${server.port}")
private String serverPort;
//新增
@PostMapping(value = "/payment/create")
public CommonResult create(@RequestBody Payment payment) {
int result = paymentService.create(payment);
log.info("*****插入结果: " + result);
if (result > 0) {
return new CommonResult(200, "插入数据库成功,端口号:" +serverPort, result);
}
return new CommonResult(444, "插入数据库失败", null);
}
//查询
@GetMapping(value = "/payment/get/{id}")
public CommonResult getPaymentById(@PathVariable("id") Long id) {
Payment payment = paymentService.getPaymentById(id);
log.info("*****查询结果: " + payment);
if (payment != null) {
return new CommonResult(200, "查询成功,端口号:" +serverPort, payment);
}
return new CommonResult(444, "没有对应记录,查询ID:" + id, null);
}
cloud-consumer-order80下ApplicationContextConfig.java
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced//添加注解
public RestTemplate getrRestTemplate() {
return new RestTemplate();
}
}
OrderController.java
@RestController
@Slf4j
public class OrderController {
/**
* 单机版写死,通过RestTemplate进行连接
* 指定cloud-consumer-order80通过8001端口连接cloud-provider-payment8001
*/
public static final String PAYMENT_URL = "http://localhost:8001";
@Resource
private RestTemplate restTemplate;
/**
* 通过在eureka上注册过的微服务(支付服务)名称调用
* 但是请求到CLOUD-PAYMENT-SERVICE不知道调用里面哪个微服务
* ——>
* 使用默认的负载均衡机制
*/
public static final String EPAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
@GetMapping("/consumer/payment/create")
public CommonResult<Payment> create(Payment payment) {
return restTemplate.postForObject(EPAYMENT_URL + "/payment/create", payment, CommonResult.class);
}
@GetMapping("/consumer/payment/get/{id}")
public CommonResult<Payment> getPayment(@PathVariable("id") Long id) {
return restTemplate.getForObject(EPAYMENT_URL + "/payment/get/" + id, CommonResult.class);
}
}
可以看到有2个提供者,1个消费者
多次访问 http://localhost/consumer/payment/get/4,可以看到会随机匹配端口号
http://localhost:8001/actuator/health
问题:暴露主机名称 localhost
问题:鼠标移到路径上浏览器左下角没有IP信息提示
# 修改application.yml
eureka:
client:
xxx
instance:
instance-id: payment8001/payment8002
prefer-ip-address: true
增加:
//服务发现 获取服务信息
@Resource
private DiscoveryClient discoveryClient;
/**
* 服务发现
*/
@GetMapping(value = "payment/discovery")
public Object discovery() {
//获得对外暴露的微服务名称
List<String> services = discoveryClient.getServices();
for (String element : services) {
log.info("*****element:" + element);
}
//获得指定微服务名称下的全部实例
List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
for (ServiceInstance instance : instances) {
log.debug(instance.getServiceId() + "\t" + instance.getHost() + "\t" + instance.getPort() + instance.getUri());
}
return this.discoveryClient;
}
添加注解:
@EnableDiscoveryClient
注册中心cloud-eureka-server7001增加配置
eureka:
server:
# 关闭自我保护机制,保证不可用服务被及时剔除(默认开启)
enable-self-preservation: false
# 设置不可用时间,默认90s,修改为2s
eviction-interval-timer-in-ms: 2000
生产者客户端cloud-provider-payment8001增加配置
eureka:
instance:
# eureka客户端8001向服务端7001发送心跳的时间间隔,单位为s,默认30s
lease-renewal-interval-in-seconds: 1
# eureka服务端7001在收到最后一次心跳后,等待的时间上限,默认90s,超时将剔除服务
lease-expiration-duration-in-seconds: 2
参考官方文档