🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
**1. 构建消费端模块:cloud-comsumer-consul-order80** **2. 在当前模块添加 consul-discovery 依赖** ```xml <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> ... </dependencies> ``` **3. 当前模块的`application.yml`** ```yml server: port: 80 spring: application: name: cloud-comsumer-order cloud: consul: port: 8500 host: localhost discovery: service-name: ${spring.application.name} #当前模块注册到consul微服务名 ``` **4. 在当前模块的启动类标注注解`@EnableDiscoveryClient`** ```java @SpringBootApplication @EnableDiscoveryClient public class OrderConsulMain80 { public static void main(String[] args) { SpringApplication.run(OrderConsulMain80.class, args); } } ``` **5. 配置 RestTemplate,方便访问服务端** ```java @Configuration public class ApplicationContextConfig { @Bean @LoadBalanced public RestTemplate getRestTemplate() { return new RestTemplate(); } } ``` **6. 当前模块的 controller 层** ```java @RestController @RequestMapping("/order") public class OrderController { //consul-payment-service 为服务端 spring.cloud.consul.discovery.service-name 配置 final private String INVOKE_URL = "http://consul-payment-service"; @Resource private RestTemplate restTemplate; @GetMapping("/payment/consul") public String getPaymentInfo() { String info = restTemplate.getForObject(INVOKE_URL + "/payment/consul", String.class); return info; } } ``` **7. 访问当前模块:http://localhost:80/order/payment/consul** ``` port:8006 ```