将spring boot服务注册到Eureka注册中心
工具:JDK 8MAVENSTS沿用之前配置的Eureka注册中心具体步骤如下:一 : 新建boot项目file ---> new Spring starter project点击Next之后选择可以自己选择要引入的包,这边选择Eureka和webpom:org.springframework.bootspring-boot-starter-weborg.springframe
·
工具:
JDK 8
MAVEN
STS
沿用之前配置的Eureka注册中心
具体步骤如下:
一 : 新建boot项目
file ---> new Spring starter project
点击Next之后选择可以自己选择要引入的包,这边选择Eureka和web
pom:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
二 : 在Application启动类上新增注解
package cn.sh.daniel;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient ##开启自动注册
public class CloudShopUserserviceApplication {
public static void main(String[] args) {
SpringApplication.run(CloudShopUserserviceApplication.class, args);
}
}
三 : 配置application.yml
server:
port: 8088
spring:
application:
name: cloud-shop-userservice
eureka:
client:
service-url:
defaultZone: http://daniel-cloud-shop-eureka:eureka123123@server1:9001/eureka/,http://daniel-cloud-shop-eureka:eureka123123@server2:9002/eureka/
四 : 新增一个rest接口对外提供服务
package cn.sh.daniel.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.sh.daniel.entity.User;
@RestController
@RequestMapping("/user")
public class UserController {
/**
* 根据ID查询用户信息
* 2018年1月18日
*/
@GetMapping("/{id}")
public User findUserById(@PathVariable("id")Long id) {
User user= new User();
user.setId(id);
user.setName("Spring Cloud User service");
user.setPassword("123123");
return user;
}
}
五 : 新增User实体
package cn.sh.daniel.entity;
public class User {
private Long id;
private String name;
private String password;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
六 : 启动服务,查看Eureka Server的面板,成功注册到服务上
七 : 访问cloud-shop-userservice
可以正常的提供服务,下面我们来实现Feign来调用服务
更多推荐
已为社区贡献4条内容
所有评论(0)