Eureka 注册中心
IDea 中创建 3个模块1、创建 eureka 服务端模块 centerpackage com.eureka.center;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springfr
·
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 服务
更多推荐
已为社区贡献4条内容
所有评论(0)