Spirng Cloud可以很容易的实现注册中心,进行服务的注册,进行服务的消费
本例子是采用gradle来实现的
首先项目结构图:
根目录gradle配置
settings.gradle
rootProject.name = 'springCloud'
include ':eureka'
include ':provider'
include ':test'
build.gradle
group 'com.zoo'
version '1.0-SNAPSHOT'
subprojects {
group 'com.zoo'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
ext {
set('springCloudVersion', "Greenwich.SR1")
set('javaVersion', "1.8")
}
}
eureka注册中心
build.gradle
plugins {
id 'org.springframework.boot' version '2.1.5.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
sourceCompatibility = javaVersion
dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
application.yml
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
application:
name: zoo-eureka
EurekaApplication
package com.zoo.eureka;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication app=new SpringApplication(EurekaApplication.class);
app.setBannerMode(Banner.Mode.OFF);// 关闭启动Banner
app.run(args);
System.out.println("eureka start completed.........");
}
}
新建provider模块
build.gradle
plugins {
id 'org.springframework.boot' version '2.1.5.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
sourceCompatibility = javaVersion
dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
implementation('org.springframework.boot:spring-boot-starter')//Core starter,包括 自动配置支持、 logging and YAML
implementation 'org.springframework.boot:spring-boot-starter-web'//构建Web,包含RESTful风格框架SpringMVC和默认的嵌入式容器Tomcat
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
application.yml
server:
port: 8010
spring:
application:
name: zoo-provider
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
ProviderApplication
package com.zoo.provider;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication app=new SpringApplication(ProviderApplication.class);
app.setBannerMode(Banner.Mode.OFF);// 关闭启动Banner
app.run(args);
System.out.println("Provider start completed.........");
}
}
HelloController
package com.zoo.provider.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author: xf
* @Date: 2019/5/23 11:27
* @Version 1.0
*/
@RestController
public class HelloController {
@RequestMapping(value = "/hello/provider")
public String helloProvider(){
return "I am is provider ,hello world";
}
}
新建test模块
build.gradle
plugins {
id 'org.springframework.boot' version '2.1.5.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
sourceCompatibility = javaVersion
dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
implementation('org.springframework.boot:spring-boot-starter')//Core starter,包括 自动配置支持、 logging and YAML
implementation 'org.springframework.boot:spring-boot-starter-web'//构建Web,包含RESTful风格框架SpringMVC和默认的嵌入式容器Tomcat
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'//feign方式远程调用需要的包
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
TestApplication
package com.zoo.test;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableEurekaClient//启用服务注册与发现
@EnableFeignClients//启用feign进行远程调用
public class TestApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(TestApplication.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);
System.out.println("Test start completed...................................");
}
//使用RestTemplate方式调用时需要,定义这个bean
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
HelloRemote
package com.zoo.test.rest;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @Author: xf
* @Date: 2019/5/23 12:49
* @Version 1.0
*/
@FeignClient(name= "zoo-provider")
public interface HelloRemote {
//这个接口要和远程调用的接口一只,参数,接口名,返回类型
@RequestMapping(value = "/hello/provider")
String helloProvider();
}
HelloController
package com.zoo.test.controller;
import com.zoo.test.rest.HelloRemote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* @Author: xf
* @Date: 2019/5/23 12:51
* @Version 1.0
*/
@RestController
public class HelloController {
@Autowired
private HelloRemote helloRemote;
@Autowired
RestTemplate restTemplate;
@RequestMapping(value = "/hello")
public String hello(){
return helloRemote.helloProvider();
}
//RestTemplate方式调用
@RequestMapping(value = "/hello2")
public String hello2(){
return restTemplate.getForEntity("http://zoo-provider/hello/provider", String.class).getBody();
}
}
总结:经过上面简单的例子,一个注册中心,一个服务提供者,一个服务调用的简单例子就实现了,可见Spring Cloud实现不同服务直接相互调用还是很简单的。