eclipse搭建Spring Boot+Spring Cloud微服务代码
时间: 2023-08-23 14:23:37 浏览: 183
好的,下面是一个简单的示例代码,演示如何在eclipse中搭建Spring Boot+Spring Cloud微服务:
1. 注册中心(Eureka Server):
```java
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
```
2. 微服务提供者:
```java
@SpringBootApplication
@EnableDiscoveryClient
public class ProductServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ProductServiceApplication.class, args);
}
@RestController
class ProductController {
@GetMapping("/products/{id}")
public String getProductById(@PathVariable Long id) {
return "Product Id: " + id;
}
}
}
```
3. 微服务消费者:
```java
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class OrderServiceApplication {
public static void main(String[] args) {
SpringApplication.run(OrderServiceApplication.class, args);
}
@Autowired
private ProductServiceClient productServiceClient;
@RestController
class OrderController {
@GetMapping("/orders/{id}")
public String getOrderById(@PathVariable Long id) {
String product = productServiceClient.getProductById(id);
return "Order Id: " + id + ", Product: " + product;
}
}
}
@FeignClient("product-service") // 与提供者应用的spring.application.name相同
interface ProductServiceClient {
@GetMapping("/products/{id}")
String getProductById(@PathVariable("id") Long id);
}
```
以上代码仅供参考,实际开发中可能会更加复杂。希望这些信息能够对您有所帮助,如果您还有其他问题,可以随时问我。
阅读全文