本文共 4629 字,大约阅读时间需要 15 分钟。
依赖:
cloud2020 pers.zhang.springcloud 1.0-SNAPSHOT 4.0.0 cloud-provider-payment8004 org.springframework.boot spring-boot-starter-web pers.zhang.springcloud cloud-api-commons ${project.version} org.springframework.cloud spring-cloud-starter-zookeeper-discovery org.apache.zookeeper zookeeper org.apache.zookeeper zookeeper 3.4.9 org.springframework.boot spring-boot-devtools runtime true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test
配置:application.yml
server: port: 8004#服务别名:注册zookeeper到注册中心名称spring: application: name: cloud-provider-payment cloud: zookeeper: connect-string: localhost:2181
启动类:
@EnableDiscoveryClient@SpringBootApplicationpublic class PaymentMain8004 { public static void main(String[] args) { SpringApplication.run(PaymentMain8004.class, args); }}
Controller:
@RestController@Slf4jpublic class PaymentController { @Value("${server.port}") private String serverPort; @RequestMapping(value = "/payment/zk") public String paymentzk(){ return "springcloud with zookeeper:" + serverPort + "\t" + UUID.randomUUID().toString(); }}
依赖:
cloud2020 pers.zhang.springcloud 1.0-SNAPSHOT 4.0.0 cloud-consumerzk-order80 org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-zookeeper-discovery org.apache.zookeeper zookeeper org.apache.zookeeper zookeeper 3.4.9 org.springframework.boot spring-boot-devtools runtime true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test
配置:application.yml
server: port: 80spring: application: name: cloud-sonsumer-order cloud: zookeeper: #注册到zookeeper connect-string: localhost:2181
配置类:
@Configurationpublic class ApplicationContextConfig { @Bean @LoadBalanced public RestTemplate getRestTemplate(){ return new RestTemplate(); }}
Controller:
@SpringBootApplication@EnableDiscoveryClientpublic class OrderZKMain80 { public static void main(String[] args) { SpringApplication.run(OrderZKMain80.class, args); }}
启动类:
@SpringBootApplication@EnableDiscoveryClientpublic class OrderZKMain80 { public static void main(String[] args) { SpringApplication.run(OrderZKMain80.class, args); }}
启动zookeeper,然后启动两个微服务:
连接zookeeper,可以看到服务已经注册:
在80端口调用8004的服务:
访问:http://localhost/consumer/payment/zk
转载地址:http://xspqb.baihongyu.com/