Eureka 注册中心

Eureka 注册中心IDea中创建3个模块1、创建eureka服务端模块centerpackagecom.eureka.center;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.netflix.eureka.server.EnableEu.

IDea 中创建 4 个模块 

Eureka 注册中心

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

分别启动三个模块

Eureka 注册中心

访问 customer服务

Eureka 注册中心

order服务 调用 customer服务

Eureka 注册中心

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 已注册成功

Eureka 注册中心

1、验证是否能通过 服务名 转发 

Eureka 注册中心

Eureka 注册中心

 2、验证 网关转发规则,通过网关9000端口加/api/customer/ 将转发至 order 服务

Eureka 注册中心

今天的文章Eureka 注册中心分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/26976.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注