欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 大家好,从本章开始我们一起进入SpringCloud的源码世界,通过源码分析再结合实战,一起
欢迎访问我的GitHub
这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos
- 大家好,从本章开始我们一起进入SpringCloud的源码世界,通过源码分析再结合实战,一起加深对SpringCloud体系的认识;
全文概览
- 本章是为后续的深度学习做准备工作,主要包含以下两部分:
- 开发三个简单应用,包括:注册中心、服务提供方、服务消费方,后续研究和实战都在这三个应用基础上进行;
- 下载后续分析研究中用到的源码,包括SpringCloud和Netflix的,版本和三个简单应用的一致;
版本列表
JDK:1.8;
spring boot:1.5.9.RELEASE;
spring cloud:Edgware.RELEASE;
应用简介
应用名称
作用
占用端口
springclouddeepeureka
注册中心
8081
springclouddeepprovider
服务提供方
8082
springclouddeepconsumer
服务消费方
8083
实战源码下载
- 稍后会详细介绍这三个应用的开发和测试步骤,您也可以在github下载这三个应用的源码,地址和链接信息如下表所示:
名称
链接
备注
项目主页
https://github.com/zq2599/blog_demos
该项目在GitHub上的主页
git仓库地址(https)
https://github.com/zq2599/blog_demos.git
该项目源码的仓库地址,https协议
git仓库地址(ssh)
git@github.com:zq2599/blog_demos.git
该项目源码的仓库地址,ssh协议
- 这个git项目中有多个文件夹,本章源码分别在springclouddeepeureka、springclouddeepprovider、springclouddeepconsumer这三个文件夹下,如下图红框所示:
- 接下来准备demo工程吧,先从Eureka开始:
Eureka应用
- 基于maven创建一个spring boot的web应用springclouddeepeureka,pom.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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.bolingcavalry</groupId>
<artifactId>springclouddeepeureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springclouddeepeureka</name>
<description>Demo project for Eureka server</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!--spring cloud依赖管理-->
<!-- 引入spring cloud的依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- resources目录下创建配置文件application.yml,内容如下:
server:
port: 8081
eureka:
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://localhost:8081/eureka/
- 启动类SpringclouddeepeurekaApplication.java中,添加注解@EnableEurekaServer:
package com.bolingcavalry.springclouddeepeureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class SpringclouddeepeurekaApplication {
public static void main(String[] args) {
SpringApplication.run(SpringclouddeepeurekaApplication.class, args);
}
}
- 启动应用,浏览器访问8081端口,可见Eureka页面如下图:
服务提供者
- 基于maven创建一个spring boot的web应用springclouddeepprovider,pom.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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.bolingcavalry</groupId>
<artifactId>springclouddeepprovider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springclouddeepprovider</name>
<description>Demo project for Spring Cloud service provider</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- resources目录下创建配置文件application.yml,内容如下:
server:
port: 8082
spring:
application:
name: springcloud-deep-provider
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8081/eureka/
instance:
prefer-ip-address: true
- 启动类SpringclouddeepproviderApplication.java中,添加注解@EnableDiscoveryClient:
package com.bolingcavalry.springclouddeepprovider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class SpringclouddeepproviderApplication {
public static void main(String[] args) {
SpringApplication.run(SpringclouddeepproviderApplication.class, args);
}
}
- 创建一个Controller类,用于提供http服务:
package com.bolingcavalry.springclouddeepprovider.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @Description : 提供普通的http服务
* @Author : za2599@gmail.com
* @Date : 2018-08-18 18:25
*/
@RestController
public class HelloService {
@GetMapping("hello/{name}")
public String hello(@PathVariable String name){
return "Hello " + name + ", " + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
}
}
- 启动应用,浏览器访问路径:http://localhost:8082/hello/123 ,可收到server端响应如下图:
- 此时去刷新Eureka的页面,发现服务已经注册成功了,如下图:
- 至此,服务提供方应用开发完毕,最后一个是服务消费方springclouddeepconsumer;
服务消费方
- 基于maven创建一个spring boot的web应用springclouddeepconsumer,pom.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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.bolingcavalry</groupId>
<artifactId>springclouddeepconsumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springclouddeepconsumer</name>
<description>Demo project for Spring Cloud service consumer</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- resources目录下创建配置文件application.yml,内容如下:
server:
port: 8083
spring:
application:
name: springcloud-deep-consumer
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8081/eureka/
instance:
prefer-ip-address: true
- 启动类SpringclouddeepconsumerApplication.java中,添加注解@EnableDiscoveryClient:
package com.bolingcavalry.springclouddeepconsumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
public class SpringclouddeepconsumerApplication {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(SpringclouddeepconsumerApplication.class, args);
}
}
- 创建一个Controller类,用于提供http服务:
package com.bolingcavalry.springclouddeepconsumer.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* @Description : 远程调用测试的
* @Author : qin_zhao@kingdee.com
* @Date : 2018-08-18 19:10
*/
@RestController
public class ConsumerServiceController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private LoadBalancerClient loadBalancerClient;
@GetMapping("serviceinfo")
public String serviceinfo(){
ServiceInstance serviceInstance = loadBalancerClient.choose("springcloud-deep-provider");
return null==serviceInstance ? "service not found" : serviceInstance.toString();
}
@GetMapping("consume/{name}")
public String consume(@PathVariable String name){
return this.restTemplate.getForObject("http://springcloud-deep-provider/hello/" + name, String.class);
}
}
- 启动应用,浏览器访问路径:http://localhost:8083/consume/tom,可收到server端响应如下图:
- 再试试获取服务信息的接口:http://localhost:8083/serviceinfo,可以看到服务信息:
- 此时去刷新Eureka的页面,发现两个应用都已注册成功,如下图:
- 至此,服务消费方应用开发完毕,后续的章节中,我们的分析和实战都在这三个应用上进行;
Spring Cloud源码下载
- 简单说说如何在GitHub下载对应版本的Spring Cloud源码:
- Spring Cloud的GitHub主页:https://github.com/spring-cloud;
- 假设我要下载的是子工程spring-cloud-commons的源码,版本号1.3.0-RELEASE,点击下图红框中的链接:
- 在spring-cloud-commons工程主页中,点击下图红框中的"releases"链接:
- 如下图,这里有多个release版本,挑选您所需的版本,点击红框中的链接即可下载源码:
Netflix源码下载
欢迎关注51CTO博客:程序员欣宸
学习路上,你不孤单,欣宸原创一路相伴...
【文章原创作者:
高防cdn http://www.juniucdn.com欢迎留下您的宝贵建议】