说在前面
之前的文章我们已经详细的讲解了微服务的概念,以及分析了Dubbo与SpringCloud这两个SOA框架的区别。传送门1(SpringCloud(1):说说什么是微服务):https://www.jianshu.com/p/183871ec8add 传送门2:(SpringCloud(2):注册中心Eureka的搭建):https://www.jianshu.com/p/79b1000e068f
那么概念了解了,注册中心也已经搭建好了,我们来搭建服务生产者吧。
服务生产者
核心坐标
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
Maven坐标
<parent>
<groupId>cn.****</groupId>
<artifactId>***</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>cn.lpck</groupId>
<artifactId>commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- alibaba的druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.0</version>
</dependency>
</dependencies>
配置文件 application.properties
spring.application.name
:服务提供者的名字,可以随便起后面会用到
eureka.client.service-url.defaultZone
:注册中心地址
management.endpoints.web.exposure.include
:制定放开的handler,由于Springboot2.0默认关闭了自带的一些handler不开放我们就无法访问,开放之后我们可以用监控中心进行健康以及心跳的检测
server.port
:容器端口
...
:一些整合配置,mybatis mysql redis.....
spring.application.name=eureka-producer
#使用eureka集群注册中心
#eureka.client.service-url.defaultZone=http://localhost:10001/eureka/,http://localhost:10002/eureka/
eureka.client.service-url.defaultZone=http://localhost:10000/eureka/
management.endpoints.web.exposure.include=refresh,health,info,env,loggers,metrics,trace,dump
management.endpoint.health.show-details=always
# 9000
server.port=9000
spring.datasource.name=myboot
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.filters=stat
spring.datasource.druid.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.druid.url=jdbc:mysql://10.*.**.**:3306/db_***?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
#spring.datasource.druid.url=jdbc:mysql://localhost:3306/db_bxgg?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
spring.datasource.druid.username=root
spring.datasource.druid.password=root
spring.datasource.druid.initial-size=1
spring.datasource.druid.min-idle=1
spring.datasource.druid.max-active=204
spring.datasource.druid.max-wait=60000
spring.datasource.druid.time-between-eviction-runs-millis=60000
spring.datasource.druid.min-evictable-idle-time-millis=300000
spring.datasource.druid.validation-query=SELECT 'x'
spring.datasource.druid.test-while-idle=true
spring.datasource.druid.test-on-borrow=false
spring.datasource.druid.test-on-return=false
spring.datasource.druid.pool-prepared-statements=false
spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20
mybatis.mapper-locations=classpath:Mapper/*.xml
mybatis.type-aliases-package=**.****.****
spring.mvc.static-path-pattern=/static/**
pagehelper.helper-dialect=mysql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true
pagehelper.params=count=countSql
##redis
#数据库索引默认为0
spring.redis.database=0
spring.redis.host=10.9.**.****
#spring.redis.host=localhost
spring.redis.port=6379
#数值为负数表示没有限制
spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.max-wait=-1
spring.redis.lettuce.pool.max-idle=8
spring.redis.jedis.pool.min-idle=0
spring.redis.timeout=5000
启动类
由于我们用的SpringBoot2.0版本,已经不需要在启动类上加任何别的注解
package cn.**.eurekaproducer;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import java.util.ArrayList;
import java.util.List;
@SpringBootApplication
@MapperScan("**.**.eurekaproducer.mapper")
public class EurekaProducerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaProducerApplication.class, args);
}
/**
* 配置fastjson
* @return
*/
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
// 1、需要先定义一个 convert 转换消息的对象;
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据;
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//同样要处理中文乱码问题
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastConverter.setSupportedMediaTypes(fastMediaTypes);
//3、在convert中添加配置信息.
fastConverter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter<?> converter = fastConverter;
return new HttpMessageConverters(converter);
}
}
到这里我们的服务提供者已经搭建好了,我们可以启动之前搭建好的注册中心,然后再启动服务提供者。
服务注册
我们看到服务服务在注册中心已经被发现了。
Contorller
框架以及搭建好了那么我们最关注的还是在实际生产环境中如何来使用,我们就来编写这样一个Controller
查询数据库中所有的机构:
@RestController
@RequestMapping("/institutions")
public class InstitutionsController {
@Autowired
private InstitutionsService institutionsService;
@RequestMapping("/all")
public List<Institutions> getInstitutionsList(){
List<Institutions> institutions = institutionsService.queryInstitutions();
return institutions;
}
.....
}
访问一下试试看:
机构json请求到数据就说明是没有问题的,到此我们的服务提供方也就配置完成了。
网友评论