依赖:
org.springframework.cloud spring-cloud-starter-netflix-eureka-serverorg.springframework.boot spring-boot-starter-testtest
yml配置:
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: online
启动类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaDemoApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaDemoApplication.class, args);
}
}
浏览器访问:http://localhost:8761/
依赖:
4.0.0 org.springframework.boot spring-boot-starter-parent2.5.5 com.example product-demo0.0.1-SNAPSHOT product-demo Demo project for Spring Boot 1.8 2020.0.4 org.springframework.cloud spring-cloud-starter-netflix-eureka-clientorg.springframework.boot spring-boot-starter-webcom.alibaba fastjson1.2.72 io.github.openfeign feign-httpclient10.2.0 org.springframework.boot spring-boot-starter-testtest org.springframework spring-web5.3.10 org.springframework.cloud spring-cloud-dependencies${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
yml配置:
server:
port: 8763
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: server-product
controller类:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ProductDemoController {
@Value("${server.port}")
String port ;
@GetMapping("/hi")
public String home(@RequestParam String name){
return "hi ... " + name + ",i am from port:" + port;
}
}
启动类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class ProductDemoApplication {
public static void main(String[] args) {
SpringApplication.run(ProductDemoApplication.class, args);
}
}
访问:http://localhost:8763/hi?name=xxx
并且注册中心界面也可以看到
依赖:
4.0.0 org.springframework.boot spring-boot-starter-parent2.5.5 com.example product-demo0.0.1-SNAPSHOT product-demo Demo project for Spring Boot 1.8 2020.0.4 org.springframework.cloud spring-cloud-starter-netflix-eureka-clientorg.springframework.boot spring-boot-starter-webcom.alibaba fastjson1.2.72 io.github.openfeign feign-httpclient10.2.0 org.springframework.boot spring-boot-starter-testtest org.springframework spring-web5.3.10 org.springframework.cloud spring-cloud-starter-netflix-hystrix2.2.9.RELEASE org.springframework.cloud spring-cloud-dependencies${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
yml配置:
server:
port: 8764
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: server-consumer
controller类:
import com.example.consumerdemo.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloControler {
@Autowired
HelloService helloService;
@RequestMapping(value = "/hi")
public String hi(@RequestParam String name)
{
return helloService.hiService(name);
}
}
service层:
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "hiError")
public String hiService(String name) {
return restTemplate.getForObject("http://server-product/hi?name=" + name, String.class);
}
public String hiError(String name) {
return "key-->" + name + ", this is error page..didi ..." ;
}
}
启动类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@EnableDiscoveryClient
@SpringBootApplication
@EnableHystrix
public class ConsumerDemoApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerDemoApplication.class, args);
}
@Bean
@LoadBalanced
// 标注此注解后,RestTemplate就具有了客户端负载均衡能力
RestTemplate template (){
return new RestTemplate();
}
}
访问:http://localhost:8764/hi?name=oop(熔断机制可以停掉生产方服务触发)
1)zuul
依赖:
4.0.0 org.springframework.boot spring-boot-starter-parent2.1.3.RELEASE com.example zuul-demo0.0.1-SNAPSHOT zuul-demo Demo project for Spring Boot 1.8 Greenwich.RELEASE org.springframework.boot spring-boot-starter-webfluxorg.springframework.cloud spring-cloud-starter-netflix-eureka-client2.1.0.RELEASE org.springframework.boot spring-boot-starter-testtest io.projectreactor reactor-testtest org.springframework.cloud spring-cloud-starter-netflix-zuul2.1.0.RELEASE org.springframework.cloud spring-cloud-starter-zuul1.4.7.RELEASE org.springframework.cloud spring-cloud-starter-eureka1.4.7.RELEASE org.junit.jupiter junit-jupiter-api5.7.2 test org.springframework.cloud spring-cloud-dependenciesGreenwich.RELEASE pom import org.springframework.boot spring-boot-maven-plugin
yml:
#端口号
server:
port: 8765
#配置应用名称
spring:
application:
name: zuul-server
#eureka
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
#zuul
#前缀
#zuul.prefix=/tour
zuul:
routes:
online:
path: /online
@EnableDiscoveryClient
@SpringBootApplication
public class GatewayDemoApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayDemoApplication.class, args);
}
}
启动服务即可:http://localhost:8765/online/hi?name=ooo
注册中心:
简单的搭建就完毕啦。



