eueka按逻辑上可以划分为3个模块,eureka-server,service-provider,service-consumer
eureka-server:服务端,提供服务注册和发现
eureka-client-service-provider:服务端,服务提供者,通过http rest告知服务端注册,更新,取消服务
eureka-client-service-consumer:客户端,服务消费者,通过http rest从服务端获取需要服务的地址列表,然后配合一些负载均衡策略(ribbon)来调用服务端服务。
构建服务中心
①新建Spring Boot工程,在pom.xml 中新增如下依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
②通过@EnableEurekaServer注解启动一个服务注册中心
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
③添加application.yml配置(属性文件配置也行)
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
2.2 高可用注册中心(集群)
在Eureka的服务治理设计中,所有的节点既是服务提供方,也是服务消费方,服务注册中心也不例外,Eurekad的高可用实际就是将自己作为服务向其他服务注册中心注册自己,这样形成一组互相注册的服务注册中心,以实现服务清单的互相同步,达到高可用的效果
例如:构建一个双节点的注册中心集群
application1.yml配置如下:
server:
port: 8761
eureka:
instance:
hostname: 10.22.0.130
client:
register-with-eureka: false
fetch-registry: false
serviceUrl:
defaultZone: http://10.22.0.131:8762/eureka/
application2.yml配置如下:
server:
port: 8762
eureka:
instance:
hostname: 10.22.0.131
client:
register-with-eureka: false
fetch-registry: false
serviceUrl:
defaultZone: http://10.22.0.130:8761/eureka/
3.服务注册
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
然后新建配置文件application.yml,配置如下:
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka
server:
port: 8763
spring:
application:
name: service-hello
新建启动类:HelloApplication.java
@RestController
@EnableEurekaClient
@SpringBootApplication
public class HelloApplication {
public static void main(String[] args) {
SpringApplication.run(HelloApplication.class, args);
}
@Value("${server.port}")
String port;
@RequestMapping("/hi")
public String sayHi(String name){
return "hi" + port + ";name"+name;
}
}
服务发现与消费
然后再构建一个eureka客户端,消费者两个服务
新建RibbonApplication.java
@EnableDiscoveryClient
@SpringBootApplication
public class RibbonApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
}
新建服务接口:HelloService.java
@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
public String hiService(String name){
return restTemplate.getForObject("http://SERVICE-HELLO/hi?name=" + name , String.class);
}
}
新建控制器:HelloControler.java
@RestController
public class HelloControler {
@Autowired
HelloService helloService;
@RequestMapping(value = "/hi")
public String hi(@RequestParam String name){
return helloService.hiService(name);
}
}
新增配置文件:application.yml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8764
spring:
application:
name: service-ribbon