直接用上一篇搭建好的spring cloud注册中心集群,编写服务提供者provider和服务消费者consumer
服务提供Provider
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.springcloud</groupId> <artifactId>provider</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version> </properties><!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies --> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.yml
spring: application: name: provider //应用名称,即注册中心里注册的名称 server: port: 8050 eureka: client: serviceUrl: defaultZone: http://peer2:8001/eureka/,http://peer3:8002/eureka/
启动类
@SpringBootApplication @EnableDiscoveryClient //表明项目具有服务注册的功能,可以在注册中心看到 public class ApplicationStarter { public static void main(String[] args) { SpringApplication.run(ApplicationStarter.class); } }
Controller
@RestController @RequestMapping("/provider") public class HelloController { @GetMapping("/hello") public Map hello(){ Map<String,String> map=new HashMap<>(); map.put("messages","Hello world!"); return map; } }
服务调用Consumer
pom.xml
与服务提供Provider一致
application.yml
spring: application: name: consumer
//默认8080端口
eureka: client: service-url: defaultZone: http://peer2:8001/eureka/,http://peer3:8002/eureka/
启动类
@SpringBootApplication @EnableDiscoveryClient public class ApplicationStart { @Bean @LoadBalanced RestTemplate restTemplate(){ return new RestTemplate(); //利用restTemplate去查找服务并调用它 } public static void main(String[] args) { SpringApplication.run(ApplicationStart.class); } }
Controller
@RestController @RequestMapping("/message") public class MessageController { @Autowired RestTemplate restTemplate; @GetMapping("/hello") public Map hello(){ return restTemplate.getForObject("http://PROVIDER/provider/hello", HashMap.class); //这里通过注册名称调用,不是域名 } }
启动Provider和Consumer,在注册中心可以看到:
浏览器输出: localhost:8080/message/hello
一个最基本的springcloud集群版的Hello World完成了