Summary: Spring cloud is built on spring boot to simplify the toolset for distributed system building, providing developers with some common patterns for rapidly building distributed systems.
For example: Configuration Management (config management), Service Discovery (services discovery), circuit breakers (circuit breakers), intelligent routing (intelligent routing),
Micro-agent (micro-proxy), control bus, one-time token (one-time tokens), Global lock (Globals locks), leading the election (leadership election),
Distributed session (distributed sessions), cluster status (cluster state).
Spring Cloud contains multiple sub-projects:
Example: Spring Cloud Config, spring cloud Netflix, etc.
Spring Cloud Project Home: http://projects.spring.io/spring-cloud/
About service discovery
In the MicroServices architecture, Service Discovery (Discovery) is one of the key principles. Manually configuring each client or some form of convention is difficult and fragile. Spring Cloud provides a variety of ways to implement service discovery,
For example: Eureka, Consul, Zookeeper. Spring Cloud is best supported by Eureka, followed by Consul, the zookeeper.
- In a production environment, we often configure a host for each application, using host instead of IP for access. To get closer to the production environment, as well as the following Docker chapters, let's first configure the host
code example
- Create a MAVEN project (Microservice-discovery-eureka) and add the following to the Pom.xml:
<?xml version="1.0" encoding="UTF-8"?><Projectxmlns="http://maven.apache.org/POM/4.0.0"Xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" ><modelversion>4.0.0</Modelversion><Artifactid>microservice-discovery-eureka</Artifactid><Packaging>jar</Packaging><Parent><Groupid>com.itmuch.cloud</Groupid><Artifactid>spring-cloud-microservice-study</Artifactid><version>0.0.1-snapshot</version > </parent> < dependencies> <dependency> <groupid>org.springframework.cloud</< Span class= "Hljs-name" >groupid> <artifactid> Spring-cloud-starter-eureka-server</artifactId> </dependency> </ Dependencies></PROJECT>
- Write Spring boot launcher: Declare a registry through @enableeurekaserver:
/** * 使用Eureka做服务发现。 * @author eacdy */@SpringBootApplication@EnableEurekaServerpublic class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); }}
|
- By default, Eureka will try to register itself as a client, so in stand-alone mode, we need to disable this behavior, just configure it in Application.yml as follows:
server: port: 8761 # specifies the Eureka instance port eureka: Instance: Hostname:discovery # specifies the host name of the Eureka instance Span class= "hljs-attr" > Client: Registerwitheureka: false fetchregistry: false ServiceUrl: defaultzone:http://${eureka.instance.hostname}:${server.port}/eureka/ |
# 参考文档:http://projects.spring.io/spring-cloud/docs/1.0.3/spring-cloud.html#_standalone_mode
# 参考文档:http://my.oschina.net/buwei/blog/618756
After starting the project, visit: http://discovery:8761/, such as. We will find that there are no services registered to Eureka at this time.
Code address (optional)
Http://git.oschina.net/itmuch/spring-cloud-study/tree/master/microservice-discovery-eureka
Https://github.com/eacdy/spring-cloud-study/tree/master/microservice-discovery-eureka
Spring Cloud First Knowledge