微服务示例-Spring Cloud

1~开发准备

  JDK:1.8

  Spring Boot:1.5.9.RELEASE

  Spring Coud:Edgware.RELEASE

  IDE:IntelliJ IDEA 2017

  Maven:3.3.9

2~创建服务注册中心

  File->New->Projects->Spring Initializr

微服务示例-Spring Cloud

点击Next,填写信息如下:

微服务示例-Spring Cloud

然后点击Next,选择如下:

微服务示例-Spring Cloud

点击Next,然后点击Finish.项目结构如下:微服务示例-Spring Cloud

其中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.human</groupId>
<artifactId>sbc-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>sbc-service</name>
<description>Demo project for Spring Boot</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>
<spring-cloud.version>Edgware.RELEASE</spring-cloud.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</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>${spring-cloud.version}</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.properties文件,内容如下:

server.port=8081
spring.application.name=human-service
eureka.instance.hostname=localhost
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

然后修改主程序代码如下:

package com.human.sbcservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication
@EnableEurekaServer
public class SbcServiceApplication { public static void main(String[] args) {
SpringApplication.run(SbcServiceApplication.class, args);
}
}

至此,一个微服务注册中心完成

3~创建微服务提供者

 大体上与创建注册中心相同,在选择依赖的时候如下图

微服务示例-Spring Cloud

选择完毕,点击Next,然后点击Finish,完成后项目结构如图:

微服务示例-Spring Cloud

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.human</groupId>
<artifactId>sbc-user</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>sbc-user3</name>
<description>Demo project for Spring Boot</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>
<spring-cloud.version>Edgware.RELEASE</spring-cloud.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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>${spring-cloud.version}</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>

然后打开主程序文件,修改代码如下:

package com.human.sbcuser;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient//Eureka微服务注解
@SpringBootApplication
public class SbcUserApplication { public static void main(String[] args) {
SpringApplication.run(SbcUserApplication.class, args);
}
}

修改application.properties文件,内容如下:

server.port=8082
eureka.client.service-url.defaultZone=http\://localhost\:8081/eureka/
spring.application.name=cloud-simple-service

新建一个名为HelloController文件,具体代码:

package com.human.sbcuser;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
public class HelloController {
@RequestMapping("/")
@ResponseBod
public String hello()
{
return "hello KinY ~KoKo";
}
}

至此,一个简单的服务提供者创建完毕

4~创建微服务消费者

利用Rbbon创建客户端,其中group:com.human,Artifact:sbc-consumer,剩下基本上与创建注册中心相同,其中选择依赖时如下图:

  微服务示例-Spring Cloud

完成后,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.human</groupId>
<artifactId>sbc-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>sbc-service</name>
<description>Demo project for Spring Boot</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>
<spring-cloud.version>Edgware.RELEASE</spring-cloud.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-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</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>${spring-cloud.version}</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>

修改程序主类,内容如下:

package com.human.sbconsumer;

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; @EnableDiscoveryClient
@SpringBootApplication
public class SbcConsumerApplication { @Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(SbcConsumerApplication.class, args);
} }

修改配置文件application.properties,内容如下:

server.port=8083
eureka.client.service-url.defaultZone=http\://localhost\:8081/eureka/
spring.application.name=cloud-simple-consumer

新增HelloController文件,内容如下:

package com.human.sbconsumer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; @RestController
public class HelloController {
@Autowired
RestTemplate restTemplate;
@RequestMapping("/")
public String hello()
{
return restTemplate.getForEntity("http://cloud-simple-service/",String.class).getBody();
}
}

至此编码完成.

5~运行

先运行注册中心项目(sbc-service),然后运行服务提供者项目(sbc-user),打开浏览器输入地址:http://localhost:8081/,显示内容如下:微服务示例-Spring Cloud

其中红色框内,说明服务提供者注册成功,此时访问地址:http://localhost:8082/  输出文本:hello KinY ~KoKo,说明服务提供者正常运行,下面运行服务消费者(sbc-consumer),再访问http://localhost:8081/  发现变成如下内容:微服务示例-Spring Cloud

说明服务客户端也成功注册,下面直接访问  http://localhost:8083/  浏览器输出文本:hello KinY ~KoKo 大功告成!!
至此一个简配的微服务架构搭建完毕.

6~补充

  ①:在选择端口时注意不要与其他程序冲突
  ②:其中消费者基于Ribbon创建,Ribbon是基于http和tcp的客户端负载均衡器

上一篇:C++11 auto类型说明符的使用


下一篇:python3在anaconda下安装caffe失败