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
 

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

Advertisement blocked!
Please support us by unblocking advertisements in your browser.
This site displays some advertisements per page, that's how we earn some revenue to create and maintain the quality content. Please whitelist our site or disable adds blocker to support us.

 
 
 
 

Java 11 Tutorials

Java 10 Tutorials

Java 9 Tutorials

Recent Tutorials

ui-buttonui-button Getting started with Spring Cloud + Microservices  Select All  Download 
  • spring-cloud-getting-started

    • hello-eureka-server

      • src
      • pom.xml
    • hello-service
      • src
      • pom.xml
    • hello-web-client-service
      • src
      • 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><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>

springcloud starter(一)的更多相关文章

  1. spring boot:使用分布式事务seata(druid 1.1.23 / seata 1.3.0 / mybatis / spring boot 2.3.2)

    一,什么是seata? Seata:Simpe Extensible Autonomous Transcaction Architecture, 是阿里中间件开源的分布式事务解决方案. 前身是阿里的F ...

  2. 极速体验|使用 Erda 微服务观测接入 Jaeger Trace

    在大型网站系统设计中,随着分布式架构,特别是微服务架构的流行,我们将系统解耦成更小的单元,通过不断的添加新的.小的模块或者重用已经有的模块来构建复杂的系统.随着模块的不断增多,一次请求可能会涉及到十几 ...

  3. SpringCloud接入EDAS——服务发现篇

    旁白 很久没有写技术文章了,最近不是写水文就是写小说.说到底,还是最近很少研究技术的缘故,已经到了江郎才尽的地步了. 不过,LZ无意间看到自己团队的小伙伴写的一些文章,觉得还是不错的,于是便动了心思, ...

  4. Spring SpringMVC SpringBoot SpringCloud概念、关系及区别

    一.正面解读: Spring主要是基于IOC反转Beans管理Bean类,主要依存于SSH框架(Struts+Spring+Hibernate)这个MVC框架,所以定位很明确,Struts主要负责表示 ...

  5. SpringCloud注解和配置以及pom依赖说明

    在本文中说明了pom依赖可以支持什么功能,以及支持什么注解,引入该依赖可以在application.properties中添加什么配置. 1.SpringCloud 的pom依赖 序号 pom依赖 说 ...

  6. 每天学点SpringCloud(十):SpringCloud监控

    今天我们来学习一下actuator这个组件,它不是SpringCloud之后才有的,而是SpringBoot的一个starter,Spring Boot Actuator.我们使用SpringClou ...

  7. SpringCloud Hystrix

    ⒈Hystrix是什么? Hystrix使一个用于处理分布式系统的延迟和容错的开源库.在分布式系统里,许多依赖不可避免的因服务超时.服务异常等导致调用失败,Hystrix能够保证在一个依赖出现问题的情 ...

  8. SpringBoot和SpringCloud面试题

    一. 什么是springboot 1.用来简化spring应用的初始搭建以及开发过程 使用特定的方式来进行配置(properties或yml文件) 2.创建独立的spring引用程序 main方法运行 ...

  9. spring、springmvc、springboot、springcloud

    Spring 最初利用“工厂模式”( DI )和“代理模式”( AOP )解耦应用组件.大家觉得挺好用,于是按照这种模式搞了一个 MVC 框架(一些用 Spring 解耦的组件),用开发 web 应用 ...

随机推荐

  1. 详解Lombok中的@Builder用法

    Builder 使用创建者模式又叫建造者模式.简单来说,就是一步步创建一个对象,它对用户屏蔽了里面构建的细节,但却可以精细地控制对象的构造过程. 基础使用 @Builder注释为你的类生成相对略微复杂 ...

  2. [刘阳Java]_Spring对Dao的支持_第10讲

    Spring框架优秀就是在于MVC开发的时候一旦需要对底层的数据库操作,它可以很好的支持JDBC技术,还有现在主流的ORM框架(Hibernate, MyBatis)技术. 重点先介绍Spring对J ...

  3. Python+Request库+第三方平台实现验证码识别示例

    1.登录时经常的出现验证码,此次结合Python+Request+第三方验证码识别平台(超级鹰识别平台) 2.首先到超级鹰平台下载对应语言的识别码封装,超级鹰平台:http://www.chaojiy ...

  4. MySQL 8.x 新版本特性赶紧学!!Linux 服务器上安装 MySQL 8.x

    我是陈皮,一个在互联网 Coding 的 ITer,微信搜索「陈皮的JavaLib」第一时间阅读最新文章,回复[资料],即可获得我精心整理的技术资料,电子书籍,一线大厂面试资料和优秀简历模板. 引言 ...

  5. POJ4007 Flood-it! 题解

    调得我快死了啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊!!!! 先自己写了几发,老是 T,然后去看题解,大体思路居然都差不多,估计是自己写挂了orz. 几乎所有题解都有个vis数组,真 nm 看不懂到底是什么意思 ...

  6. Bootstrap框架--DataTables列表示例--添加判断

    一.参考代码 <%@ include file="./include/header.jsp"%> <!-- jquery.dataTables.css --> ...

  7. 第十四篇 -- CPU学习二——此部分重点在AMD CPU

    一.CPU的一些英文简写或解释 Definitions: ACPI--Advanced Configuration and Power Interface APP--Adjusted Peak Per ...

  8. 《笨方法学python》随笔

    字符串 字符串可以包含格式化字符%s,只要将格式化的变量放到字符串中,再紧跟着一个百分号 %(percent), 再紧跟着变量名即可.唯一要注意的地方是, 如果你想要在字符串中通过格式化字符放入多个变 ...

  9. 网安日记③之通过iis搭建ftp并使用通过serv-u搭建ftp

    通过iis搭建ftp并使用通过serv-u搭建ftp 安装iis的ftp访问 由于在安装iis时勾选了ftp服务,我们直接在iis界面右键ftp服务打开属性查看本地路径 在电脑目录下打开安装目录,并在 ...

  10. .net 知新:【4】NuGet简介和使用

    在包管理以前我们在项目中引用第三方包通常是去下载dll放到项目中再引用,后来逐渐发展成各种包管理工具,nuget就是一种工具,适用于任何现代开发平台的基本工具可充当一种机制,通过这种机制,开发人员可以 ...