简介:通常来说服务注册与发现包括两部分,一个是服务器端,另一个是客户端。Server是一个公共服务,为Client提供服务注册与发现的功能,维护注册到自身的Client的相关消息,同时提供接口给Client获取注册表中其他服务的消息,使得动态变化得Client能够进行服务间得相互调用。Client将自己得服务信息通过一定得方式登记到server上,并在正常范围内维护自己信息一致性,方便其他服务发现自己,同时可以通过Server获取到自己依赖得其他服务信息,完成服务调用。
1.设置域名
在host文件设置服务端跟客户端域名
# 本地测试环境
127.0.0.1 standalone
127.0.0.1 client
2.搭建Eureka 服务注册中心
在idea创建SpringBoot 项目,主要依赖如下:
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</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>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
在启动类中添加注释@EnableEurekaServer,代码如下所示:
//会为项目自动配置必须得配置类,标识该服务为注册中心
@EnableEurekaServer
@SpringBootApplication
public class EurakeServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurakeServerApplication.class, args);
}
}
在application.yml 配置文件中添加一下配置,配置注册中心得端口和标识:
server:
port: 8761
eureka:
instance:
hostname: standalone
instance-id: ${spring.application.name}:${vcap.application.instance-id:${spring.application.instance-id:${random.value}}}
client:
register-with-eureka: false #表明该服务不会向Eureka Server注册自己得信息
fetch-registry: false #表明该服务不会向Eureka Server获取注册信息
service-url: #Eureka Server注册中心得地址,用于Client与Server交流
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
application:
name: eureka-service
3.搭建Euraka服务提供者
在idea创建SpringBoot 项目,主要依赖如下:
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<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>
<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>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
启动类如下:
@SpringBootApplication
public class EurakeClient01Application {
public static void main(String[] args) {
SpringApplication.run(EurakeClient01Application.class, args);
}
}
application.yml 配置如下:
server:
port: 8762
eureka:
instance:
hostname: client
instance-id: ${spring.application.name}:${vcap.application.instance-id:${spring.application.instance-id:${random.value}}}
client:
service-url:
defaultZone: http://localhost:8761/eureka/ #服务注册中心访问地址
spring:
application:
name: eureka-client-service
新建一个controller包,添加一个提供服务的接口,代码如下:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
/*
* @Author chencundeng
* @Description //TODO
* @Date 2019/2/21
**/
@RestController
public class SayHelloController {
@GetMapping(value="/sn/hello/{name}")
public String sayHello(@PathVariable("name") String name){
return "Hello,".concat(name).concat("!");
}
}
4.搭建Euraka服务调用者
在idea创建SpringBoot 项目,主要依赖如下:
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<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>
<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>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
application.yml 配置如下:
server:
port: 8763
eureka:
instance:
hostname: client
instance-id: ${spring.application.name}:${vcap.application.instance-id:${spring.application.instance-id:${random.value}}}
client:
service-url:
defaultZone: http://localhost:8761/eureka/ #服务注册中心地址
spring:
application:
name: eureka-client
新建一个controller包,添加一个AskController 向eureka-client-service(服务提供者)请求sayHello的服务,代码如下:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/*
* @Author chencundeng
* @Description //TODO
* @Date 2019/2/21
**/
@RestController
@Configuration
public class AskController {
//注入本地服务名
@Value("${spring.application.name}")
private String name;
@Autowired
RestTemplate restTemplate;
@GetMapping("/sn/ask")
public String ask(){
String askHelloService = restTemplate.getForEntity("http://EUREKA-" +
"CLIENT-SERVICE/sn/hello/{name}",String.class,name).getBody();
return askHelloService;
}
//注入一个可以进行负载均衡的RestTemplate用于服务间调用
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
5.Eureka 服务注册与发现
搭建好上述三个Eureka应用后,依次启动三个应用。
(1)访问Eureka Server 的主页 http://127.0.0.1:8761/,可以看到所示界面:
(2)访问http://127.0.0.1:8763/sn/ask,eureka-client将调用eureka-client-service的sayHello服务响应结果如下:
Hello,eureka-client!
6.例子源码地址如下:
https://gitee.com/mingzhishuyuan/spring-cloud.git