<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-configartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-eureka-serverartifactId>
dependency>
dependencies>
@SpringBootApplication
@EnableEurekaServer
public class ServerApp {
public static void main(String[] args) {
new SpringApplicationBuilder(ServerApp.class).web(true).run(args);
}
}
server:
port: 8761
eureka:
client:
register-with-eureka: false
fetch-registry: false
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-configartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-eurekaartifactId>
dependency>
public class Member {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@RestController
public class MainController {
@RequestMapping(value = "/member/{id}", method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public Member getMember(@PathVariable Integer id) {
Member p = new Member();
p.setId(id);
p.setName("angus");
return p;
}
}
@SpringBootApplication
@EnableEurekaClient
public class MemberApp {
public static void main(String[] args) {
new SpringApplicationBuilder(MemberApp.class).web(true).run(args);
}
}
spring:
application:
name: spring-zuul-member
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-configartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-eurekaartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-ribbonartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-feignartifactId>
dependency>
@FeignClient("spring-zuul-member")
public interface MemberClient {
@RequestMapping(value = "/member/{id}", method = RequestMethod.GET)
public Member getMember(@PathVariable("id") Integer id);
}
@RestController
public class TestController {
@Autowired
private MemberClient memberClient;
@RequestMapping(value = "/food-sale/{memberId}", method = RequestMethod.GET)
public String foodSale(@PathVariable Integer memberId) {
Member m = memberClient.getMember(memberId);
System.out.println(m.getId() + "---" + m.getName());
return "success";
}
}
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class SaleApp {
public static void main(String[] args) {
new SpringApplicationBuilder(SaleApp.class).web(true).run(args);
}
}
server:
port: 8081
spring:
application:
name: spring-zuul-sale
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-configartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-eurekaartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-zuulartifactId>
dependency>
<dependency>
<groupId>org.apache.httpcomponentsgroupId>
<artifactId>httpclientartifactId>
<version>4.5.2version>
dependency>
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class GatewayApp {
public static void main(String[] args) {
new SpringApplicationBuilder(GatewayApp.class).web(true).run(args);
}
}
server:
port: 9000
spring:
application:
name: spring-zuul-gateway
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
zuul:
routes:
sale:
path: /sale/**
serviceId: spring-zuul-sale
Eureka服务器、服务提供者、服务调用者、网关
访问:http://localhost:9000/sale/food-sale/2
注意:Zuul在调用客户端访问服务的时候,底层默认采用的是HttpClient进行通信,如果要更换需进行如下配置:
以上为疯狂SpringCloud微服务架构实战学习笔记
感谢杨恩雄老师:https://my.oschina.net/JavaLaw