Spring Cloud Eureka 服务注册与发现中心(一)
Spring Cloud 项目中的子项目,Eureka 一个注重于AP的服务注册与发现服务,对比Zookeeper的集群,注重高可用与分区容错性。
如图:
![Image 1][]![Image 1][]
- Eureka-discovery 2个服务互为伙伴,增加高可用性;
- Business-Service 多个业务服务想discovery进行注册,互为伙伴的discovery 会自动同步节点下注册的服务;
- discovery-Client 像discovery注册的同时,获取注册服务列表,通过Ribbon进行自身软负载
相关代码:
1.总体父工程pom
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test.cloud</groupId>
<artifactId>test-cloud-parent</artifactId>
<version>1.0.RELEASE</version>
<packaging>pom</packaging>
<name>test-cloud-parent</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
2.Eureka-discovery相关POM代码
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test.cloud</groupId>
<artifactId>test-cloud-discovery</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>test-cloud-discovery</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>com.test.cloud</groupId>
<artifactId>test-cloud-parent</artifactId>
<version>1.0.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.hytera.test.cloud.App</mainClass>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
3.discovery的入口程序
package com.test.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* Hello world!
*
* @author Administrator
*/
@SpringBootApplication
@EnableEurekaServer
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}
4.discovery相关配置application.yml
server:
port: 9080
eureka:
client:
g-zip-content: true
service-url:
defaultZone: http://IP:${server.port}/eureka/
fetch-registry: false
register-with-eureka: false
server:
enable-self-preservation: false
eviction-interval-timer-in-ms: 1000
spring:
application:
name: Eureka-discovery-0
这里为注册中心的一个节点,如需要配置伙伴,defaultZone请配置对端IP及端口,
fetch-registry: true
5.business-server pom
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test.cloud</groupId>
<artifactId>test-cloud-server</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>test-cloud-server</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>com.test.cloud</groupId>
<artifactId>test-cloud-parent</artifactId>
<version>1.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.hytera.test.cloud.App</mainClass>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
6.business-server 配置
server:
port: 9070
eureka:
client:
service-url:
defaultZone: http://IP:${server.port}/eureka/
instance:
#instance-id: ${spring.application.name}:${server.port}
instance-id: ${spring.cloud.client.ipAddress}:${server.port}
prefer-ip-address: true
lease-expiration-duration-in-seconds: 15
lease-renewal-interval-in-seconds: 5
spring:
application:
name: Eureka-Server
7.business-server 入口类
package com.test.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* Hello world!
*
* @author Administrator
*/
@SpringBootApplication
@EnableDiscoveryClient
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}
8.business-server 服务生产类
package com.test.cloud.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
*
* @author Administrator
* @date 2017/11/23
*/
@RestController
public class MessageController {
private static final Logger LOGGER = LoggerFactory.getLogger(MessageController.class);
@Autowired
private DiscoveryClient client;
@RequestMapping("/getMsgInfo")
public String getMessageInfo(String strGuid, String strUserName){
String strMsg = strGuid + strUserName;
ServiceInstance instance = client.getLocalServiceInstance();
LOGGER.info("uri={},serviceId={},strMsg={}", instance.getUri(), instance.getServiceId(), strMsg);
return strMsg;
}
}
9.discovery-client pom
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test.cloud</groupId>
<artifactId>test-cloud-client</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>test-cloud-client</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>com.test.cloud</groupId>
<artifactId>test-cloud-parent</artifactId>
<version>1.0.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.hytera.test.cloud.App</mainClass>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
10.discovery-client 配置
server:
port: 9061
eureka:
client:
service-url:
defaultZone : http://192.168.72.30:9080/eureka/
instance:
prefer-ip-address: true
lease-expiration-duration-in-seconds: 15
lease-renewal-interval-in-seconds: 5
#instance-id: ${spring.application.name}:${server.port}
instance-id: ${spring.cloud.client.ipAddress}:${server.port}
spring:
application:
name: cloudclient
11.discovery-client 入口
package com.test.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* Hello world!
* @author Administrator
*/
@SpringBootApplication
@EnableDiscoveryClient
public class App {
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
12.discovery-client 服务消费类
package com.test.cloud;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
*
* @author Administrator
* @date 2017/11/22
*/
@RestController
public class ConsumerController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping(value = "/add", method = RequestMethod.GET)
public String add() {
return restTemplate.getForEntity("http://Eureka-Server/getMsgInfo?strGuid=10&strUserName=20", String.class).getBody();
}
}
全部代码完成。
访问discovery-client ,会使用自身软负载功能进行选择server。
[Image 1]:
还没有评论,来说两句吧...