💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
**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 ```