1.采用elipse 分布式创建项目
注:eureka-client 用来注册服务及提供者 ,eureka-server 用来创建
eureka服务中心, 这两个工程都继承父工程mh-cloud
2.mh-cloud 父工程的 pom.xml
<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.mh.cloud</groupId>
<artifactId>mh-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.5.RELEASE</version>
</parent>
<!-- 这里加入子工程 -->
<modules>
<module>eureka-server</module>
<module>eureka-client</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
注:父工程中pom.xml保存后 jar包会在子工程中! 子工程无需在配置pom.xml,除了单独需要的
3.eureka-server工程
3.1开始配置pom(需要加入eureka-server依赖)
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
3.2启动类
/**
* @author menghao
*生成eureka 服务中心类
*/
@SpringBootApplication
@EnableEurekaServer
public class ServerApp {
public static void main(String[] args) {
SpringApplication.run(ServerApp.class, args);
System.out.println( "Hello World!" );
}
}
3.3 配置application.yml
server:
port: 8080
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false #注:false为服务
fetch-registry: false
service-url:
defaultZone: http://localhost:8080/eureka/
security:
basic:
enabled: true
user:
name: admin #登陆用户名
password: admin#登陆密码
4.eureka
-client 工程
4.1启动类,提供服务
@SpringBootApplication
@EnableEurekaClient
@RestController
public class ClientApp {
public static void main(String[] args) {
SpringApplication.run(ClientApp.class, args);
System.out.println( "Hello World!" );
}
@Value("${server.port}")
String port;
@RequestMapping("/hi")
public String home(@RequestParam String name) {
return "hi "+name+",i am from port:" +port;
}
}
4.2application.yml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8080/eureka/ #注册中心
server:
port: 8081
spring:
application:
name: eureka-client #提供的服务名
5.启动服务 访问http://localhost:8080 查看注册中心 ,访问http://localhost:8081/hi?name=mh 调用提供的服务