SpringCloud——Eureka 服务注册与发现

目录

1. 单机 Eureka 构建

1.1 创建 EurekaServer 服务注册中心

1.2 Eureka 注册服务提供者

2. Eureka 集群

2.1 创建两个 EurekaServer

2.2 修改映射文件

2.3 写 YML

2.4 服务提供者发布到集群

2.5 测试

3. 服务提供者集群

4. 服务消费者获取提供者服务

4.1 修改服务地址

4.2 RestTemplate 添加负载均衡

5. 完善 actuator 微服务信息

5.1 修改服务名称

5.2 添加微服务 info

6. 服务发现 Discovery

6.1 修改 controller

6.2 修改启动类

6.3 测试结果

7. Erueka 自我保护

7.1 自我保护机制

7.2 禁止自我保护

 


 

1. 单机 Eureka 构建

1.1 创建 EurekaServer 服务注册中心

a. 建 moudle

b. 改 pom

<dependencies>
        <!--eureka server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <!--自定义通用包-->
        <dependency>
            <groupId>com.zth.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!--boot web actuator-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!--一般通用配置-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

c. 写 YML

server:
  port: 7001
eureka:
  instance:
    hostname: localhost   # eureka 服务端的示例名称
  client:
    # false :不向注册中心注册自己
    register-with-eureka: false
    # false:表示自己就是注册中心,职责是维护服务实例,不需要去检索服务
    fetch-registry: false
    # 设置 eureka 服务地址
    service-url:
      defaultZoon: http://${eureka.instance.hostname}:${server.port}/eureka/

d. 主启动

@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7001 {
    public static void main(String[] args){
        SpringApplication.run(EurekaMain7001.class,args);
    }
}

e. 测试

 

1.2 Eureka 注册服务提供者

a. 改 pom

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

b. 写 YML

eureka:
  client:
    register-with-eureka: true
    fetchRegistry: true
    service-url:
      defaultZone: http://localhost:7001/eureka

c. 主启动

添加  @EnableEurekaClient

@EnableEurekaClient
@SpringBootApplication
public class PaymentMain8001 {
    public static void main(String[] args){
        SpringApplication.run(PaymentMain8001.class,args);
    }
}

d. 测试

先启动 EurekaServer

【注】服务提供者和服务消费者入驻 Eureka 一致。

2. Eureka 集群

2.1 创建两个 EurekaServer

cloud-eureka-server7001

cloud-eureka-server7002

2.2 修改映射文件

127.0.0.1  eureka7001.com
127.0.0.1  eureka7002.com

2.3 写 YML

cloud-eureka-server7001:

server:
  port: 7001

eureka:
  instance:
    hostname:  eureka7001.com   # eureka 服务端的示例名称
  client:
    # false :不向注册中心注册自己
    register-with-eureka: false
    # false:表示自己就是注册中心,职责是维护服务实例,不需要去检索服务
    fetch-registry: false
    # 设置 eureka 服务地址
    service-url:
      defaultZone: http://eureka7002.com:7002/eureka/

cloud-eureka-server7002:

server:
  port: 7002

eureka:
  instance:
    hostname:  eureka7002.com   # eureka 服务端的示例名称
  client:
    # false :不向注册中心注册自己
    register-with-eureka: false
    # false:表示自己就是注册中心,职责是维护服务实例,不需要去检索服务
    fetch-registry: false
    # 设置 eureka 服务地址
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/

2.4 服务提供者发布到集群

服务提供者 cloud-payment-server8001 YML:

eureka:
  client:
    register-with-eureka: true
    fetchRegistry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka

2.5 测试

 

3. 服务提供者集群

两个服务提供者:

cloud-provider-payment8001

cloud-provider-payment8002

YML 配置:

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/mycloud?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: mysql

eureka:
  client:
    register-with-eureka: true
    fetchRegistry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka

两个服务提供者的 application.name 一致,则认为是同一个服务提供者。

即:将提供相同服务的不同的服务提供者 application.name 保持一致,并注册到 Erueka,就完成了服务提供者集群。

 

4. 服务消费者获取提供者服务

4.1 修改服务地址

@RestController
@Slf4j
public class OrderController {
    /**
     * public static final String PAYMENT_URL = "http://localhost:8001";
     */
    
    public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/consumer/payment/create")
    public CommonResult<Payment> create(Payment payment){
        return restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment,CommonResult.class);
    }

    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult<Payment> getPayment(@PathVariable("id") long id){
        return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
    }

}

 

4.2 RestTemplate 添加负载均衡

@Configuration
public class ApplicationContextConfig {
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

5. 完善 actuator 微服务信息

5.1 修改服务名称

eureka:
  client:
    register-with-eureka: true
    fetchRegistry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
  instance:
    instance-id: payment8001
    prefer-ip-address: true

instance-id: payment8001:修改服务名称
prefer-ip-address: true:访问信息是否提示 ip

5.2 添加微服务 info

a. 添加 pom

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

b. 配置 YML

info:
  groupId: "@groupId@"
  artifactId: "@artifactId@"
  version: "@version@"
  principal: 张三
  phone: 110

c. 查看 info 

 

6. 服务发现 Discovery

可以通过服务发现获得注册进 eureka 里面微服务的信息。

6.1 修改 controller

@Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping(value = "/payment/discovery")
    public Object discovery(){
        List<String> services = discoveryClient.getServices();
        for (String element : services) {
            log.info("element:"+element);
        }

        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;
    }

【注】import org.springframework.cloud.client.discovery.DiscoveryClient;

6.2 修改启动类

启动类添加 @EnableDiscoveryClient

6.3 测试结果

 

7. Erueka 自我保护

7.1 自我保护机制

为了防止 EurekaClient 可以正常运行但与 EurekaServer 网络不通情况下,EurekaServer 立刻剔除 EurekaClient。

默认情况下,如果 EurekaServer 在一 定时间内没有接收到某个微服务实例的心跳,EurekaServer 将会注销该实例 (默认90秒)
 

7.2 禁止自我保护

EurekaServer 端 YML:

eureka.server.enable-self-preservation=false  关闭自我保护

eureka:
  instance:
    hostname:  eureka7001.com   # eureka 服务端的示例名称
  client:
    # false :不向注册中心注册自己
    register-with-eureka: false
    # false:表示自己就是注册中心,职责是维护服务实例,不需要去检索服务
    fetch-registry: false
    # 设置 eureka 服务地址
    service-url:
      defaultZone: http://eureka7002.com:7002/eureka/
  server:
    enable-self-preservation: false

EurekaClient 端:

eureka:
  client:
    register-with-eureka: true
    fetchRegistry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
  instance:
    instance-id: payment8001
    prefer-ip-address: true
    # Eureka 客户端向服务端发送心跳的时间间隔,单位为秒(默认是30秒)
    lease-expiration-duration-in-seconds: 1
    # Eureka 服务端在收到最后一次心跳后等待时间上限, 单位为秒(默认是90秒),超时将剔除服务
    lease-renewal-interval-in-seconds: 3

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值