多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
**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 ```