服务集群
新建相同的模块,更改端口,即可(略)
服务发现
以 8001为例:
DiscoveryClient就是服务发现。
8001的PaymentController的代码如下:
package com.banana.springcloud.controller;
import com.banana.springcloud.entity.CommonResult;
import com.banana.springcloud.entity.Payment;
import com.banana.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
/**
* @author layman
* @date 2020/12/30
* @Slf4j :开启日志注解
* @RestController注解 == @ResponseBody + @Controller
* @ResponseBody: 是用来把返回对象自动序列化成HttpResponse的。
* @Controller是: 一种特殊化的@Component类。习惯和@RequestMapping绑定使用,后者是用来指定路由映射。
* 1: 如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp页面,或者html,
* 配置的视图解析器 InternalResourceViewResolver不起作用,返回的内容就是return 里的内容。
* 可以用做ajax请求数据等
* 2: 如果需要返回到指定页面,则需要用 @Controller配合视图解析器InternalResourceViewResolver才行。
* 如果需要返回JSON,XML或自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解。
*/
@RestController
@Slf4j
@RequestMapping("/payment")
public class PaymentController {
/* RequestMapping:payment和/payment都能正常访问(规范要加)
加/:绝对路径(根路径),不加/:相对路径*/
@Resource
private PaymentService paymentService;
@Resource
private DiscoveryClient discoveryClient;
@Value("${server.port}")
private String serverPort;
/**
* @RequestBody: 接收前端传递给后端的json字符串中的数据(请求体)
* GET方式无请求体(请求参数附带在浏览器的URL里),只有POST请求才有请求体
* RequestBody与RequestParam()可同时使用:一个请求,只有一个RequestBody,但可以有多个RequestParam。
* RequestParam:参数可以是普通元素、数组、集合、对象等等(key-value 类型)
*/
@PostMapping("/create")
public CommonResult create(@RequestBody Payment payment){
int result = paymentService.create(payment);
log.info("****插入结果:"+result);
if(result > 0){
return new CommonResult(200,"插入数据成功,serverPort:"+serverPort,payment.getId());
}
return new CommonResult(400,"插入数据失败");
}
@GetMapping("/get/{id}")
public CommonResult getPaymentById(@PathVariable("id") Long id){
//@PathVariable:路径变量,是spring3.0的新功能,能够接收请求路径中占位符的值
Payment payment = paymentService.getPaymentById(id);
if(payment !=null ){
log.info("****查询结果:"+payment);
return new CommonResult(200,"查询数据成功,serverPort:"+serverPort,payment);
}
return new CommonResult(400,"没有对应的记录,查询id为:"+id);
}
@RequestMapping("/discovery")
public Object discovery(){
List<String> services = discoveryClient.getServices();
for (String service : services) {
log.info("注册的服务名称:"+service);
}
List<ServiceInstance> instances = discoveryClient.getInstances("cloud-payment-service"); for (ServiceInstance instance : instances) {
log.info(instance.getServiceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri());
}
return this.discoveryClient;
}
测试
同时,可以看到控制台打印以下信息:
其实,此时8002的端口,已经被停掉
但是,8002的服务名称依然出现在列表中,这里就要多谈一句, Eureka的保护机制。
Eureka的保护机制
自我保护的定义
如何禁止自我保护
修改7001的application.yml,新增以下内容:
server:
# 关闭Eureka的自我保护机制
enable-self-preservation: false
# 如果服务不可用,2000ms后剔除
eviction-interval-timer-in-ms: 2000
完整内容如下:
server:
port: 7001
eureka:
instance:
#eureka服务端的实例名称
hostname: localhost
#hostname: eureka7001.com
client:
#false表示不向注册中心注册自己
register-with-eureka: false
#false表示自己端就是注册中心,职责是维护实例,并不需要检索服务
fetch-registry: false
service-url:
#设置 服务的注册地址
defaultZone: http://${
eureka.instance.hostname}:${
server.port}/eureka/
#defaultZone: http://eureka7001.com:7001/eureka #集群版 #集群指向其他eureka
server:
# 关闭Eureka的自我保护机制
enable-self-preservation: false
# 如果服务不可用,2000ms后剔除
eviction-interval-timer-in-ms: 2000
写在最后
因为Eureka已经停止更新,已经不再推荐使用。
但是,它的设计理念,依然值得我们学习。