- 注册服务中心
new–>project–>spring Initializr—>(next)…—>Dependencies(Cloud Discovery Eureka Server)
在入口类处加注解@EnableEurekaServer表明该服务器是一个服务注册中心
@SpringBootApplication
//服务注册中心
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
配置application.yml
#服务器端口号
server:
port: 8761
#向服务中心注册
eureka:
client:
service-url:
defaultZone: http://localhost:8761
#eureka.client.register-with-eureka:false(是否向自身注册),eureka.client.fetch-register:false表明该服务器是个eureka server
register-with-eureka: false
fetch-registry: false
spring:
application:
name: eureka
- 服务提供者
(创建过程同上)
在入口类加入注解@EnableDiscoveryClient,@EnableEurekaClient表明这是一个eureka client
@SpringBootApplication
@EnableDiscoveryClient
@EnableEurekaClient
//注意:如果单独创建controller类,要放在该入口类的子包下,否则扫描不到
@RestController
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
@Value("${server.port}")
String port;
@RequestMapping("/hi")
public String home(@RequestParam(value = "name", defaultValue = "forezp") String name) {
return "来自"+port+"的"+name+"人类,欢迎来到我们的时间";
}
}
server:
port: 8762
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
instance:
hostname: clientName
spring:
application:
name: eureka-client
在pom.xml中加jar包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
配置EurekaClientApplication下的小三角里的Edit Configurations–>single instance only(去掉对勾)
,改application.yml里的端口号,改为8763,启动
- 服务消费者(负载均衡)
restTemplate+ribbon实现负载均衡
在pom.xml配置加入以下配置
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--ribbon:负载均衡客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
@SpringBootApplication
@EnableEurekaClient
//向服务中心注册
@EnableDiscoveryClient
@RestController
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
//向程序的ioc注入一个bean
@Bean
//表明这个restTemplated开启负载均衡
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
}
创建Service类
@Service
public class HelloService {
@Autowired
private RestTemplate restTemplate;
public String hiService(String name){
return restTemplate.getForObject("http://EUREKA-CLIENT/hi?name="+name,String.class);
}
}
创建Controller类
@RestController
public class HelloController {
@Autowired
private HelloService helloService;
@GetMapping(value = "/hi")
public String hi(@RequestParam String name){
return helloService.hiService(name);
}
}
分别开启服务注册中心,服务提供者(8762,8763),服务消费者,访问http://localhost:8764/hi?name=zy,不断刷新,就会发现页面交替出现,
来自8762的zy人类,欢迎来到我们的时间
来自8763的zy人类,欢迎来到我们的时间
说明已经达到了负载均衡
feign+rest实现负载均衡
在pom.xml加入如下配置
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
在入口类加入注解@EnableFeignClients
//通过@FeignClient来指定调用哪个服务
@FeignClient(value = "eureka-client")
public interface SchedualServiceHi {
@RequestMapping(value = "/hi",method = RequestMethod.GET)
String sayHiFromClientOne(@RequestParam(value = "name")String name);
}
@RestController
public class HiController {
@Autowired
private SchedualServiceHi schedualServiceHi;
@RequestMapping(value = "/hi")
public String sayHi(@RequestParam(value = "name")String name){
return schedualServiceHi.sayHiFromClientOne(name);
}
}
- 断路器
为什么要使用断路器?因为服务与服务之间可以互相调用,如果一个服务宕机,很有可能导致线程阻塞,线程资源被消耗完毕,从而导致雪崩。