1. eureka是做什么的?
Eureka就好比是滴滴,负责管理、记录服务提供者的信息。服务调用者无需自己寻找服务,而是把自己的需求告诉Eureka,然后Eureka会把符合你需求的服务告诉你。
同时,服务提供方与Eureka之间通过“心跳”
机制进行监控,当某个服务提供方出现问题,Eureka自然会把它从服务列表中剔除。
这就实现了服务的自动注册、发现、状态监控。
2.eureka原理图
3.编写eureka案例
(1)、eureka-server(注册中心服务)的编写;
- pom文件中导入
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
-(eureka-server)yml配置文件
server:
port: 10086
#将10086服务提供给10087的注册中心
#eureka可以既是服务提供者也可以是调用者
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10087/eureka
spring:
application:
name: eureka-server
- (eureka-server2)yml配置文件
server:
port: 10087
#将10087服务提供给10086的注册中心
#eureka可以既是服务提供者也可以是调用者
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
spring:
application:
name: eureka-server
- 启动类
@EnableEurekaServer //重点
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class);
}
}
(2)、(user-service)服务提供方的编写;
- pom文件导包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- yml配置文件
server:
port: 8081
spring:
datasource:
url: jdbc:mysql://localhost:3306/yun6
username: root
password: 123456
application:
name: user-service
mybatis:
type-aliases-package: cn.itcast.user.pojo
#将服务提供给两个Eureka(注册中心)
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka,http://127.0.0.1:10087/eureka
#指定eureka中的ip地址
instance:
prefer-ip-address: true
ip-address: 127.0.0.1
- 启动类
@EnableDiscoveryClient //跨客户端访问的方式
@SpringBootApplication
@MapperScan("cn.itcast.user.mapper")
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class);
}
}
- 实体类
@Table(name = "tb_user")
@Data
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
// 用户名
private String userName;
// 密码
private String password;
// 姓名
private String name;
// 年龄
private Integer age;
// 性别,1男性,2女性
private Integer sex;
// 。。。省略getters和setters
}
- mapper
@org.apache.ibatis.annotations.Mapper
public interface UserMapper extends Mapper<User> {
}
- service
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User queryById(Long id) {
return this.userMapper.selectByPrimaryKey(id);
}
}
- controller
@RestController
@RequestMapping("user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/{id}")
public User queryById(@PathVariable("id") Long id) {
return this.userService.queryById(id);
}
}
(3)、服务调用方(consumer-service)的编写
- pom包导入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- yml配置文件
server:
port: 8082
spring:
application:
name: consumer-service
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka,http://127.0.0.1:10087/eureka
instance:
prefer-ip-address: true
ip-address: 127.0.0.1
- 启动类
@EnableDiscoveryClient
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class);
}
@Bean
public RestTemplate restTemplate() {
// 这次我们使用了OkHttp客户端,只需要注入工厂即可
return new RestTemplate();
}
}
- controller
@RestController
@RequestMapping("consumer")
public class ConsumerController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping("{id}")
public User queryUserById(@PathVariable("id") Long id){
List<ServiceInstance> instances = discoveryClient.getInstances("USER-SERVICE");
ServiceInstance instance = instances.get(0);
System.out.println("地址: " + instance.getHost());
System.out.println("端口: " + instance.getPort());
String url = "http://"+ instance.getHost() + ":" + instance.getPort() +"/user/" + id;
return this.restTemplate.getForObject(url, User.class);
}
}
(4)、服务启动结果