ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
**1. 构建服务端模块:cloud-provider-payment8006** **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: 8006 spring: application: name: consul-payment-service cloud: consul: host: localhost port: 8500 discovery: service-name: ${spring.application.name} #当前模块注册到consul微服务名 ``` **4. 在当前模块的启动类标注注解`@EnableDiscoveryClient`** ```java @SpringBootApplication @EnableDiscoveryClient public class PaymentMain8006 { public static void main(String[] args) { SpringApplication.run(PaymentMain8006.class, args); } } ``` **5. 当前模块的 controller 层** ```java @RestController @RequestMapping("/payment") public class PaymentContrller { @Value("${server.port}") private String serverPort; @RequestMapping("/consul") public String paymentConsul() { return "port:" + serverPort; } } ``` **6. 访问当前模块:http://localhost:8006/payment/consul** ``` port:8006 ```