一、需求:idea springboot+mybatis 并创建三个项目 :注册中心、服务方、消费方
1、首先 创建注册中心 ,并在 pom.xml添加依赖(此项目无业务逻辑、页面显示,可以当成一个中转站)
<!--spring cloud center pom-->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
<!--spring cloud center management plugin-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2、application.properties中添加配置
#服务注册中心
spring.application.name=server-register-center
server.port=1111
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
3、注册中心 接口处添加注释@EnableEurekaServer
@SpringBootApplication
@EnableEurekaServer
public class ServiceRegisterApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceRegisterApplication.class, args);
}
}
二、1、创建 服务方,并在pom.xml中添加依赖(此项目有业务逻辑,无界面显示)
<!--服务提供方的依赖包-->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2、application.properties中添加配置
#spring cloud配置
spring.application.name=simple-service-provider
server.port=2222
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
3、服务方接口处加入注解@EnableDiscoveryClient
@SpringBootApplication
@EnableDiscoveryClient
public class SimpleServerProviderApplication {
public static void main(String[] args) {
SpringApplication.run(SimpleServerProviderApplication.class, args);
}
}
4、然后就是服务方逻辑的实现 (服务方只需提供接口,以便消费方调用)
@RestController
public class ComputerController {
@RequestMapping("/plus/{a}/{b}")
public Integer plus(@PathVariable Integer a, @PathVariable Integer b){
return a+b;
}
}
三、1、创建消费方 ,并在pom.xml中添加依赖 (和服务方的依赖相同)
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2、添加配置
spring.application.name=simple-service-consumer
server.port=3333
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
3、消费方接口处添加注释@EnableDiscoveryClient,以及实例化 RestTemplate
@SpringBootApplication
@EnableDiscoveryClient
public class SimpleServiceConsumerApplication {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(SimpleServiceConsumerApplication.class, args);
}
}
4、消费方的Controller需要写一些代码,以便调用服务方准备好的接口( 传值推荐采用restful风格)
@RestController
public class MyConsumerController {
@Autowired
private LoadBalancerClient lbc;
@Autowired
private RestTemplate template;
@RequestMapping("/comsumer/{a}/{b}")
public String computePlus(@PathVariable Integer a, @PathVariable Integer b){
//发现服务
ServiceInstance choose = lbc.choose("simple-service-provider");
String url = "http://" + choose.getHost() + ":" + choose.getPort() + "/plus/"+a+"/"+b;
Integer result = template.getForObject(url, Integer.class);
return a+"+"+b+"="+result;
}
5、然后三个项目同时启动,SpringCloub项目搭建成功