第一步:导入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<!--排除启动器中配置好的版本-->
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--加入我们需要的版本-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.9</version>
<!--如果组件出现日志冲突可以将这两个排除掉-->
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
第二部:编写yml配置文件
server:
port: 80
spring:
application:
name: cloud-consumer-order
cloud:
zookeeper:
# 表示将组件注册到的哪个地址
connect-string: 192.168.1.3:2181 # 如果是linux请记得关闭防火墙或者开放端口号
第三步:编写配置类,在启动类上开启注解@EnableDiscoveryClient
@EnableDiscoveryClient
@SpringBootApplication
public class ZkOrderMain80 {
public static void main(String[] args) {
SpringApplication.run(ZkOrderMain80.class,args);
}
}
@Configuration
public class ZkOrderConfiguration {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
第四步:编写一个controller测试类进行测试
@RestController
@Slf4j
@RequestMapping(value = "consumer/payment")
public class ZkOrderController {
@Resource
private RestTemplate restTemplate;
String url = "http://cloud-provider-payment";
@GetMapping(value = "/zk")
public String paymentInfo(){
String result = restTemplate.getForObject(url + "/payment/zk",String.class);
return result;
}
}
第五步:启动程序,到浏览器输入localhost/consumer/payment/zk,有结果说明成功
标签:配置,String,springcloud,zookeeper,class,org,public,cloud From: https://www.cnblogs.com/huoziqi/p/17532061.html