原文链接:http://www.dubby.cn/detail.html?id=9005
Spring Boot
新建项目
http://start.spring.io/上面可以直接下载一个完整的Spring Boot项目,只需要选择Web依赖就足够了,然后倒入到你熟悉的IDE就可以了。
编写一个Restful接口
package com.example.demo.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.metrics.CounterService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
private static final Logger logger = LoggerFactory.getLogger(HelloController.class);
@Autowired
private CounterService counterService;
@RequestMapping("hello")
public String hello() {
counterService.increment("demo.hello");
return "Hello, world." ;
}
}
启动项目
Spring Boot项目有很多启动方式:
+ 直接启动main方法
+ mvn spring-boot:run
+ 打包之后,执行java -jar xxx.jar
其实还有很多细节我没有仔细介绍,因为如果展开的说,就不是一篇文章可以塞得下了。如果感兴趣的话,可以去我的https://github.com/dubby1994/spring-boot-demo上看源代码,或者关注我的微信公众号ITBusTech,或者关注我的博客http://www.dubby.cn/,或者收藏本站,我会持续更新的。
Eureka
服务注册中心
一开始可以静态配置服务信息,不过这种方式不能动态的增加服务实例,而且随着业务的发展,系统越来越复杂,这种配置会越来越难以维护,所以需要一个服务注册中心来统一管理所有的服务的信息。
服务注册中心维护着类似这么一个东西:
服务名 | 服务信息 |
---|---|
ACCOUNT-SERVICE | 127.0.0.1:8081,127.0.0.1:8082 |
SHOP-SERVICE | 127.0.0.1:8083,127.0.0.1:8084,127.0.0.1:8085 |
… | … |
使用Netflix Eureka创建服务注册中心
首先,创建一个Spring Boot项目,参考上面的文章,不妨直接命名为eureka-server。
修改POM文件,添加必要的依赖:
<?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>cn.dubby.sb</groupId>
<artifactId>eureka-server</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.7.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-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.SR5</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>
注意:1.4.4.RELEASE是个分界点,这之前和之后,有部分类和注解被删除了,比如
@SpringApplicationConfiguration
,所以如果你遇到了这种情况,不妨先把版本改成1.4.4.RELEASE,然后注意观察所有被@Deprecated
的类和注解,并修复。
修改application.properties
:
server.port=1111
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
修改Application.java
:
package cn.dubby.sb;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
然后启动这个应用,那么一个服务注册中心就启动了。
真实使用的话,服务注册中心肯定是一个集群,目的当然是为了高可用,本篇文章目的是快速上手,所以就不介绍了。
看到这个就代表成功了:
注册服务
我们需要修改一下之前的那个demo的Spring Boot项目。
修改POM:
<?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.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.4.RELEASE</version>
<!--<version>1.5.6.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.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</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-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.SR5</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>
主要目的是为了加上spring-cloud-starter-eureka-server
。
修改HelloController.java
:
package com.example.demo.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.metrics.CounterService;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
private static final Logger logger = LoggerFactory.getLogger(HelloController.class);
@Autowired
private CounterService counterService;
@Autowired
private DiscoveryClient discoveryClient;
@RequestMapping("hello")
public String hello() {
counterService.increment("demo.hello");
ServiceInstance serviceInstance = discoveryClient.getLocalServiceInstance();
String message = "host:" + serviceInstance.getHost() + ",serviceId:" + serviceInstance.getServiceId() + ",uri:" + serviceInstance.getUri();
logger.info(message);
return "Hello, world." + message;
}
}
然后在DemoApplication.java
增加@EnableDiscoveryClient
,激活Eureka。
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
然后修改application.properties
:
server.port=8080
management.security.enabled=false
endpoints.shutdown.enabled=true
spring.application.name=demo-service
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
然后启动这个应用吧,再去http://localhost:1111看看:
可以看到这个服务已经被注册成功了。
Ribbon
Ribbon是啥
Ribbon 是 Netflix 发布的云中间层服务开源项目,其主要功能是提供客户侧软件负载均衡算法,将 Netflix 的中间层服务连接在一起。
创建服务消费者
首先,还是创建一个Spring Boot项目,不妨就命名为ribbon-consumer
吧。然后改一下POM文件:
<?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>cn.dubby.sb</groupId>
<artifactId>ribbon-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ribbon-consumer</name>
<description>ribbon-consumer</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.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.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-actuator</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-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.SR5</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>
主要目的为了增加spring-cloud-starter-ribbon
这个依赖。
修改启动类RibbonConsumerApplication.java
:
package cn.dubby.sb;
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 RibbonConsumerApplication {
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(RibbonConsumerApplication.class, args);
}
}
创建一个ConsumerController.java
吧:
package cn.dubby.sb.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* Created by teeyoung on 17/8/18.
*/
@RestController
public class ConsumerController {
@Autowired
RestTemplate restTemplate;
@RequestMapping(value = "ribbon-consumer", method = RequestMethod.GET)
public String consumer() {
return restTemplate.getForEntity("http://DEMO-SERVICE/hello", String.class).getBody();
}
}
启动服务消费者
启动main方法,看到下面几张图就可以了。
注册中心:
请求消费者的controllerhttp://localhost:9000/ribbon-consumer:
OK,这时候,你可以去demo-service这个服务去看看,有没有log打出来,正常的情况下是已经可以看到log的了。这就代表你已经成功的搭起来一个最基本的Spring Cloud的应用了。