一、SpringCloud
1、注册中心eureka-server
配置文件
server.port=8761
spring.application.name=server
eureka.instance.hostname=server
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
#默认配置也是http://localhost:8761/eureka/
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
在SpringBoot主程序上添加@EnableEurekaServer开启服务,主程序运行后可以在浏览器http://localhost:8761查看注册的服务
2、提供者eureka-provider
配置文件
spring.application.name=provider
server.port=8081
eureka.instance.prefer-ip-address=true //使用ip地址来注册服务
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
TicketService
public interface TicketService {
public String getTicket();
}
TicketServiceImpl
@Service
public class TicketServiceImpl implements TicketService {
@Override
public String getTicket() {
return "《魔兽世界大电影》";
}
}
TicketController
@RestController
public class TicketController {
@Autowired
TicketService ticketService;
@GetMapping("/getTicket")
public String getTicket(){
return ticketService.getTicket();
}
}
启动主程序后将会把服务注册到自己配置的eureka注册中心
3、Eureka-Consumer
配置文件
server.port=8082
spring.application.name=consumer
eureka.instance.prefer-ip-address=true
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
SpringBoot主程序
@SpringBootApplication
@EnableDiscoveryClient //开启Eureka客户端发现服务
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
//主要将RestTemplate放入容器,可以在其他配置类
@Bean
@LoadBalanced //当使用服务注册中心的Application名时必须开启,否则会异常找不到服务
//如果使用服务时用得时ip地址+端口号的方式则不能使用负载均衡
RestTemplate restTemplate(){
return new RestTemplate();
}
}
Controller
@RestController
public class MyController {
@Autowired
RestTemplate restTemplate;
@GetMapping("/buyTicket")
public String buyTicket(String name){
//使用ip+port方式,RestTemplate就不能使用@LoadBalanced负载均衡
//String ticket = restTemplate.getForObject("http://localhost:8081/getTicket", String.class);
//PROVIDER为注册中心服务名,RestTemplate就必须使用@LoadBalanced负载均衡
String ticket = restTemplate.getForObject("http://PROVIDER/getTicket", String.class);
return name + "购买了" + ticket;
}
}
二、Dubbo
1、使用docker安装zookeeper,测试使用的是zookeeper 3.4.12和3.4.14
2、pom依赖,需要创建两个模块Provider和Consumer,都加入以下依赖(若版本问题报错可以看另一篇文章)
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.7</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
3、Provider
配置文件
server.port=8100
spring.application.name=provider
dubbo.scan.base-packages=com.crsbg.provider.service
dubbo.registry.address=zookeeper://zookeeper所在服务器ip地址:2182
dubbo.registry.timeout=10000 #默认为3000
ProServiceImpl
@Component
@DubboService
public class ProServiceImpl implements ProService {
@Override
public String getTicket() {
return "《魔兽世界大电影》";
}
}
4、Consumer
配置文件
server.port=8200
spring.application.name=consumer
dubbo.registry.address=zookeeper://zookeeper所在服务器ip地址:2182
dubbo.registry.timeout=10000
在Consumer中创建一个和provider中ProService相同的接口,全类名要一致
Cousumer自己的Service
@Service
public class ConService {
@DubboReference
ProService proService;
public String buyTicket(){
return proService.getTicket();
}
}
Controller
@RestController
public class ConController {
@Autowired
ConService conService;
@RequestMapping("/buyTicket")
public String buyTicket(String name){
return name + "购买了" + conService.buyTicket();
}
}