服务消费者-------> Eureka服务器实例1 <----------- 服务提供方实例
1、使用springboot项目生成工具生成一个应用框架;在pom文件中添加eureka-server相关依赖
<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>
2、配置application.properties配置文件,添加如下配置;
spring.application.name=discovery-server eureka.client.register-with-eureka=false eureka.client.fetch-registry=false server.port=8761
3、使用应用入口类上添加@EnableEurekaServer注解
pom中添加eureka-client依赖包
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
应用入口使用@EnableDiscoveryClient注解
application.properties配置文件内容
spring.application.name=time-service eureka.client.service-url.defaultZone=http://localhost:8761/eureka eureka.instance.instance-id=${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
pom中添加eureka-client依赖包
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
application.properties配置文件内容
spring.application.name=time-client eureka.client.service-url.defaultZone=http://localhost:8761/eureka eureka.client.register-with-eureka=false
应用入口使用@EnableDiscoveryClient注解并且
@Autowired private RestTemplate restTemplate;
@Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); }
案例中使用RestTemplate工具,通过发送http请求请求响应;http请求格式为http://[服务提供方应用名]/某个具体方法名