springcloud starter(一)
Spring Cloud - Getting Started Example,
转载自:https://www.logicbig.com/tutorials/spring-framework/spring-cloud/hello-world.html
Following is a quick-start example of Spring Cloud. We are going to develop very simple microservices using Spring Cloud, Spring Boot and Eureka Server.
In microservice architecture, an application is composed of loosely coupled small services as opposed to a single monolithic application.
In microservice architecture a registry service is used to register the microservices so that they can be discovered.
Example
In this example we are going to use Eureka Server as the service registry. Eureka is developed by Netflix; it is open source. Spring has integrated Eureka into dedicated Spring Cloud modules to make it easier to use it.
We are going to develop two microservices:
First is 'hello-service' which will just return a hello message.
Second service 'hello-web-client-service' will handle the request coming from a client. On receiving a request it will call 'hello-service' and will return a web page in response.
There will be three separate servers; one for Eureka and two of microservices. Also there will be three separate maven projects.
Eureka Server
Maven dependencies
pom.xml
<project .....> <modelVersion>4.0.0</modelVersion> <groupId>com.logicbig.example</groupId> <artifactId>hello-eureka-server</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project>
Configuration
src/main/resources/application.yml
server: port: 7777 eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false
In above configuration, the properties eureka.client.* are related to the service clients who want to register with Eureka.
The property eureka.client.register-with-eureka=false specifies that this server should not be registered to the service client itself.
The property eureka.client.fetch-registry=false specifies that the server should not fetch the registered information to itself.
Main class
@SpringBootApplication @EnableEurekaServer public class HelloEurekaServerMain { public static void main(String[] args) { SpringApplication.run(HelloEurekaServerMain.class, args); } }
Run above main class from your IDE. That will start the Eureka Server.
Now we can access the Eureka server at http://localhost:7777 as shown:
hello-service
Maven dependencies
pom.xml
<project .....> <modelVersion>4.0.0</modelVersion> <groupId>com.logicbig.example</groupId> <artifactId>hello-service</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project>
Domain object
public class HelloObject { private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
A Rest Controller
@RestController public class HelloController { private AtomicLong counter = new AtomicLong(); @GetMapping("/hello") public HelloObject getHelloWordObject() { HelloObject hello = new HelloObject(); hello.setMessage("Hi there! you are number " + counter.incrementAndGet()); return hello; } }
src/main/resources/application.properties
eureka.client.serviceUrl.defaultZone=http://localhost:7777/eureka/
src/main/resources/bootstrap.properties
spring.application.name=hello-service
Boot main class
@SpringBootApplication @EnableDiscoveryClient public class HelloServiceMain{ public static void main(String[] args) { SpringApplication.run(HelloServiceMain.class, args); } }
Run above main class from your IDE.
On refreshing Eureka page you should see HELLO-SERVICE instance listed in the registry:
hello-web-client-service
pom.xml
<project .....> <modelVersion>4.0.0</modelVersion> <groupId>com.logicbig.example</groupId> <artifactId>hello-web-client-service</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project>
A Spring MVC Controller
@Controller public class HelloWebClientController { @Autowired private DiscoveryClient discoveryClient; @GetMapping("/") public String handleRequest(Model model) { //accessing hello-service List<ServiceInstance> instances = discoveryClient.getInstances("hello-service"); if (instances != null && instances.size() > 0) {//todo: replace with a load balancing mechanism ServiceInstance serviceInstance = instances.get(0); String url = serviceInstance.getUri().toString(); url = url + "/hello"; RestTemplate restTemplate = new RestTemplate(); HelloObject helloObject = restTemplate.getForObject(url, HelloObject.class); model.addAttribute("msg", helloObject.getMessage()); model.addAttribute("time", LocalDateTime.now()); } return "hello-page"; } }
c/main/resources/templates/hello-page.html
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <body> <h2>Hello Page</h2> <div th:text="${msg}"/> <div>Time: <span th:text="${time}"/></div> </body> </html>
src/main/resources/application.properties
server.port=9080
eureka.client.serviceUrl.defaultZone=http://localhost:7777/eureka/
src/main/resources/bootstrap.properties
spring.application.name=hello-service
Boot main class
@SpringBootApplication @EnableDiscoveryClient public class HelloWebClientServiceMain { public static void main(String[] args) { SpringApplication.run(HelloWebClientServiceMain.class, args); } }
Run above class from your IDE.
Refresh Eureka web page again:
Final output
Now make request to 'hello-web-client-service' by entering localhost:9080 in web-browser:
Example Project
Dependencies and Technologies Used:
- Spring Boot 2.0.4.RELEASE
Corresponding Spring Version 5.0.8.RELEASE - Spring Cloud Finchley.SR1
- spring-cloud-starter-netflix-eureka-server 2.0.1.RELEASE: Spring Cloud Starter Netflix Eureka Server.
- spring-boot-starter-web : Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container.
- spring-cloud-starter-netflix-eureka-client 2.0.1.RELEASE: Spring Cloud Starter Netflix Eureka Client.
- spring-boot-starter-thymeleaf : Starter for building MVC web applications using Thymeleaf views.
Uses org.thymeleaf:thymeleaf-spring5 version 3.0.9.RELEASE - JDK 1.8
- Maven 3.5.4