SpringCloud简单搭建
前面已经介绍过SpringBoot与Mybatis,Redis,ActiveMQ的整合。接下来要做的是SpringCloud的项目的搭建。
在微服务的架构体系中,SpringBoot只能用于应用开发,而想要实现完整的微服务架构,还需要实现架构中的服务注册与发现,API网关和负载均衡等功能。接下来通过SpringCloud来实现架构中的这些功能。
在创建项目之前,需要了解的是SpringCloud与SpringBoot的版本号区别是:
SpringBoot是通过数字来区分版本号的,而SpringCloud是根据英文字母的顺序,采用伦敦的"地名+版本号"的方式来命名的。例如:Angel SR6,Brixton SR7,Camden SR7,Dalston SR3.其中Angel, Brixton,Camden,Dalston是地名,SR是Service Releases的编写。,后面的数字是版本号。
要求:
jdk:1.8
开发工具:IDEA
注:需要了解springcloud
1.创建基于web的Maven项目(springcloud)
2.创建服务注册中心。在springcloud项目中创建SpringBoot项目(springboot):
选中项目右击–>New–>Module
勾选Cloud Discovery–>Eureka server。以方便导包。
3.编写springboot项目
3.1查看pom.xml文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RC2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.4.2.RELEASE</version>
</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>
3.2修改application.yml文件
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http:// e u r e k a . i n s t a n c e . h o s t n a m e : {eureka.instance.hostname}: eureka.instance.hostname:{server.port}/eureka/
registerWithEureka 和 fetchRegistry 设置为false,表明自己属于服务中心主体
为什么要用yml文件?而不是xml或properties文件?
说明:xml或properties文件都是以标签来配置数据,这样就造成了资源的浪费,不利于我们开发大型项目,而yml(yaml)文件则取消了这样的配置,文件中都是以数据为中心,层级结构以空格作为层级分隔符。使数据得到突出,简化了开发,同时占用的资源也更少。
3.3编写启动类SpringbootApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
添加@EnableEurekaServer,该注解表明标注类是一个Eureka Server。
4.启动项目
在浏览器中输入http://localhost:8761/
5.创建生产者
创建方式与服务注册中心相同。
5.1查看pom.xml文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springbootclient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springbootclient</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RC2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.4.4.RELEASE</version>
</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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
5.2修改application.yml文件
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8763
spring:
application:
name: service-hi
注:端口不能与上面的相同。这里的服务name:service-hi 可以根据自己情况定义。
5.3编写启动类
在application中加入注解@EnableEurekaClient,表明自己属于一个生产者。这里为了方便测试,直接使用@RestController获取返回值。
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableEurekaClient
@RestController
public class SpringbootclientApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootclientApplication.class, args);
}
@Value("${server.port}")
String port;
@RequestMapping("/hi")
public String home(@RequestParam String name)
{
return "hi " + name + ",i am from port:" + port;
}
}
5.4运行服务
在浏览器中输入http://localhost:8765/hiname=fys,可以看到如下信息
然后查看http://localhost:8761/
上图说明我们的服务者注册成功!
6.创建消费者
6.1 创建消费者modul,流程如上述工程创建流程。
6.2查看pom.xml文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>serviceribbon</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>serviceribbon</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RC2</spring-cloud.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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>1.4.2.RELEASE</version>
</dependency>
<!--断路器-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.4.4.RELEASE</version>
</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>
6.3yml配置
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8764
spring:
application:
name: service-ribbon
在工程的配置文件指定服务的注册中心地址为http://localhost:8761/eureka/ 。#### 6.4编写启动类
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 ServiceribbonApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceribbonApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate()
{
return new RestTemplate();
}
}
@EnableDiscoveryClient表明标注类是消费者,加入restTemplate以消费相关的服务
7.创建service和controller
7.1 service层
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
public String hiService(String name)
{
return restTemplate.getForObject("http://SERVICE-HI/hi?name=" + name, String.class);
}
}
7.2 controller层
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloControler {
@Autowired
HelloService helloService;
@RequestMapping(value = "/hi")
public String hi(@RequestParam String name)
{
return helloService.hiService(name);
}
}
这里利用字符串进行传输。当然restTemplate也是可以以对象进行传输的。
8.运行服务
在浏览器中输入http://localhost:8764/hiname=admin
输入http://localhost:8761/
上面的图说明:我们的消费者注册成功!