Open zhuangjinxin opened 6 years ago
服务注册与发现 Eukera:
build.gradle
dependencies {
compile('org.springframework.cloud:spring-cloud-starter-eureka-server')
}
application.properties
server.port=8761
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
EukeraServerApplication.java
@SpringBootApplication
@EnableEurekaServer
public class ServerApplication {
public static void main(String[] args) {
SpringApplication.run(EukeraServerApplication.class, args);
}
}
访问地址:http://localhost:8761/
Ribbon:
build.gradle
dependencies {
compile('org.springframework.cloud:spring-cloud-starter-eureka-server')
compile('org.springframework.cloud:spring-cloud-starter-ribbon')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.cloud:spring-cloud-starter-hystrix')
compile('org.springframework.cloud:spring-cloud-starter-hystrix-dashboard')
}
application.properties
spring.application.name=ribbon
server.port=8764
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
RibbonApplication.java
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
@EnableHystrixDashboard
public class RibbonApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
HelloController.java
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@RequestMapping(value = "/hi")
public String hi(@RequestParam String name){
return helloService.hiService(name);
}
}
HelloService.java
@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "helloError")
public String hiService(String name) {
return restTemplate.getForObject("http://client/hi?name="+name,String.class);
}
public String helloError(String name) {
return "hi,"+name+",sorry,error!";
}
}
访问地址:http://localhost:8764/hi?name=forezp
Feign:
build.gradle
dependencies {
compile('org.springframework.cloud:spring-cloud-starter-eureka-server')
compile('org.springframework.cloud:spring-cloud-starter-feign')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.cloud:spring-cloud-starter-hystrix')
compile('org.springframework.cloud:spring-cloud-starter-hystrix-dashboard')
}
application.properties
spring.application.name=feign
server.port=8765
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
feign.hystrix.enabled=true
FeignApplication.java
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrix
@EnableHystrixDashboard
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}
}
HelloController.java
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@RequestMapping(value = "/hi",method = RequestMethod.GET)
public String sayHi(@RequestParam String name){
return helloService.sayHiFromClient(name);
}
}
HellService.java
@FeignClient(value = "client",fallback = HelloErrorService.class)
public interface HelloService {
@RequestMapping(value = "/hi",method = RequestMethod.GET)
String sayHiFromClient(@RequestParam(value = "name") String name);
}
HelloErrorService.java
@Component
public class HelloErrorService implements HelloService{
@Override
public String sayHiFromClient(String name) {
return "sorry "+name;
}
}
访问地址:http://localhost:8765/hi?name=forezp
断路器 Hystrix:
如上!
Hystrix Dashboard:
添加@EnableHystrixDashboard
开启Hystrix Dashboard
访问地址:
1.http://localhost:8764/hystrix/monitor
2.http://localhost:8765/hystrix/monitor
路由网关 Zuul:
build.gredle
dependencies {
compile('org.springframework.cloud:spring-cloud-starter-eureka-server')
compile('org.springframework.cloud:spring-cloud-starter-ribbon')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.cloud:spring-cloud-starter-hystrix')
compile('org.springframework.cloud:spring-cloud-starter-hystrix-dashboard')
}
application.properties
spring.application.name=zuul
server.port=8769
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
zuul.routes.ribbon.path=/ribbon/*
zuul.routes.ribbon.serviceId=ribbon
zuul.routes.feign.path=/feign/*
zuul.routes.feign.serviceId=feign
ZuulApplication.java
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class, args);
}
}
访问地址:
1.http://localhost:8769/ribbon/hi?name=forezp
2.http://localhost:8769/feign/hi?name=forezp
拦截器: MyFilter.java
@Component
public class MyFilter extends ZuulFilter {
private static Logger log = LoggerFactory.getLogger(MyFilter.class);
@Override
public String filterType() {
return "pre";
}
@Override
public int filterOrder() {
return 0;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
log.info(String.format("%s >>> %s", request.getMethod(), request.getRequestURL().toString()));
Object accessToken = request.getParameter("token");
if(accessToken == null) {
log.warn("token is empty");
ctx.setSendZuulResponse(false);
ctx.setResponseStatusCode(401);
try {
ctx.getResponse().getWriter().write("token is empty");
}catch (Exception e){}
return null;
}
log.info("ok");
return null;
}
}
访问地址:
1.http://localhost:8769/ribbon/hi?name=forezp
2.http://localhost:8769/ribbon/hi?name=forezp&token=1
Spring Cloud Config:
Config Server 端: build.gradle
dependencies {
compile('org.springframework.cloud:spring-cloud-config-server')
compile('org.springframework.cloud:spring-cloud-starter-eureka-server')
}
application.properties
spring.application.name=config-server
server.port=8888
spring.cloud.config.server.git.uri=https://github.com/zhuangjinxin/spring-cloud
spring.cloud.config.server.git.searchPaths=config
spring.cloud.config.label=master
spring.cloud.config.server.git.username=USERNAME
spring.cloud.config.server.git.password=PASSWORD
ConfigServerApplication.java
@SpringBootApplication
@EnableConfigServer
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
访问地址:http://localhost:8888/config-client/dev
Config Client 端: build.gradle
dependencies {
compile('org.springframework.cloud:spring-cloud-starter-config')
compile('org.springframework.boot:spring-boot-starter-web')
bootstrap.properties
spring.application.name=config-client
spring.cloud.config.label=master
spring.cloud.config.profile=dev
spring.cloud.config.uri= http://localhost:8888/
server.port=8881
ConfigClientApplication.java
@SpringBootApplication
@RestController
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
@Value("${foo}")
String foo;
@RequestMapping(value = "/hi")
public String hi(){
return foo;
}
}
访问地址:http://localhost:8881/hi
参考文章:
http://blog.csdn.net/forezp/article/details/70148833