本文共 4928 字,大约阅读时间需要 16 分钟。
依赖:
cloud2020 pers.zhang.springcloud 1.0-SNAPSHOT 4.0.0 cloud-providerconsul-payment8006 pers.zhang.springcloud cloud-api-commons ${project.version} org.springframework.cloud spring-cloud-starter-consul-discovery org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-devtools runtime true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test cn.hutool hutool-all RELEASE test cn.hutool hutool-all RELEASE test
配置:application.yml
server: port: 8006spring: application: name: consul-provider-payment #consul注册中心 cloud: consul: host: localhost port: 8500 discovery: service-name: ${ spring.application.name}
Controller:
@RestController@Slf4jpublic class PaymentController { @Value("${server.port}") private String serverPort; @RequestMapping(value = "/payment/consul") public String paymentConsul(){ return "springcloud with consul" + serverPort + "\t" + UUID.randomUUID().toString(); }}
启动类:
@SpringBootApplication@EnableDiscoveryClientpublic class PaymentMain8006 { public static void main(String[] args) { SpringApplication.run(PaymentMain8006.class, args); }}
依赖:
cloud2020 pers.zhang.springcloud 1.0-SNAPSHOT 4.0.0 cloud-consumerconsul-order80 org.springframework.cloud spring-cloud-starter-consul-discovery org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-devtools runtime true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test
配置:
application.ymlserver: port: 80spring: application: name: cloud-consumer-order #consul注册中心 cloud: consul: host: localhost port: 8500 discovery: service-name: ${ spring.application.name}
RestTemplate:
@Configurationpublic class ApplicationContextConfig { @Bean @LoadBalanced public RestTemplate getRestTemplate(){ return new RestTemplate(); }}
Controller:
@RestController@Slf4jpublic class OrderConsulController { private static final String INVOKE_URL = "http://consul-provider-payment"; @Resource RestTemplate restTemplate; @GetMapping("/consumer/payment/consul") public String paymentInfo(){ String result = restTemplate.getForObject(INVOKE_URL + "/payment/consul", String.class); return result; }}
启动类:
@SpringBootApplication@EnableDiscoveryClientpublic class OrderConsulMain80 { public static void main(String[] args) { SpringApplication.run(OrderConsulMain80.class, args); }}
启动consul,再启动两个微服务:
访问:localhost:8500
访问:localhost/consumer/payment/consul
转载地址:http://ospqb.baihongyu.com/