目录
1.创建maven项目
2.idea打开maven项目
3.修改pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lizz</groupId>
<artifactId>eureka-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-consumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RC2</spring-cloud.version>
</properties>
<dependencies>
<!--web项目 默认使用内置tomcat-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--test 相关jar包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<!--spring cloud版本依赖-->
<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>
<build>
<!--打包生成的文件名及路径-->
<finalName>../eureka-consumer</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<!--maven远程仓库-->
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>
4.创建yml配置文件
在src/main/resources目录下创建application.yml文件(使用application.properties也可以)
spring:
application:
name: eureka-consumer #服务名
server:
port: 8602 #服务访问端口
servlet:
context-path: / #访问路径
eureka:
instance: #实例
prefer-ip-address: true #是否使用IP地址进行访问
#实例显示名
instanceId: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@
client:
serviceUrl:
#Eureka Server服务集群
defaultZone: http://192.168.180.15:9991/eureka/,http://192.168.180.15:9992/eureka/,http://192.168.180.15:9993/eureka/
5.创建消费服务
创建文件.../controller/ConsumerController.java
package com.lizz.eurekaconsumer.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* ConsumerController
*
* @author lizz
*/
@RestController
@RequestMapping(value = "/consumer/", produces = "application/json; charset=utf-8")
public class ConsumerController {
private static final Logger logger = LoggerFactory.getLogger(ConsumerController.class);
@Autowired
private RestTemplate restTemplate;
@RequestMapping("{name}/getHi")
public String getHi(@PathVariable String name) {
logger.info("************getHi {}",name);
String msg=restTemplate.getForEntity("http://provider/test/{name}/hi/",String.class,name).getBody();
logger.info("************getHi provider msg:{}",msg);
return msg;
}
}
6.开启Eureka消费者服务
在启动文件中增加@EnableDiscoveryClient或@EnableEurekaClient标签,执行main方法,启动程序。
EnableDiscoveryClient:Spring Cloud内置通用服务发现客户端
EnableEurekaClient:Eureka特定服务发现客户端
package com.lizz.eurekaconsumer;
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;
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaConsumerApplication {
/**
* 实例化RestTemplate,并加入负载均衡策略
* @return
*/
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(EurekaConsumerApplication.class, args);
}
}
7.查看Eureka控制台
注册成功,其他Eureka服务信息一致。