GitHub代码地址
UTF-8
UTF-8
1.8
Finchley.SR1
2.0.5.RELEASE
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-dependencies
${springboot.version}
pom
import
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class Application_Eureka {
public static void main(String[] args){
SpringApplication.run(Application_Eureka.class);
}
}
server:
port: 7001
eureka:
instance:
hostname: 127.0.0.1
client:
fetch-registry: false
register-with-eureka: false
org.springframework.boot
spring-boot-starter-test
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
@SpringBootApplication
@EnableEurekaClient
public class Application_Provider {
public static void main(String[] args){
SpringApplication.run(Application_Provider.class);
}
}
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("provider")
public class ProviderController {
@RequestMapping("get")
public String getData(){
return "提供了数据";
}
}
server:
port: 8001
spring:
application:
name: TEST-PROVIDER #服务名称,随意取
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:7001/eureka #注册到Eureka的地址
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
@SpringBootApplication
public class Application_Consumer {
public static void main(String[] args){
SpringApplication.run(Application_Consumer.class);
}
}
@RestController
@RequestMapping("consumer")
public class ConsumerController {
//资源路径
private final String url = "http://127.0.0.1:8001/provider/get";
@RequestMapping("get")
public String get(){
RestTemplate template = new RestTemplate();
String data = template.getForObject(url, String.class);
return data;
}
}
server:
port: 9001
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-netflix-zuul
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class Application_Zuul {
public static void main(String[] args){
SpringApplication.run(Application_Zuul.class);
}
}
server:
port: 9527
spring:
application:
name: ZUUL-SERVER #Zuul网关在Eureka的服务名称,随便取
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:7001/eureka #Eureka的注册地址
zuul:
routes:
provider.serviceId: TEST-PROVIDER #没有提示,自己取名称,值为服务的名称
server:
port: 9527
spring:
application:
name: ZUUL-SERVER
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:7001/eureka
zuul:
routes:
provider.serviceId: TEST-PROVIDER
provider.path: /test/** #匹配指定的路径,资源匹配的路径才会拦截,转发
server:
port: 9527
spring:
application:
name: ZUUL-SERVER
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:7001/eureka
zuul:
routes:
provider.serviceId: TEST-PROVIDER
provider.path: /test/** #匹配指定的路径,资源匹配的路径才会拦截,转发
ignored-services: "*" #忽略直接使用服务名 *忽略所有的服务
prefix: /server #统一给访问前缀
我们要在一个服务提供者中使用另一个服务提供者的服务,需要使用fegin进行内部服务的调用
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-openfeign
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients("com.wlg.feign.client") //feign接口的地址(上图的client目录)
public class Application_Feign {
public static void main(String[] args){
SpringApplication.run(Application_Feign.class);
}
}
application.yml
server:
port: 9002
spring:
application:
name: FEIGN-CONSUMER
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:7001/eureka
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
@FeignClient(value = "TEST-PROVIDER") //要调用的服务名称
@RequestMapping("provider") //和调用服务controller的一致
public interface DataClient {
//和调用服务controller的一致
@RequestMapping("get")
public String getData();
}
import com.wlg.feign.client.DataClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("consumer")
public class DataFeignController {
@Autowired
private DataClient dataClient;
@RequestMapping("feignGet")
public String get(){
return dataClient.getData();
}
}
配置中心(config),熔断器(hystrix),(负载均衡)ribbon
下一章:https://blog.csdn.net/qq_44575680/article/details/106784855