springcloud——服务入驻到zookeeper注册中心

注:zookeeper相对于eureka不同的是:在不能接收到心跳包的时候,会立刻把已经注册的服务给删掉,而eureka会保留出故障的服务。

1、控制器

package spring.cloud.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * @author dc
 * @date 2020/7/25 - 16:43
 */
@RestController
@Slf4j
public class PaymentController {


    @Value("${server.port}")
    private String serverPort;

    @GetMapping("/payment/first")
    public String doFirst() {
        return "springcloud with zookeeper : " + serverPort + "\t" + UUID.randomUUID().toString();
    }
}

2、启动类

package spring.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

import javax.activation.MailcapCommandMap;

/**
 * @author dc
 * @date 2020/7/25 - 16:37
 */
@SpringBootApplication
@EnableDiscoveryClient      //该注解用于向使用consul或者zookeeper作为注册中心时注册服务
public class Provider8084Main {


    public static void main(String[] args) {
        SpringApplication.run(Provider8084Main.class, args);
    }


}

3、配置文件

#指定主机的端口号
server.port=8084

#指定服务的名称
spring.application.name=payment-provider8084

#连接本机的zookeeper注册中心
spring.cloud.zookeeper.connect-string=127.168.1.16:2181

猜你喜欢

转载自blog.csdn.net/weixin_43925059/article/details/107581647