SpringCloud 是微服务中的翘楚,最佳的落地方案。
Eureka 作为注册中心,是 SpringCloud 体系中最重要最核心的组件之一。
Feign 使用接口加注解的方式调用服务,配合 Eureka 还能实现负载均衡。
GitHub地址:https://github.com/intomylife/SpringCloud
4.0.0
com.zwc
springcloud-eureka-commons
1.0
springcloud-eureka-commons
公用工程
jar
UTF-8
1.8
Cairo-SR3
Finchley.RELEASE
io.spring.platform
platform-bom
${platform-bom.version}
pom
import
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud-dependencies.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
此工程下有四个模块:一个注册中心,两个提供者以及一个消费者
4.0.0
com.zwc
springcloud-eureka-service
1.0
com.zwc
springcloud-eureka-registry-service
0.0.1-SNAPSHOT
springcloud-eureka-registry-service
注册中心
jar
com.zwc
springcloud-eureka-commons
1.0
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-maven-plugin
# 端口
server:
port: 8761
# 应用名称
spring:
application:
name: eurka-server
eureka:
instance:
# 使用 ip 代替实例名
prefer-ip-address: true
# 实例的主机名
hostname: ${spring.cloud.client.ip-address}
# 实例的 ID 规则
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
client:
# 是否向注册中心注册自己
registerWithEureka: false
# 是否向注册中心获取注册信息
fetchRegistry: false
serviceUrl:
# 注册中心地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
package com.zwc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class SpringcloudEurekaRegistryServiceApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudEurekaRegistryServiceApplication.class, args);
}
}
1. 项目启动成功后访问 http://localhost:8761/ 即可看到 eureka-server 主页面
4.0.0
com.zwc
springcloud-eureka-providerfirst-service
1.0
com.zwc
springcloud-eureka-providerfirst-service-core
1.0
springcloud-eureka-providerfirst-service-core
提供者一号服务工程 - 核心
jar
com.zwc
springcloud-eureka-commons
1.0
com.zwc
springcloud-eureka-providerfirst-service-api
1.0
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-maven-plugin
# 端口
server:
port: 8090
# 应用名称
spring:
application:
name: say-hello
eureka:
instance:
# 使用 ip 代替实例名
prefer-ip-address: true
# 实例的主机名
hostname: ${spring.cloud.client.ip-address}
# 实例的 ID 规则
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
client:
serviceUrl:
# 注册中心地址
defaultZone: http://${eureka.instance.hostname}:8761/eureka/
package com.zwc.providerfirst.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @ClassName SayHelloController
* @Desc TODO Say Hello
* @Date 2019/5/15 15:28
* @Version 1.0
*/
@RestController
public class SayHelloController {
/*
* @ClassName SayHelloController
* @Desc TODO 读取配置文件中的端口
* @Date 2019/5/15 15:49
* @Version 1.0
*/
@Value("${server.port}")
private String port;
/*
* @ClassName SayHelloController
* @Desc TODO Say Hello
* @Date 2019/5/15 15:30
* @Version 1.0
*/
@RequestMapping("/hello")
public String hello(){
return "Hello Spring Cloud!!!port:" + port;
}
}
package com.zwc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class SpringcloudEurekaProviderfirstServiceCoreApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudEurekaProviderfirstServiceCoreApplication.class, args);
}
}
1. 项目启动成功后访问 http://localhost:8090/hello 看到输出内容 'Hello Spring Cloud!!!port:8090'
2. 刷新 http://localhost:8761/(注册中心)可以看到服务已经被注册进来了
3. 同理,还有一个提供者工程只是端口不一致,也启动起来
4. 项目启动成功后访问 http://localhost:8091/hello 看到输出内容 'Hello Spring Cloud!!!port:8091'
5. 再次刷新 http://localhost:8761/(注册中心)可以看到相同的服务有两个提供者
4.0.0
com.zwc
springcloud-eureka-consumer-service
1.0
com.zwc
springcloud-eureka-consumer-service-core
1.0
springcloud-eureka-consumer-service-core
消费者服务工程 - 核心
jar
com.zwc
springcloud-eureka-commons
1.0
com.zwc
springcloud-eureka-consumer-service-api
1.0
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-openfeign
org.springframework.boot
spring-boot-maven-plugin
# 端口
server:
port: 8080
# 应用名称
spring:
application:
name: service-feign
eureka:
instance:
# 使用 ip 代替实例名
prefer-ip-address: true
# 实例的主机名
hostname: ${spring.cloud.client.ip-address}
# 实例的 ID 规则
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
client:
serviceUrl:
# 注册中心地址
defaultZone: http://${eureka.instance.hostname}:8761/eureka/
package com.zwc.consumer.api.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @ClassName FeignApi
* @Desc TODO 使用 Feign 调用 Api - 接口
* @Date 2019/5/15 16:11
* @Version 1.0
*/
@FeignClient("say-hello")
public interface FeignApi {
/*
* @ClassName FeignApi
* @Desc TODO 通过 say-hello 服务名调用 /hello 方法
* @Date 2019/5/15 16:17
* @Version 1.0
*/
@RequestMapping("/hello")
String hello();
}
package com.zwc.consumer.controller;
import com.zwc.consumer.api.feign.FeignApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @ClassName FeignController
* @Desc TODO 使用 Feign 调用 Api - 前端控制器
* @Date 2019/5/15 16:18
* @Version 1.0
*/
@RestController
public class FeignController {
@Autowired(required = false)
private FeignApi feignApi;
/*
* @ClassName FeignController
* @Desc TODO 调用 Say Hello 方法
* @Date 2019/5/15 16:20
* @Version 1.0
*/
@RequestMapping("/feign")
public String feign(){
return feignApi.hello();
}
}
package com.zwc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class SpringcloudEurekaConsumerServiceCoreApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudEurekaConsumerServiceCoreApplication.class, args);
}
}
1. 项目启动成功后多次访问 http://localhost:8080/feign
2. 可以发现轮流输出 'Hello Spring Cloud!!!port:8090' 和 'Hello Spring Cloud!!!port:8091'
3. 此时已经达到了负载均衡的效果
4. 再次刷新 http://localhost:8761/(注册中心)可以看到此时多了一个消费者
CentOS7中使用Docker简单部署SpringCloud项目
希望能够帮助到你
over