注册中心(Eureka):
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- 集成Eureka服务端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
application.properties
#端口号
server.port=1001
#表示是否将自己注册到Eureka Server,默认为true
#当前应用就是Eureka Server,设置为false
eureka.client.register-with-eureka=false
#是否从Eureka Server获取注册信息,默认为true
#单点的Eureka Server,不需要同步其他Eureka Server节点的数据,设置为false
eureka.client.fetch-registry=false
#设置与Eureka Server交互的地址,查询和注册服务都需要依赖这个地址
eureka.client.serviceUrl.defaultZone= http://192.168.1.12:1001/eureka/
EurekaApplication
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
提供服务:
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- 集成Eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
application.properties
#注册中心
eureka.client.serviceUrl.defaultZone=http://192.168.1.12:1001/eureka/
#服务名称
spring.application.name=compute-server1
#端口号
server.port=2002
Server1Application
@EnableEurekaClient
@SpringBootApplication
public class Server1Application {
public static void main(String[] args) {
SpringApplication.run(Server1Application.class, args);
}
}
Server1Controller
@RestController
public class Server1Controller {
@Autowired
private Server1Service server1Service;
@RequestMapping("test")
public String test(){
return "server1-test";
}
@RequestMapping("server1")
public String test(String name){
String s = server1Service.test(name);
return s;
}
}
Server1Service/Impl
public interface Server1Service {
public String test(String name);
}
@Service
public class Server1ServiceImpl implements Server1Service {
@Override
public String test(String name) {
return "server1"+name;
}
}
消费服务(Feigin):
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- 集成Eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
application.properties
#注册中心
eureka.client.serviceUrl.defaultZone=http://192.168.1.12:1001/eureka/
#服务名称
spring.application.name=SpringCould-Feigin
#端口号
server.port=1003
#断路器
feign.hystrix.enabled=true
FeiginApplication
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class FeiginApplication {
public static void main(String[] args) {
SpringApplication.run(FeiginApplication.class, args);
}
}
FeiginController
@RestController
public class FeiginController {
@Autowired
private FeiginService feiginService;
@RequestMapping("test")
public String test(){
return "feigin-test";
}
@RequestMapping("feigin")
public String test(String name){
String s = feiginService.test(name);
return s;
}
}
FeiginService/Impl
@FeignClient(value = "compute-server1",fallback = FeiginServiceImpl.class)
public interface FeiginService {
@RequestMapping("server1")
public String test(@RequestParam("name") String name);
}
@Component
public class FeiginServiceImpl implements FeiginService {
@Override
public String test(String name) {
return "调用服务失败,启用断路器";
}
}
消费服务(Ribbon):
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- 集成Eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
application.properties
#注册中心
eureka.client.serviceUrl.defaultZone=http://192.168.1.12:1001/eureka/
#服务名称
spring.application.name=SpringCould-Ribbon
#端口号
server.port=1004
RibbonApplication
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
public class RibbonApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate template(){
return new RestTemplate();
}
}
RibbonController
@RestController
public class RibbonController {
@Autowired
private RibbonServiceImpl ribbonService;
@RequestMapping("test")
public String test(){
return "ribbon-test";
}
@RequestMapping("ribbon")
public String test(String name){
String s = ribbonService.test(name);
return s;
}
}
RibbonServiceImpl
@Service
public class RibbonServiceImpl{
@Autowired
private RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "error")
public String test(String name){
return restTemplate.getForObject("http://compute-server1/server1?name="+name,String.class);
}
public String error(String name) {
return "调用服务失败,启用断路器";
}
}
网关(Zuul):
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- 集成Eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
application.properties
#注册中心
eureka.client.serviceUrl.defaultZone=http://192.168.1.12:1001/eureka/
#服务名称
spring.application.name=SpringCould-Zuul
#端口号
server.port=1002
#创建的服务
zuul.routes.compute-server1=/api-a/**
zuul.routes.compute-server2=/api-b/**
ZuulApplication
@EnableZuulProxy
@SpringBootApplication
public class ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class, args);
}
}