springcloud常用注解代码示例
时间: 2023-08-28 18:08:16 浏览: 98
springCloud参考指南
1. @EnableDiscoveryClient
```java
@SpringBootApplication
@EnableDiscoveryClient
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
```
2. @EnableFeignClients
```java
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
```
3. @FeignClient
```java
@FeignClient(name = "service-provider")
public interface HelloClient {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
String hello();
}
```
4. @RestController
```java
@RestController
public class HelloController {
@Autowired
private HelloClient helloClient;
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello() {
return helloClient.hello();
}
}
```
5. @EnableCircuitBreaker
```java
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableCircuitBreaker
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
```
6. @HystrixCommand
```java
@FeignClient(name = "service-provider")
public interface HelloClient {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
@HystrixCommand(fallbackMethod = "fallback")
String hello();
default String fallback() {
return "fallback";
}
}
```
7. @EnableHystrixDashboard
```java
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableCircuitBreaker
@EnableHystrixDashboard
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
```
8. @EnableZuulProxy
```java
@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
```
9. @EnableConfigServer
```java
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
```
10. @RefreshScope
```java
@RestController
@RefreshScope
public class ConfigController {
@Value("${my.config}")
private String config;
@RequestMapping(value = "/config", method = RequestMethod.GET)
public String config() {
return config;
}
}
```
阅读全文