在微服务架构中,业务都会被拆分成一个独立的服务。Spring cloud有两种服务调用方式,一种是ribbon+restTemplate,另一种是feign
搭建服务注册中心可以参考:https://www.cnblogs.com/wanghy898/p/11167301.html
启动配置中心服务:https://www.cnblogs.com/wanghy898/p/11167465.html
新建一个springboot项目sell-product并在pom.xml文件中引入核心依赖:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId></dependency><dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId></dependency>
bootstrap.yml文件中添加配置信息:
server: port: 8083 #以下是从统一配置中心获取 spring: application: name: product # 由于配置了公共配置 所以直接从公共配置中心拉取即可 cloud: config: discovery: enabled: true service-id: SELL-CONFIG #代表引用公共配置服务 profile: dev #此处生产环境也可以不用填写
在启动类中加入依赖:
@SpringCloudApplication //替代@SpringBootApplication @EnableDiscoveryClient @MapperScan("wanghy.com.cn.mapper") //扫描mappper public class SellProductApplication { public static void main(String[] args) { SpringApplication.run(SellProductApplication.class, args); } }