Eureka集群核心观点
- 相互注册,相关守望
1.简单理解就是多个注册中心(服务端)相互注册,客户端都注册到服务端。下面通过两个注册中心,生产者服务解释。因为只有一台机器,所以通过修改host文件将127.0.0.1配置出了多个域名。
eureka注册中心集群设置
1.eureka7001yml配置文件
eureka7001的服务端往eureka7002注册了自己。其他和之前的单机版本代码一致
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com #eureka服务端的实例名称
client:
fetch-registry: false # 表明自己就是注册中心
register-with-eureka: false # 是否向注册中心注册自己
service-url:
#eureka注册中心集群:相互注册,相关守望。7001服务往7002服务注册
defaultZone: http://eureka7002.com:7002/eureka/
2.eureka7002yml配置文件
server:
port: 7002
eureka:
instance:
hostname: eureka7002.com #eureka服务端的实例名称
client:
fetch-registry: false # 表明自己就是注册中心
register-with-eureka: false # 是否向注册中心注册自己
service-url:
#eureka注册中心集群:相互注册,相关守望。7002服务往7001服务注册
defaultZone: http://eureka7001.com:7001/eureka/
验证
达到下面两点证明成功
1.访问http://eureka7001.com:7001成功,并且DS Replicas出现了eureka7002.com
2.访问http://eureka7002.com:7002成功,并且DS Replicas出现了eureka7001.com
eureka客户端搭建集群(可以是任意业务)
1.假设我们有一个消费者服务端口号为80,两个生产者服务端口分别为8001和8002(集群)。
2.消费者和生产者都是eureka客户端,他们的maven都是需要eureka客户端依赖,其他的代码依赖按需添加
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
消费者
消费者服务只有一个并且不和数据库打交道,消费者只需要通过spring自己的RestTemplate模板类通过http请求调用生产者的接口就可以了。这也是springcloud或者微服务生态的理念。
yml文件
1.fetchRegistry配置为了达到负载均衡必须是true
2.service-url必须配置两个注册中心
server:
port: 80
spring:
application:
name: cloud-consumer-order
eureka:
client:
register-with-eureka: true #向注册中心注册自己服务
service-url: #eureka注册中心地址
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群配置,往多个注册中心都注册
fetchRegistry: true #是否从Eureka注册中心抓取已有的服务,默认是true,
#但是集群必须是true才可以配合ribbon做负载均衡
RestTemplate配置
1.@LoadBalanced是必须加的,这是默认的轮询算法实现的负载均衡
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
controller
1.需要注意的是我们消费者调用htpp请求是通过生产者的服务名调用的CLOUD-PAYMENT-SERVICE
@RestController
public class OrderController {
@Resource
private RestTemplate restTemplate;
//通过服务名去调用生产者服务
public static final String PAYMENT_URL="http://CLOUD-PAYMENT-SERVICE";
@GetMapping("/consumer/payment/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id")Long id){
return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id, CommonResult.class);
}
@GetMapping("/consumer/payment/add")
public CommonResult add(Payment payment){
return restTemplate.postForObject(PAYMENT_URL+"/payment/add",payment,CommonResult.class);
}
}
生产者
生产者yml文件
1.生产者是具体和db层打交道的,并且这边就不放业务代码了,只放yml配置文件和controller。
server:
port: 8001
spring:
application:
name: cloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包
url: jdbc:mysql://localhost:3306/db01?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: woyu1996
mybatis:
mapperLocations: classpath:mapper/*.xml
type-aliases-package: com.yu.springcloud.entities # 所有Entity别名类所在包
eureka:
client:
fetchRegistry: true #是否从Eureka注册中心抓取已有的服务,默认是true,
#但是集群必须是true才可以配合ribbon做负载均衡
register-with-eureka: true #向eureka注册中心注册自己
service-url: #eureka注册中心地址
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群配置,往多个注册中心都注册
instance:
instance-id: payment8001 #设置Eureka注册中心web页面中Status中名称
prefer-ip-address: true # 是否显示ip地址
@RestController
@Slf4j
public class PaymentController {
@Autowired
private PaymentService paymentService;
@Value("${server.port}")
private String port;
@PostMapping("/payment/add")
public CommonResult addPayment(@RequestBody Payment payment){
int result = paymentService.addPayment(payment);
if(result>0){
return new CommonResult(200,"添加成功,端口:"+port,result);
}else {
return new CommonResult(444,"添加失败,端口:"+port,result);
}
}
@GetMapping("/payment/get/{id}")
public CommonResult getPaymentById(@PathVariable("id") Long id){
Payment payment = paymentService.getPaymentById(id);
if(payment!=null){
log.info("查询成功"+payment+"哈哈哈哈哈哈");
return new CommonResult(200,"添加成功,端口:"+port,payment);
}else {
log.info("查询失败:"+id);
return new CommonResult(444,"未找到该记录:"+id+",端口:"+port,payment);
}
}
}
ps:具体的代码可以访问:git代码地址