Original source: http://blog.didispace.com/springcloud1/
About Spring Cloud
Spring Cloud is a spring boot-based cloud application development tool for configuration management, service discovery, circuit breakers, intelligent routing, micro-agents, control buses, global locks, decision-making campaigns, Operations such as distributed sessions and cluster state management provide a simple way to develop.
Spring Cloud contains multiple sub-projects (for many different open source products involved in distributed systems), such as spring Cloud Config, Spring Cloud Netflix, Spring Cloud Cloudfoundry, Spring Cloud AWS, Spring Cloud Security, Spring Cloud Commons, Spring Cloud Zookeeper, Spring Cloud CLI, and more.
Micro-Service Architecture
The "MicroServices architecture" has been so hot in recent years that the product communities associated with microservices architectures have become more active (for example, Netflix, Dubbo), and Spring cloud is also being watched by architects and developers due to the strong visibility and influence of the spring community.
So what is a "microservices architecture"? To put it simply, a microservices architecture is to split a complete application from the data store vertically into multiple different services, each of which can be deployed independently, independently maintained, independently scaled, and invoked between services and services through, for example, restful APIs.
For the "micro-service Architecture", we can find a lot of relevant introduction and research articles on the Internet to learn and understand. You can also read the ancestor Martin Fowler's "MicroServices", this article does not do more introduction and description.
Service Registration and Discovery
After a brief introduction to Spring cloud and the microservices architecture, here's how to use spring Cloud to build a service registration and Discovery module.
We're going to use Spring cloud Netflix, one of Spring Cloud's sub-projects, mainly on the packaging of Netflix's range of open source products, which provides a self-configuring Netflix OSS integration for spring boot applications. With some simple annotations, developers can quickly configure common modules in the application and build large, distributed systems. It mainly provides modules including: Service Discovery (Eureka), Circuit breaker (hystrix), intelligent Path (Zuul), Client load Balancing (Ribbon), etc.
So, our core content here is the Service Discovery module: Eureka. Let's do some experimenting here.
Create a Service registration center
Create a basic spring boot project and pom.xml
introduce the dependent content you need in:
12345678910111213141516171819202122232425262728293031 |
<parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>1.3.5.release</version> <relativepath/> <!--lookup parent from repository to</parent> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-eureka-server</artifactid> </dependency> </dependencies> <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> </dependencymanagement> |
@EnableEurekaServer
Start a service registry with annotations to provide conversations for other apps. This step is very simple, just add this annotation to a normal spring boot application to enable this feature, such as the following example:
12345678910 |
@EnableEurekaServer @SpringBootApplication public class application { public static void main(string[] args) { new Springapplicationbuilder (Application.class). Web (True). Run (args); } |
Under the default setting, the service registry will also try to register itself as a client, so we need to disable its client registration behavior by simply application.properties
adding the following configuration in the Ask:
12345 |
server.port=1111 eureka.client.register-with-eureka=falseeureka.client.fetch-registry=false Eureka.client.serviceurl.defaultzone=http://localhost:${server.port}/eureka/ |
To differentiate the service from subsequent registrations, the port for the service registry is server.port
set to 1111
.
After starting the project, visit: http://localhost:1111/
You can see the following page, which has not found any services
Alt
The project can be found in: chapter9-1-1/eureka-server
Create a "service provider"
Below we create the client that provides the service, and register ourselves with the Service registration center.
Assuming we have a microservices module that provides computational functionality, we implement a restful API by passing in two parameters A and B, and finally returning the result of a + B.
First, create a basic spring boot application, in pom.xml
which the following configuration is added:
1234567891011121314151617181920212223242526272829303132 |
<parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>1.3.5.release</version> <relativepath/> <!--lookup parent from repository to</parent> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-eureka</artifactid> </dependency> </dependencies> <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> </dependencymanagement> |
Second, the implementation /add
of the request processing interface, through the DiscoveryClient
object, in the log to print out the relevant content of the service instance.
1234567891011121314151617 |
@RestControllerpublic class Computecontroller { Private final Logger Logger = Logger.getlogger (GetClass ()); @Autowiredprivate discoveryclient client;@RequestMapping (value = "/add", method = Requestmethod.get) Public integer add(@RequestParam integer A, @RequestParam integer b) { serviceinstance instance = client.getlocalserviceinstance (), Integer r = A + B;logger.info ("/add, Host:" + instance.gethost () + ", service_id:" + instance.getserviceid () + ", Result:" + R); return R;} } |
Finally, by adding annotations to the main class, @EnableDiscoveryClient
the annotations can activate the implementation in the Eureka in order to realize the output of the DiscoveryClient
service information in the controller.
12345678910 |
@EnableDiscoveryClient @SpringBootApplication public class computeserviceapplication { public static void main( String[] (args) {new Springapplicationbuilder (Computeserviceapplication.class). Web (True). Run (args); } |
After we have completed the implementation of the service content, we will continue to application.properties
do some configuration work, as follows:
12345 |
Spring.application.name=compute-service server.port=2222 Eureka.client.serviceurl.defaultzone=http://localhost : 1111/eureka/ |
By using spring.application.name
properties, we can specify that the name of the microservices will follow the call and only use that name to access the service.
eureka.client.serviceUrl.defaultZone
Property corresponds to the configuration content of the service registry, specifying the location of the service registry.
In order to test differentiated service providers and service registries on this computer, use server.port
the properties to set different ports.
Once the project is started, visit: http://localhost:1111/
As you can see, our defined service is registered.
Alt
Spring Cloud Building MicroServices Architecture (i) service registration and discovery