spring cloud (四、服务消费者demo_consumer)
spring cloud (一、服务注册demo_eureka)
spring cloud (二、服务注册安全demo_eureka)
spring cloud (三、服务提供者demo_provider)
写完这些案例的demo后面有时间再写这个框架的思想;
注册中心负责服务管理;提供者负责提供服务;消费者调用提供者的服务;
消费者的demo也是一样建一个spring boot 项目;然后修改pom文件如下:
<?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.example</groupId> <artifactId>consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>consumer</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> <relativePath /> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR3</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> <repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
配置信息
#应用(服务)名称 spring.application.name=eureka-consumer server.port=8771 eureka.client.serviceUrl.defaultZone=http://admin:admin@localhost:3333/eureka/
启动类
/** * 消费者 */ @EnableFeignClients @EnableDiscoveryClient @SpringBootApplication public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } }
到这里为止;服务注册、提供者、消费者三个demo已经可以正常启动了;并且在服务注册控制台也可以看到对应的服务信息;
接下来我们在提供者的demo中写一个服务;返回一个字符串
创建controller和service
接口
public interface TestService { String test01(); }
实现类
@Service public class TestServiceImp implements TestService { @Override public String test01() { System.out.println(3); return "Hello World"; } }
controller
@Controller public class TestController { @Autowired private TestService testService; @RequestMapping("/test") @ResponseBody public String home() { System.out.println(2); return testService.test01(); } }
接下来在消费者demo中写调用的代码
创建controller和service
接口
/** * //指定访问的服务名称 */ @Service @FeignClient(name= "eureka-provider") public interface TestService { /** * 指定请求的url * @return */ @RequestMapping(value = "/test") String hello(); }
以上服务名称和url都必须要和提供者那边的对应
controller
@Controller public class TestController { @Autowired private TestService testService; @RequestMapping("/test01") @ResponseBody public String index() { System.out.println(1); return testService.hello(); } }
此时的代码已经写完了,我们通过访问http://localhost:8771/test01可以看到提供者那边返回的字符串