上一篇文章中介绍了怎么搭建 Eureka 的服务端,下面介绍搭建客户端,客户端和服务端链接,怎么实现父负载均衡。
创建客户端
创建客户端和服务器有点类型,创建项目过程不做介绍了。注意看需要修改的文件 build.gradle
buildscript {
repositories {
// 阿里镜像
maven{ url "http://maven.aliyun.com/nexus/content/groups/public/"}
mavenCentral()
jcenter()
}
dependencies {
classpath group: 'org.springframework.boot', name: 'spring-boot-gradle-plugin', version: SPRING_BOOT_VERSION
classpath group: 'org.jetbrains.kotlin', name: 'kotlin-gradle-plugin', version: KOTLIN_VERSION
classpath group: 'org.jetbrains.dokka', name: 'dokka-gradle-plugin', version: DOKKA_VERSION
classpath group: 'com.google.protobuf', name: 'protobuf-gradle-plugin', version: PROTOBUF_GRADLE_VERSION
// ssh插件, 用于部署
classpath 'org.hidetake:gradle-ssh-plugin:2.10.1'
}
}
apply plugin: 'kotlin'
apply plugin: 'org.jetbrains.dokka'
apply plugin: 'war'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group 'com.wx'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
maven{ url "http://maven.aliyun.com/nexus/content/groups/public/"}
mavenCentral()
jcenter()
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
testCompile group: 'junit', name: 'junit', version: '4.12'
// kotlin 依赖
compile group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib-jdk8', version: KOTLIN_VERSION
compile group: 'org.jetbrains.kotlin', name: 'kotlin-reflect', version: KOTLIN_VERSION
//
// spring boot 依赖
providedCompile group:'org.springframework.boot', name:'spring-boot-starter-tomcat', version: SPRING_BOOT_VERSION
compile group: "org.springframework.boot", name: "spring-boot-starter-web", version: SPRING_BOOT_VERSION
// // spring cloud 依赖
// compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-eureka-server', version: SPRING_CLOUD_VERSION
// spring cloud 依赖
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-eureka', version: SPRING_CLOUD_VERSION
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${SPRING_CLOUD_DEPENDENCIES}"
}
}
更换内容:
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-eureka-server', version: SPRING_CLOUD_VERSION
替换为:
compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-eureka', version: SPRING_CLOUD_VERSION
添加依赖:
compile group: "org.springframework.boot", name: "spring-boot-starter-web", version: SPRING_BOOT_VERSION
启动类中的注解 @EnableEurekaServer 更换为 @EnableEurekaClient,CloudClientApp 内容如下:
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.builder.SpringApplicationBuilder
import org.springframework.boot.runApplication
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer
import org.springframework.cloud.netflix.eureka.EnableEurekaClient
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.ResponseBody
import org.springframework.web.bind.annotation.RestController
import java.util.*
/**
* 配置用用
*
* @author wx
*/
@RestController
@EnableEurekaClient
@SpringBootApplication
open class CloudClientApp : SpringBootServletInitializer() {
/**
* 配置应用
*
* @param builder 应用builder对象
* @return 配置好的应用builder对象
*/
override fun configure(builder: SpringApplicationBuilder): SpringApplicationBuilder {
return builder.sources(CloudClientApp::class.java)
}
/**
* 假如这个客户端要提供一个getUser的方法
* @return
*/
@GetMapping(value = ["/getUserInfo"])
@ResponseBody
open fun getUser(@RequestParam id: Int): Map<String, Any>? {
val data: MutableMap<String, Any> = HashMap()
data["id"] = id
data["userName"] = "admin"
data["from"] = "client-B" //改这里是为了让大家更能理解它负载均衡的机制
return data
}
} // Class CloudEurekaApp
/**
* Web应用入口
*
* @param args 命令行参数
*/
fun main(args: Array<String>) {
runApplication<CloudClientApp>(*args)
return
}
yml 文件内容:
eureka:
client:
serviceUrl: #注册中心的注册地址
defaultZone: http://127.0.0.1:8761/eureka/
server:
port: 8083 #服务端口号
spring:
application:
name: service-clinet #服务名称--调用的时候根据名称来调用该服务的方法
yml-dev内容:
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:${server.port}/eureka/
#开发环境
server:
port: 8761
注意 yml 文件中,在服务器中注册名称 service-clinet
Eureka 启动请看上一篇文章,这里直接运行客户端进行连接,客户端中直接写了一个接口可以请求调试。
把客户端在复制出来一份,运行两个客户端,注意修改端口号,在 Eureka 中的注册名称要一下,客户端不做详细介绍了运行结果如下:
可以看到有两个客户端,相同的名称注册了,这时候我们还需要开启一个项目 调用 getUserInfo 接口,来测试是否达到了负载均衡 ,新建一个项目,搭建一个接口,这里省略了,直接看启动类中的接口。
/**
* 配置用用
*
* @author wx
*/
@RestController
@EnableEurekaClient
@SpringBootApplication
open class CloudClientApp : SpringBootServletInitializer() {
/**
* 配置应用
*
* @param builder 应用builder对象
* @return 配置好的应用builder对象
*/
override fun configure(builder: SpringApplicationBuilder): SpringApplicationBuilder {
return builder.sources(CloudClientApp::class.java)
}
@LoadBalanced
@Autowired
var restTemplate: RestTemplate? = null
/**
* 实例化RestTemplate
* @return
*/
@LoadBalanced
@Bean
open fun rest(): RestTemplate? {
return RestTemplate()
}
/**
* Rest服务端使用RestTemplate发起http请求,然后得到数据返回给前端----
* @param id
* @return
*/
@GetMapping(value = ["/gotoUserInfo"])
@ResponseBody
open fun getUserInfo(@RequestParam id: Int):Map<*,*>?{
try {
val data = restTemplate!!.getForObject(
"http://SERVICE-CLINET/getUser?id=$id",
Map::class.java
)
return data
} catch (e: Exception) {
e.printStackTrace()
}
return null
}
} // Class CloudEurekaApp
/**
* Web应用入口
*
* @param args 命令行参数
*/
fun main(args: Array<String1>) {
runApplication<CloudClientApp>(*args)
return
}
调用接口调试:
反复的刷新可以看到,两个服务器交替被调用。
注意:
这里一定要和 Eureka 上的名字一直,要大写。可能说的不是很清楚,请多多指教