本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 java
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
复制代码
package spring.cloud.demoo.eurekaclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
复制代码
@EnableEurekaClient 向eureka服务中心注册服务(推荐使用),若是是其余服务注册中心,例如:consul、zookeeper,建议使用@EnableDiscoveryClientgit
spring:
application:
name: eureka-client
server:
port: 8801
eureka:
instance:
hostname: localhost
# 表示eureka client间隔多久去拉取服务注册信息,默认为30秒,若是要迅速获取服务注册状态,能够缩小该值
lease-renewal-interval-in-seconds: 5
# 表示eureka server至上一次收到client的心跳以后,等待下一次心跳的超时时间,在这个时间内若没收到下一次心跳,则将移除该instance。
# 默认为90秒
# 若是该值太大,则极可能将流量转发过去的时候,该instance已经不存活了。
# 若是该值设置过小了,则instance则极可能由于临时的网络抖动而被摘除掉。
# 该值至少应该大于 leaseRenewalIntervalInSeconds
lease-expiration-duration-in-seconds: 10
client:
service-url:
defaultZone: http://localhost:8701/eureka/
复制代码
配置中http://localhost:8701/eureka/ 调用eureka-server,能够参考: eureka-server注册中心搭建github
package spring.cloud.demoo.eurekaclient.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
/** * @auther: maomao * @DateT: 2019-09-17 */
@RestController
public class EurekaClientController {
@Value("${server.port}")
private String port;
@RequestMapping("/info")
public String syaHello(HttpServletRequest request) {
String message = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getServletPath();
return message;
}
}
复制代码
访问http://localhost:8801/info,并返回结果,以下图所示。 web
这时在访问服务注册中心,若是下入所示:spring
至此,一个简单的单机eureka client服务提供者就搭建完成。服务的提供者提供一个Restful服务。bash
127.0.0.1 eureka1.client.com
127.0.0.1 eureka2.client.com
127.0.0.1 eureka3.client.com
复制代码
增长application-clinet1.yml和application-clinet2.yml文件,修改原application.yml文件。网络
spring:
application:
name: eureka-client
server:
port: 8801
eureka:
instance:
hostname: eureka1.client.com
lease-renewal-interval-in-seconds: 5
lease-expiration-duration-in-seconds: 10
client:
service-url:
defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
复制代码
spring:
application:
name: eureka-client
server:
port: 8802
eureka:
instance:
hostname: eureka2.client.com
lease-renewal-interval-in-seconds: 5
lease-expiration-duration-in-seconds: 10
client:
service-url:
defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
复制代码
spring:
application:
name: eureka-client
server:
port: 8803
eureka:
instance:
hostname: eureka3.client.com
lease-renewal-interval-in-seconds: 5
lease-expiration-duration-in-seconds: 10
client:
service-url:
defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
复制代码
分别访问http://eureka1.client.com:8801/info、eureka2.client.com:8802/info、 eureka3.client.com:8803/info 显示结果以下:app
截图中红框中表明三个client已经注册成功。分布式
本文简单实现了服务提供者单机和集群的搭建,后续继续更新其余关于spring cloud其余内容。spring-boot
转载请注明出处,