springcloud-Consul服务注册与发现(三)
2021-05-07 13:31
标签:ccf 系统 rand text div test ret img host springcloud-Consul服务注册与发现(三) 标签:ccf 系统 rand text div test ret img host 原文地址:https://www.cnblogs.com/binwenhome/p/13183665.htmlConsul简介
服务提供者注册进Consul
dependency>
groupId>org.springframework.cloudgroupId>
artifactId>spring-cloud-starter-consul-discoveryartifactId>
dependency>
#consul服务端口号
server:
port: 8006
spring:
application:
name: consul-provider-payment
cloud:
consul: #consul注册中心
host: localhost
port: 8500
discovery:
service-name: ${spring.application.name}
#hostname: 127.0.0.1
@EnableDiscoveryClient
@SpringBootApplication
public class PaymentMain8006 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8006.class, args);
}
}
@RestController
@Slf4j
public class PaymentController {
@Value("${server.port}")
private String serverPort;
@GetMapping("/payment/consul")
public String paymentConsul() {
return "SpringCloud with consul: " + serverPort + "\t" + UUID.randomUUID().toString();
}
}
服务消费者注册进Consul
dependencies>
dependency>
groupId>org.springframework.cloudgroupId>
artifactId>spring-cloud-starter-consul-discoveryartifactId>
dependency>
dependency>
groupId>springcloudgroupId>
artifactId>cloud-api-commonsartifactId>
version>${project.version}version>
dependency>
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-webartifactId>
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>
#consul服务端口号
server:
port: 80
spring:
application:
name: consul-consumer-order
cloud:
consul: #consul注册中心
host: localhost
port: 8500
discovery:
service-name: ${spring.application.name}
#hostname: 127.0.0.1
@SpringBootApplication
@EnableDiscoveryClient
public class OrderConsulMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderConsulMain80.class,args);
}
}
@Configuration
public class ApplicationContextConfig {
@LoadBalanced
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
@RestController
@Slf4j
public class OrderConsulController {
private static final String INVOKE_URL = "http://consul-provider-payment";
@Resource
private RestTemplate restTemplate;
@GetMapping("/consumer/consul")
public String paymentInfo() {
String url = INVOKE_URL + "/payment/consul/";
String result = restTemplate.getForObject(url, String.class);
return result;
}
}
与Eureka的区别
上一篇:python之网络编程
文章标题:springcloud-Consul服务注册与发现(三)
文章链接:http://soscw.com/index.php/essay/83701.html