一、注册中心server服务器
1.eureka-server中需要的pom文件
<spring-cloud.version>Finchley.RC1</spring-cloud.version> <!--重点-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<!--相比2.0之前有改动-->
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!--使用spring cloud 需要引入的依赖管理-->
<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.配置文件
eureka.instance.hostname=127.0.0.1
eureka.client.registerWithEureka=false<!-- 是否将自己的服务注册到eureka服务器-->
eureka.client.fetchRegistry=false<!--是否从eureka服务器获取注册信息-->
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
3.在启动类中添加注解
@EnableEurekaServer
二、注册服务client客户端
1.eureka-client客户端pom文件
<spring-cloud.version>Finchley.RC1</spring-cloud.version>
<dependency>
<groupId>org.springframework.cloud</groupId>
<!-- 对比2.0.0之前不同-->
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<!--对比2.0.0之前不同 -->
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<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.配置文件
由于是使用yml格式的配置,所以要特别注意格式。
eureka:
client:
serviceUrl:
defaultZone: http://127.0.0.1:8761/eureka/
3.启动类中添加注解
@EnableEurekaClient
三、eureka服务器上服务之间数据的请求
1.这是eureka服务器中的两个服务,现在要实现他们之间数据的交互。
2.在hello中来提供服务,在test01中使用服务
a.先是hello中的提供类的创建
@EnableEurekaClient // 一定加上
@RestController
@RequestMapping("/rest/outer")
public class StduentFeign {
@Autowired
private TestService people;
@GetMapping("/findAll")
public List<Student> findAll() {
return people.findAll();
}
}
b.然后是test01中服务的使用,这里使用接口接可以了。
@FeignClient(name="hello",fallback = StudentFeignImpl.class)
public interface StudentFeign {
@GetMapping("/rest/outer/findAll")
public List<Student> findAll();
}
@Component // 加上注解
public class StudentFeignImpl implements StudentFeign {
@Override
public List<Student> findAll() {
List<Student> students = new ArrayList<>();
Student student = new Student();
student.setClassGrade("1");
student.setId(2);
student.setName("32");
student.setSetting("221");
students.add(student);
return students;
}
}
在启动类中添加@EnableFeignClients,然后服务之间的交互方能成功。
其中fallback的回调类中要起作用,必须在配置中添加
feign:
hystrix:
enabled: true