Zookeeper 服务注册与发现02——服务消费者
生活随笔
收集整理的這篇文章主要介紹了
Zookeeper 服务注册与发现02——服务消费者
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>dym_cloud2021</artifactId><groupId>com.atguigu.springcloud</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>cloud-consumerzk-order80</artifactId><dependencies><!-- SpringBoot整合Web組件 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- SpringBoot整合zookeeper客戶端 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-zookeeper-discovery</artifactId><!--先排除自帶的zookeeper--><exclusions><exclusion><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId></exclusion></exclusions></dependency><!--添加zookeeper3.4.9版本--><dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>3.4.6</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies></project>application.yml
server:port: 80spring:application:name: cloud-consumer-ordercloud:#注冊到zookeeper地址zookeeper:connect-string: 81.70.58.163:2181OrderZKMain80.java
package com.dym.springcloud;import lombok.extern.slf4j.Slf4j; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication @EnableDiscoveryClient public class OrderZKMain80 {public static void main(String[] args) {SpringApplication.run(OrderZKMain80.class,args);} }ApplicationContextConfig.java
package com.dym.springcloud.config;import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate;@Configuration public class ApplicationContextConfig {@Bean@LoadBalancedpublic RestTemplate getRestTemplate(){return new RestTemplate();} }OrderZKController.java
package com.dym.springcloud.controller;import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate;import javax.annotation.Resource;@RestController @Slf4j public class OrderZKController {public static final String INVOKE_URL="http://cloud-provider-payment";@Resourceprivate RestTemplate restTemplate;@GetMapping(value = "/consumer/payment/zk")public String paymentInfo(){String result=restTemplate.getForObject(INVOKE_URL+"/payment/zk",String.class);return result;}}總結
以上是生活随笔為你收集整理的Zookeeper 服务注册与发现02——服务消费者的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Zookeeper 服务注册与发现01—
- 下一篇: Ribbon 负载均衡调用01——概述