我们可以通过Sping Boot快速搭建一个服务提供者,再将其作为Eureka Client注册到Eureka Server,对外提供服务。
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
- 在resources路径下创建application.yml,添加Eureka Client相关的配置。
server:
port: 8010
spring:
application:
name: provider
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
属性在上一篇博文中有过解释,这里就不赘述了。- 在Java路径下添加启动类ProviderApplication
@SpringBootApplication
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class,args);
}
}
- 依次启动注册中心,ProviderApplication,启动成功控制台输出一下信息
- 打开浏览器,访问http://localhost:8761/,若成功则应看到以下界面
此时服务提供者Provider已经成功在Eureka Server中注册了。注:这里将Eureka Server自身也作为一个Client注册在案。
- 现在provider作为服务提供者还没有可供外部调用的方法,可以简单加上一个例子。在java路径下新增一个ProviderController
@RestController
@RequestMapping("/provider")
public class ProviderController {
@RequestMapping("/hello")
public String hello() {
String hello = "this is my first Spring Cloud Client";
return hello;
}
}
- 重新启动provider,在浏览器中访问http://localhost:8010/provider/hello,可得到以下响应
provider可以正常向外提供服务,如此一个简单的服务提供者便完成了。
当然了,实际开发中肯定是需要结合数据库操作的。以我使用的Mybatis为例,在以往的Spring与Mybatis进行整合需要大量的配置,需要配置数据源、指定Mybatis Mapper文件的位置,指定Mybatis配置文件、扫描Mybatis Mapper接口等操作,而这些在Spring Boot中则完全不需要,Spring Boot将自动完成Mybatis的集成配置,我们只需要在application文件中添加个性化配置就可实现Mybatis开箱即用。
- 在provider的pom.xml中添加spring Boot整合Mybatis与数据库连接依赖。
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
- 创建数据表。
create table actor(
id int primary key auto_increment,
name varchar(11),
gender varchar(5) ,
birthday date
);
- 先向表中添加三条数据。
- 接下来的操作大家应该很熟悉了,创建表对应的实体类。
public class Actor {
private Long id;
private String name ;
private String gender;
private Date birthday;
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 getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
} - 创建ActorMapper,定义基本的CRUD接口,这里图简单,之定义三个简单的接口。
@Repository
public interface ActorMapper {
public List<Actor> findAll();
public int save(Actor actor);
public int deleteById(Long id);
}
<mapper namespace="com.leslie.provider.dao.ActorMapper">
<resultMap type="com.leslie.provider.entity.Actor" id="Actor">
<id column="ID" property="id" javaType="java.lang.Long" jdbcType="DOUBLE"/>
<result column="NAME" property="name" javaType="java.lang.String" jdbcType="VARCHAR"/>
<result column="GENDER" property="gender" javaType="java.lang.String" jdbcType="VARCHAR"/>
<result column="BIRTHDAY" property="birthday" javaType="java.util.Date" jdbcType="DATE"/>
</resultMap>
<select id="findAll" resultMap="Actor">
select * from ACTOR
</select>
<insert id="save" parameterType="com.leslie.provider.entity.Actor">
insert into ACTOR(NAME,GENDER,BIRTHDAY) values(#{name},#{gender},#{birthday})
</insert>
<delete id="deleteById" parameterType="java.lang.Long">
delete from ACTOR where ID = #{id}
</delete>- 创建ActorController,注入ActorMapper。
@RestController
@RequestMapping("/actor")
public class ActorController {
@Autowired
private ActorMapper actorMapper;
@GetMapping("/findAll")
public List<Actor> findAll(){
return actorMapper.findAll();
}
@PostMapping("/save")
public int save(@RequestBody Actor actor){
return actorMapper.save(actor);
}
@DeleteMapping("/deleteById/{id}")
public int deleteById(@PathVariable("id") Long id){
return actorMapper.deleteById(id);
}
}- 修改配置文件application.yml,添加MySQL数据源信息及MyBatis的相关配置。
server:
port: 8010
spring:
application:
name: provider
datasource:
url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT
username: root
password: 123456
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
mybatis:
mapper-locations: classpath:/sqlMapper/*.xml
type-aliases-package: com.leslie.provider.entity- 修改启动类Application,添加一个类注解@MapperScan("com.leslie.provider.dao")。
@SpringBootApplication
@MapperScan("com.leslie.provider.dao")
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class,args);
}
}由于Mybatis并非Spring生态下的模块,Spring不会自动管理Mybatis相关对象的生命周期,因此需要手动配置,将Mybatis 相关对象交给Spring同期来管理,@MapperScan 注解的作用就是指定 MyBatis Mapper 接口所在的包,将目标包下的类全部扫描到 Spring 容器中。- 重新启动provider,使用Postman工具测试接口。
- findAll
- save
添加成功,再次调用findAll查看
- deleteById
以上就是创建服务提供者的具体操作,另外演示了本应属于Spring Boot内容的Spring Boot整合Mybatis的过程











No comments:
Post a Comment