• eureka 原理• 搭建 EurekaServer• 服务注册• 服务发现
eureka原理
在Eureka架构中,微服务角色有两类:
一:EurekaServer:服务端,注册中心
搭建EurekaServer
搭建EurekaServer分三步:
1.创建项目,引入spring-cloud-starter-netflix-eureka-server的依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
2.编写启动类,添加@EnableEurekaServer注解。
启动类:
package cn.itcase.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args){
SpringApplication.run(EurekaApplication.class,args);
}
}
3.创建application.yml文件,编写如下的配置:
server:
port: 10010 # 服务端口
spring:
application:
name: eurekaserver # eureka的服务名称
eureka:
client:
service-url: # eureka的地址信息
defaultZone: http://127.0.0.1:10010/eureka
这样我们就搭建好了EurekaServer,我们启动就可以看到eureka吧自己注册了上去,搭建成功了。
服务注册
将模块user-service服务注册到EurekaServer步骤分两步,如下:
1.在user-service项目的pom中引入spring-cloud-starter-netflix-eureka-client客户端的依赖。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2.在application.yml文件中配置:
spring:
application:
name: userservice # eureka的服务名称
eureka:
client:
service-url: # eureka的地址信息
defaultZone: http://127.0.0.1:10010/eureka
根据如上将其他模块orderserver也进行注册。注册完成后启动可以看到已经注册成功了。
服务发现
在模块orderserver中完成服务拉取userservice;服务拉取是基于服务名称获取服务列表,然后再对服务列表做负载均衡
1.修改OrderService的代码,修改访问的url路径,用服务名代替ip、端口改成如下:
package cn.itcast.order.service;
import cn.itcast.order.mapper.OrderMapper;
import cn.itcast.order.pojo.Order;
import cn.itcast.order.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class OrderService {
@Autowired
private OrderMapper orderMapper;
@Autowired
private RestTemplate restTemplate;
public Order queryOrderById(Long orderId) {
// 1.查询订单
Order order = orderMapper.findById(orderId);
// 2.利用RestTemplate发起http请求,查询用户
// 2.1 url路径
String url = "http://userservice/user/" + order.getUserId();
// 2.2 发送http请求,实现远程调用
User user = restTemplate.getForObject(url, User.class);
// 3.封装user到Order
order.setUser(user);
// 4.返回
return order;
}
}
2.在order-service项目的启动类OrderApplication中的RestTemplate添加负载均衡的注解:
package cn.itcast.order;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@MapperScan("cn.itcast.order.mapper")
@SpringBootApplication
public class OrderServiceApplication {
public static void main(String[] args) {
SpringApplication.run(OrderServiceApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}