1. Introduction to Spring Cloud:
Spring Cloud是对Netflix的多个开源组件进一步的封装而成,同时又实现了和云端平台,和SpringBoot开发框架很好的集成。
Spring Cloud为开发者提供了在分布式系统(配置管理,服务发现,熔断,路由,微代理,控制总线,一次性token,全居琐,leader选举,分布式session,集群状态)中快速构建的工具,使用SpringCloud的开发者可以快速的启动服务或构建应用、同时能够快速和云平台资源进行对接。
2. Create a server
Refer to Spring Inuti to build a Spring Boot project to build a basic Boot project
Add dependencies in pom.xml:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
Add the annotation to the Application startup file: @EnableEurekaServer
@EnableEurekaServer
@SpringBootApplication
public class SpringCloudApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudApplication.class, args);
}
}
Add configuration in the configuration file application.properties:
server.port=1228
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/Eureka/
After clicking the launcher to start the project, visit: http://localhost:1228/ (note the final backslash)
3. Create a client that provides services
Refer to Spring Inuti to build a Spring Boot project to build a basic Boot project
Add dependencies in pom.xml:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
Implementing an interface to provide API support
/**
* Created by 从小就坏 on 2016/12/28.
*/
@RestController
public class HelenController {
private final Logger logger = Logger.getLogger(getClass());
@Autowired
private DiscoveryClient client;
@RequestMapping(value = "/add" ,method = RequestMethod.GET)
public String add() {
ServiceInstance instance = client.getLocalServiceInstance();
logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r);
return "HELEN";
}
}
Add the annotation to the Application startup file: @EnableEurekaServer
@EnableEurekaServer
@SpringBootApplication
public class SpringCloudApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudApplication.class, args);
}
}
Add configuration in the configuration file application.properties:
spring.application.name=compute-service
server.port=1229
eureka.client.serviceUrl.defaultZone=http://localhost:1228/eureka/
Start 2 projects and you will find that your service is registered
For architecture, microservices, distributed, if you want to know more, you can also add my group to learn: 561614305