分布式系统设计模式及其实现
// 文件
package cn.yulei.server.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties("service")
public class ServiceProperties {
private String name;
private boolean dualLive;
private int port;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isDualLive() {
return dualLive;
}
public void setDualLive(boolean dualLive) {
this.dualLive = dualLive;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
}
// 文件
package cn.yulei.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
// 配置文件
server:
port: 8081
eureka:
instance:
appname: server
prefer-ip-address: true
client:
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
cloud:
config:
label: master
server:
git:
uri: https://github.com/example/demo-configs.git
// 文件
package cn.yulei.server.registry;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties;
import org.springframework.cloud.client.serviceregistry.EurekaAutoServiceRegistration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnProperty(value = "", havingValue = "true", matchIfMissing = true)
public class EurekaRegistrationBean {
@Value("${}")
private String appName;
@Bean
public AutoServiceRegistrationProperties autoServiceRegistrationProperties() {
AutoServiceRegistrationProperties properties = new AutoServiceRegistrationProperties();
properties.setServiceId(appName);
return properties;
}
@Bean
public EurekaAutoServiceRegistration eurekaAutoServiceRegistration(
AutoServiceRegistrationProperties registration) {
EurekaAutoServiceRegistration eurekaAutoServiceRegistration =
new EurekaAutoServiceRegistration(registration, null);
return eurekaAutoServiceRegistration;
}
}
// 文件
package cn.yulei.server;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.bootstrap.BootstrapImportSelector;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.util.ClassUtils;
@Order(Ordered.LOWEST_PRECEDENCE - 10)
public class Bootstrap implements BootstrapImportSelector {
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
if (ClassUtils.isPresent("", getClass().getClassLoader())) {
Class<?> consulClass = ClassUtils
.resolveClassName("",
Thread.currentThread().getContextClassLoader());
try {
Object configurationClass = consulClass.getDeclaredConstructor().newInstance();
Method method =
configurationClass.getClass().getMethod("isEnabled", ClassLoader.class);
Boolean enabled = (Boolean) method.invoke(configurationClass, getClass().getClassLoader());
if (!enabled) {
throw new IllegalArgumentException(
"Please add dependency to'spring-cloud-starter-consul' in your project");
}
} catch (NoSuchMethodException | SecurityException | InstantiationException
| IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
return new String[]{""};
}
public static void main(String[] args) {
new SpringApplicationBuilder(DemoApplication.class).web(false).run(args);
}
}
// 文件
package cn.yulei.server.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.config.environment.EnvironmentRepository;
import org.springframework.cloud.config.server.config.ConfigServerHealthIndicator;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Collections;
@RestController
@RequestMapping("/{application}/{profile}[/{label}]")
public class ConfigController {
@Autowired
private EnvironmentRepository environmentRepository;
@Autowired
private ConfigServerHealthIndicator healthIndicator;
@GetMapping
public ResponseEntity<String> repo(@PathVariable String application,
@PathVariable String profile,
@PathVariable(required = false) String label) {
return ResponseEntity.ok(this.environmentRepository.findOne(application, profile,
label == null? "" : label));
}
@GetMapping("/health")
public ResponseEntity<Void> health() {
if (this.healthIndicator!= null &&!this.healthIndicator.health().getStatus().is2xxSuccessful()) {
return ResponseEntity.status(500).build();
} else {
return ResponseEntity.ok().build();
}
}
}