1. Github项目地址 https://github.com/zengzhen1994/springboot-learning (选择Springcloud-learning-1)
2. Springcloud服务发现、注册与消费包含三个子项目构建,分别是
Eureka服务器搭建,用于作为注册中心。服务向Eureka注册,客户端从Eureka获取服务并消费;
服务提供者;
服务消费者。
因为Springcloud是基于springboot开发的,所以默认采用springboot框架
2.1 Eureka服务器搭建
pom注入springcloud依赖包以及Eureka依赖包。
<?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.zz.springcloud</groupId>
<artifactId>eureka-server</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>eureka-server</name>
<description>Spring Cloud 1</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
</properties>
<dependencies>
<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.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>
修改配置文件 application.properties,第三行第四行是为了避免Eureka服务把自己注册进去,这里端口我设置的是81。
server.port=81
spring.application.name=eureka-server
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http\://localhost\:81/eureka/
之后启动springboot主程序,开启注解EurekaServer即可。
package com.zz.springcloud;
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);
}
}
打开网址 http://localhost:81/,出现如下图,则表示Eureka服务器搭建成功
2.2 服务者搭建
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.zz.springcloud</groupId>
<artifactId>eureka-service</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>eureka-service</name>
<description>Spring Cloud 1</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.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-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.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>
appliction.properties向Eureka注册
spring.application.name=eureka-service
server.port=79
eureka.client.serviceUrl.defaultZone=http://localhost:81/eureka/
springboot启动代码,插入注解@EnableDiscoveryClient
package com.zz.springcloud;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class ServiceApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(ServiceApplication.class).web(true).run(args);
}
}
接下来就是写一个你需要注册的服务,让服务消费者调用,这里就简单写一个给数字加1的服务。因为采用HTTP请求,所以用Rest风格发送请求获得服务
package com.zz.springcloud.controller;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Controller {
private final Logger logger = Logger.getLogger(getClass());
@Autowired
private DiscoveryClient client;
@RequestMapping(value = "/plus" ,method = RequestMethod.GET)
public int add(@RequestParam(value="number") int z) {
ServiceInstance instance = client.getLocalServiceInstance();
logger.info("host:" + instance.getHost() + ", service-id:" + instance.getServiceId());
return z+1;
}
}
启动服务者,可以看到服务者向之前打开的Eureka服务器注册了,如下图
2.3 服务消费者
pom和服务提供者一样。加入Ribbon轮询机制
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
配置同样发送到Eureka服务器,如下
spring.application.name=ribbon-consumer
server.port=83
eureka.client.serviceUrl.defaultZone=http://localhost:81/eureka/
springboot主程序注入RestTemplate的bean,因为我们要向服务提供者发送rest请求,从而获取服务。@Loadbalance开启轮询机制
package com.zz.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringCloudApplication
public class RibbonApplication {
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}
}
编写服务消费者获取服务代码,如下
package com.zz.springcloud.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class PlusService {
@Autowired
RestTemplate restTemplate;
public String plus() {
return restTemplate.getForEntity("http://EUREKA-SERVICE/plus?number=0", String.class).getBody();
}
}
之后就是服务消费者的controller部分,调用从服务提供者获取的服务
package com.zz.springcloud.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 com.zz.springcloud.service.PlusService;
@RestController
public class Controller {
@Autowired
PlusService plusService;
@RequestMapping(value = "/plusone", method = RequestMethod.GET)
public String add() {
return plusService.plus();
}
}
启动之后,输入http://localhost:83/plusone 页面如下,如果你开启多个服务提供者在不同端口,也可以在后台发现被轮询服务。
3. 到这里springcloud注册、发现、消费就完成了。之后还会介绍路由熔断、zuul服务网关以及分布式配置。