1、Eureka注册中心搭建,选择Eureka Server
Eureka application.yml配置
# 服务注册中心(单节点)
server:
port: 8080
tomcat:
uri-encoding: UTF-8
servlet:
encoding:
charset: UTF-8
eureka:
instance:
hostname: localhost
client:
fetch-registry: false # 表示是否从Eureka Server获取注册信息,默认为true.因为这是一个单点的Eureka Server,不需要同步其他的Eureka Server节点的数据,这里设置为false
register-with-eureka: false # 表示是否将自己注册到Eureka Server,默认为true.由于当前应用就是Eureka Server,故而设置为false.
service-url:
# 设置与Eureka Server的地址,查询服务和注册服务都需要依赖这个地址.默认是http://localhost:8761/eureka/;多个地址可使用','风格.
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
# 安全认证的配置(非必需配置,需要引入Security安全框架依赖)
#spring:
# security:
# user:
# name: admin
# password: a
EurekaApplication启动类
@SpringBootApplication
@EnableEurekaServer//开启注册中心
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
// @EnableWebSecurity//非必须配置,引入Security安全框架才需此配置
// static class WebSecurityConfig extends WebSecurityConfigurerAdapter {
// @Override
// protected void configure(HttpSecurity http) throws Exception {
// http.csrf().ignoringAntMatchers("/eureka/**");
// super.configure(http);
// }
// }
}
启动Eureka注册中心,打开浏览器输入localhost:8080
2、Service服务端,选择spring web和Eureka Discovery Client
Service application.yml
server:
port: 8082
tomcat:
uri-encoding: UTF-8
servlet:
encoding:
charset: UTF-8
spring:
application:
name: service
eureka:
client:
service-url:
defaultZone: http://localhost:8080/eureka
ServiceApplication启动类
@SpringBootApplication
@EnableEurekaClient//注册到注册中心
public class ServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceApplication.class, args);
}
}
Service 测试类
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("service")
public class TestController {
@Value("${server.port}")
private String port;
@GetMapping("hello")
public String hello() {
return "hello,I'm " + port + "service!";
}
}
3、Client客户端,选择spring web和Eureka Discovery Client
Client application.yml
server:
port: 8081
tomcat:
uri-encoding: UTF-8
servlet:
encoding:
charset: UTF-8
spring:
application:
#name配置后不建议修改,应第一次配置的时候就确定好。因为后面调用服务端都会用到这个name。
name: client
# 安全认证的配置(非必须配置)
# security:
# user:
# name: admin
# password: 123456
eureka:
client:
register-with-eureka: false #不注册到注册中心,默认是注册到注册中心的。
service-url:
# defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@localhost:8080/eureka # 指定服务注册中心
defaultZone: http://localhost:8080/eureka # 指定服务注册中心
ClientApplication启动类
@SpringBootApplication
@EnableEurekaClient//注册到注册中心
@EnableHystrix //开启断路器(需要引入断路器依赖)
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
//开启负载均衡
@Bean
@LoadBalanced//此注解的作用是让spring boot项目之间可以通过application name相互访问,以达到负载均衡的目的。
RestTemplate restTemplate(){
return new RestTemplate();
}
}
Client pom.xml dependencies加入断路器依赖
org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.cloud spring-cloud-starter-netflix-hystrix 2.2.9.RELEASE
Client 测试类
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@RestController
@RequestMapping("client")
public class TestController {
@Resource
RestTemplate restTemplate;
@GetMapping("hello")
public String hello(){
//getForObject第一个是service服务端的地址,第二个参数是服务端地址的返回类型.还有postForObject自行去了解。
return restTemplate.getForObject("http://service/service/hello",String.class);//service是服务端application.yml里面配置的spring.application.name
}
}
依次启动Eureka、Service、Client。浏览器访问localhost:8080
为什么只有Service在里面?因为只有service注册到了Eureka注册中心,Client是没有注册到注册中心的。当然了,Client也可以注册到注册中心。这里只是为了展示。
然后浏览器再访问:localhost:8081/client/hello
这里可以看到访问client的接口地址,然后client再去访问service的接口地址,service再把数据返回给client。
下面来实现负载均衡和断路器(Hystrix降级服务)
修改复制的Service启动端口为8083。-Dserver.port=8083
启动Service2,重启Client。不重启的话,负载均衡可能会不生效。
然后在浏览器访问:localhost:8081/client/hello
第一次是8082
第二次是8083。说明负载均衡成功实现。
接下来讲断路器(Hystrix降级服务)。把Service2停掉,在浏览器访问:localhost:8081/client/hello
第一次
第二次报500。说明断路器(Hystrix降级服务)生效。
然后一直访问,最后会发现只请求8082端口的service。
访问localhost:8080,发现Service 8083端口的多了个DOWN(1)。
至此一个简单的spring cloud分布式架构就搭建好了。
断路器(Hystrix降级服务)具体的实现Spring cloud Hystrix 降级服务



