Eureka 注册中心
2023-09-14 09:16:41 时间
IDea 中创建 4 个模块
1、创建 eureka 服务端模块 register-center
package com.eureka.registercenter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class RegisterCenterApplication {
public static void main(String[] args) {
SpringApplication.run(RegisterCenterApplication.class, args);
}
}
application.yml
spring:
application:
name: register-center
server:
port: 8761
eureka:
client:
register-with-eureka: false
fetch-registry: false
serviceUrl:
defaultZone: http://localhost:${server.port}/eureka/
# 测试时关闭自我保护机制,保证不可用服务及时踢出
server:
enable-self-preservation: false
2、创建 eureka 客户端模块 customer
CustomerApplication 代码
package com.eureka.customer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class CustomerApplication {
public static void main(String[] args) {
SpringApplication.run(CustomerApplication.class, args);
}
}
创建 CustomerController
package com.eureka.customer.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CustomerController {
@GetMapping("/get/{id}")
public String get(@PathVariable String id){
return "xieong"+id;
}
@GetMapping("/show")
public String show(){
return "顾客8762";
}
}
application.yml
spring:
application:
name: customer
server:
port: 8762
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
instance-id: customer-8762
prefer-ip-address: true
lease-renewal-interval-in-seconds: 1
lease-expiration-duration-in-seconds: 2
3、创建 eureka 客户端模块 order
package com.eureka.order;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableEurekaClient
public class OrderApplication {
@Bean
@LoadBalanced
RestTemplate restTemplate () {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}
创建 OrderController
package com.eureka.order.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class OrderController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/c/get/{id}")
public String get(@PathVariable String id) {
String result = restTemplate.getForObject("http://customer:8762/get/"+id, String.class);
return result;
}
@GetMapping("/show")
public String show(){
return "订单8763";
}
}
application.yml
spring:
application:
name: order
server:
port: 8763
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
instance-id: order-8763
prefer-ip-address: true
lease-renewal-interval-in-seconds: 1
lease-expiration-duration-in-seconds: 2
分别启动三个模块
访问 customer服务
order服务 调用 customer服务
4、创建网关 gateway 模块,并把网关加入 eureka
package com.eureka.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
application
server:
port: 9000
spring:
application:
name: gateway
cloud:
gateway:
routes:
# 路由ID(一个路由配置一个ID)
- id: order-service
# 通过注册中心来查找服务(lb代表从注册中心获取服务,并且负载均衡)
uri: lb://order/
# 匹配到的以/api/customer/开头的路径都转发到order的服务,相当于访问 lb://ORDER/**
predicates:
- Path=/api/customer/**
# 去掉匹配到的路径的前2级 这里也就是 /api/customer
filters:
- StripPrefix=2
discovery:
locator:
enabled: true
lowerCaseServiceId: true
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
instance-id: gateway-9000
prefer-ip-address: true
# 每间隔1s,向服务端发送一次心跳,证明自己依然”存活“
lease-renewal-interval-in-seconds: 1
# 告诉服务端,如果我2s之内没有给你发心跳,就代表我“死”了,将我踢出掉。
lease-expiration-duration-in-seconds: 2
再次查看 eureka 注册中心,此时 gateway 已注册成功
1、验证是否能通过 服务名 转发
2、验证 网关转发规则,通过网关9000端口加/api/customer/ 将转发至 order 服务
相关文章
- eureka手动注册服务_istio服务注册与发现
- Spring Cloud入门-Eureka服务注册与发现(Hoxton版本)
- 微服务架构与springcloud04——Eureka服务注册与发现
- 微服务的注册与发现:如何集成Eureka Server?
- 微服务组件--注册中心Spring Cloud Eureka分析
- SpringCloud之Eureka注册中心与Robbin负载均衡
- spring cloud eureka docker多网卡ip注册异常
- 图个源码系列 · Eureka Client启动注册,Server如何处理的全流程剖析
- EPA注册是什么意思,空气净化器装置做EPA认证申请说明
- ChatGPT能做什么?ChatGPT保姆级注册教程
- 手把手教你将Eureka升级Nacos注册中心
- 遗留非springboot传统项目接入eureka注册与服务发现
- 什么是服务注册和发现
- 亲测有效2023年ChatGPT注册(共享账号)超详细解决国外邮箱问题,国外手机验证问题!
- 中国版Office 365 应用程序注册详解程序员
- Spring Cloud(二):Spring Cloud Eureka Server高可用注册服务中心的配置详解编程语言
- JS注册事件和销毁事件
- NameSilo优惠码 新注册COM仅$7.99
- 使用 Docker 企业版搭建自己的私有注册服务器
- 器如何使用Oracle注册监听器(oracle注册监听)
- Oracle注册监听:让系统运行更稳定(oracle注册监听)
- 用Oracle注册监听:指南和实现(oracle注册监听)
- MySQL免费下载无需注册或帐号,轻松获取最新版数据库(mysql下载没有帐号)
- Oracle 12c 注册表注册企业转型的重要抓手(oracle12c注册表)
- 北京金融局启动在京注册P2P网贷机构自查工作