微服务SpringCloud开弓之Zookeeper服务注册与发现《四》

目录

      • SpringCloud整合Zookeeper替代Eureka
        • 1、注册中心Zookeeper
        • 2、服务提供者
        • 3、服务消费者

Eureka停止更新了

地址

SpringCloud整合Zookeeper替代Eureka

1、注册中心Zookeeper

  • 关闭Linux服务器防火墙后启动Zookeeper服务器

    //临时关闭
    service iptables stop
    //禁止开机启动
    chkconfig iptables off
    
  • Zookeeper服务器取代Eureka服务器,zk作为服务注册中心

2、服务提供者

1、新建cloud-provider-payment8004

2、POM


spring-cloud-starter-zookeeper-discovery

 parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>cloud-provider-payment8004artifactId>
    <description>Zookeeper服务提供者description>

    <dependencies>
        <dependency>
            <groupId>com.atguigu.springcloudgroupId>
            <artifactId>cloud-api-commonartifactId>
            <version>${project.version}version>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-zookeeper-discoveryartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-actuatorartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <scope>runtimescope>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>
project>

3、YML

server:
  # 8004表示注册到zookeeper服务器的支付服务提供者端口号
  port: 8004
spring:
  application:
    # 服务别名---注册zookeeper到注册中心的名称
    name: cloud-provider-payment
  cloud:
    zookeeper:
      # 默认localhost:2181
      connect-string: localhost:2181

4、主启动类

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

5、Controller

@RestController
@Slf4j
public class PaymentController {

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

    @RequestMapping(value = "payment/zk")
    public String paymentZk() {
        return "SpringCloud with zookeeper:" + serverPort + "\t" + UUID.randomUUID().toString();
    }
}

6、启动8004注册进zookeeper zkServer.sh start

  • 报错

微服务SpringCloud开弓之Zookeeper服务注册与发现《四》_第1张图片

  • 原因

解决zookeeper版本jar包冲突问题

  • 重新弄Pom文件
<dependencies>
        <dependency>
            <groupId>com.atguigu.springcloudgroupId>
            <artifactId>cloud-api-commonartifactId>
            <version>${project.version}version>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-zookeeper-discoveryartifactId>
            <exclusions>
                
                <exclusion>
                    <groupId>org.apache.zookeepergroupId>
                    <artifactId>zookeeperartifactId>
                exclusion>
            exclusions>
        dependency>
        
        <dependency>
            <groupId>org.apache.zookeepergroupId>
            <artifactId>zookeeperartifactId>
            <version>3.4.9version>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-actuatorartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <scope>runtimescope>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>
  • 验证

    http://localhost:8004/payment/zk

json解析工具地址

微服务SpringCloud开弓之Zookeeper服务注册与发现《四》_第2张图片

{
  "name": "cloud-provider-payment",
  "id": "6c79f953-21dd-4d45-9dc4-d4e05faaf1de",
  "address": "localhost",
  "port": 8004,
  "sslPort": null,
  "payload": {
    "@class": "org.springframework.cloud.zookeeper.discove.ZookeeperInstance",
    "id": "application-1",
    "name": "cloud-provider-payment",
    "metadata": {}
  },
  "registrationTimeUTC": 1586592028753,
  "serviceType": "DYNAMIC",
  "uriSpec": {
    "parts": [
      {
        "value": "scheme",
        "variable": true
      },
      {
        "value": "://",
        "variable": false
      },
      {
        "value": "address",
        "variable": true
      },
      {
        "value": ":",
        "variable": false
      },
      {
        "value": "port",
        "variable": true
      }
    ]
  }
}

服务节点是临时节点

3、服务消费者

  • 新建cloud-consumerzk-order80

  • POM

    <dependencies>
            <dependency>
                <groupId>com.atguigu.springcloudgroupId>
                <artifactId>cloud-api-commonartifactId>
                <version>${project.version}version>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
            
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-starter-zookeeper-discoveryartifactId>
                <exclusions>
                    
                    <exclusion>
                        <groupId>org.apache.zookeepergroupId>
                        <artifactId>zookeeperartifactId>
                    exclusion>
                exclusions>
            dependency>
            
            <dependency>
                <groupId>org.apache.zookeepergroupId>
                <artifactId>zookeeperartifactId>
                <version>3.4.10version>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-actuatorartifactId>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-devtoolsartifactId>
                <scope>runtimescope>
                <optional>trueoptional>
            dependency>
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
                <optional>trueoptional>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
                <scope>testscope>
            dependency>
        dependencies>
    
  • YML

    server:
      port: 80
    spring:
      application:
        # 服务别名
        name: cloud-consumer-order
      cloud:
        zookeeper:
          # 注册到zookeeper地址
          connect-string: 192.168.149.103:2181
    
  • 主启动

    @SpringBootApplication
    @EnableDiscoveryClient
    public class OrderZkMain80 {
        public static void main(String[] args) {
            SpringApplication.run(OrderZkMain80.class, args);
        }
    }
    
  • 业务类

    1、配置been

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

    2、controller

    @RestController
    @Slf4j
    public class OrderZkController {
        public static final String INVOKE_URL = "http://cloud-provider-payment";
        @Resource
        private RestTemplate restTemplate;
        /**
         * http://localhost/consumer/payment/zk
         *
         * @return
         */
        @GetMapping("/consumer/payment/zk")
        public String paymentInfo() {
            return restTemplate.getForObject(INVOKE_URL + "/payment/zk", String.class);
        }
    }
    
    
  • 验证测试
    在这里插入图片描述
    在这里插入图片描述
    欢迎转载和分享,记得点赞呀

你可能感兴趣的:(springCloud,java,java,zookeeper)